From 7d2dd6070fe607f8e810bedd5c086a669bf996ab Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Mon, 1 Jul 2019 00:59:43 -0700 Subject: [PATCH 1/2] Try this. --- config.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index a189e0d..96dc0f6 100644 --- a/config.go +++ b/config.go @@ -2,6 +2,8 @@ package deluge import ( "encoding/json" + "strconv" + "strings" "time" ) @@ -95,7 +97,7 @@ type XferStatus2 struct { SeedsPeersRatio float64 `json:"seeds_peers_ratio"` SeedRank int `json:"seed_rank"` State string `json:"state"` - StopAtRatio bool `json:"stop_at_ratio"` + StopAtRatio NumBool `json:"stop_at_ratio"` StopRatio float64 `json:"stop_ratio"` TimeAdded float64 `json:"time_added"` TotalDone float64 `json:"total_done"` @@ -291,7 +293,7 @@ type XferStatusCompat struct { SeedsPeersRatio float64 `json:"seeds_peers_ratio"` SeedRank int `json:"seed_rank"` State string `json:"state"` - StopAtRatio bool `json:"stop_at_ratio"` + StopAtRatio NumBool `json:"stop_at_ratio"` StopRatio float64 `json:"stop_ratio"` TimeAdded float64 `json:"time_added"` TotalDone float64 `json:"total_done"` @@ -366,3 +368,26 @@ type XferStatusCompat struct { Endpoints []interface{} `json:"endpoints"` } `json:"trackers"` } + +// NumBool provides a container and unmarshalling for fields that may be +// boolean or numbrs in the WebUI API. +type NumBool struct { + Val bool + Num float64 +} + +// UnmarshalJSON parses fields that may be numbers or booleans. +func (f *NumBool) UnmarshalJSON(b []byte) (err error) { + switch str := strings.ToLower(strings.Trim(string(b), `"`)); str { + case "true": + f.Val = true + case "false": + f.Val = false + default: + f.Num, err = strconv.ParseFloat(str, 10) + if f.Num > 0 { + f.Val = true + } + } + return err +} From 61d3e70fb90147600739b225cb76dc2d0298092a Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Tue, 2 Jul 2019 14:44:15 -0700 Subject: [PATCH 2/2] more simpler --- config.go | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/config.go b/config.go index 96dc0f6..a09a038 100644 --- a/config.go +++ b/config.go @@ -2,8 +2,6 @@ package deluge import ( "encoding/json" - "strconv" - "strings" "time" ) @@ -97,7 +95,7 @@ type XferStatus2 struct { SeedsPeersRatio float64 `json:"seeds_peers_ratio"` SeedRank int `json:"seed_rank"` State string `json:"state"` - StopAtRatio NumBool `json:"stop_at_ratio"` + StopAtRatio Bool `json:"stop_at_ratio"` StopRatio float64 `json:"stop_ratio"` TimeAdded float64 `json:"time_added"` TotalDone float64 `json:"total_done"` @@ -293,7 +291,7 @@ type XferStatusCompat struct { SeedsPeersRatio float64 `json:"seeds_peers_ratio"` SeedRank int `json:"seed_rank"` State string `json:"state"` - StopAtRatio NumBool `json:"stop_at_ratio"` + StopAtRatio Bool `json:"stop_at_ratio"` StopRatio float64 `json:"stop_ratio"` TimeAdded float64 `json:"time_added"` TotalDone float64 `json:"total_done"` @@ -369,25 +367,14 @@ type XferStatusCompat struct { } `json:"trackers"` } -// NumBool provides a container and unmarshalling for fields that may be +// Bool provides a container and unmarshalling for fields that may be // boolean or numbrs in the WebUI API. -type NumBool struct { - Val bool - Num float64 -} +type Bool bool // UnmarshalJSON parses fields that may be numbers or booleans. -func (f *NumBool) UnmarshalJSON(b []byte) (err error) { - switch str := strings.ToLower(strings.Trim(string(b), `"`)); str { - case "true": - f.Val = true - case "false": - f.Val = false - default: - f.Num, err = strconv.ParseFloat(str, 10) - if f.Num > 0 { - f.Val = true - } - } - return err +// https://stackoverflow.com/questions/30856454/how-to-unmarshall-both-0-and-false-as-bool-from-json/56832346#56832346 +func (bit *Bool) UnmarshalJSON(b []byte) error { + txt := string(b) + *bit = Bool(txt == "1" || txt == "true") + return nil }