Skip to content

Commit b5b0792

Browse files
authored
Merge pull request #3 from golift/dn2_fix_bug
Fix number/bool unmarshal bug.
2 parents 8fba78e + 61d3e70 commit b5b0792

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

config.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ type XferStatus2 struct {
9595
SeedsPeersRatio float64 `json:"seeds_peers_ratio"`
9696
SeedRank int `json:"seed_rank"`
9797
State string `json:"state"`
98-
StopAtRatio bool `json:"stop_at_ratio"`
98+
StopAtRatio Bool `json:"stop_at_ratio"`
9999
StopRatio float64 `json:"stop_ratio"`
100100
TimeAdded float64 `json:"time_added"`
101101
TotalDone float64 `json:"total_done"`
@@ -291,7 +291,7 @@ type XferStatusCompat struct {
291291
SeedsPeersRatio float64 `json:"seeds_peers_ratio"`
292292
SeedRank int `json:"seed_rank"`
293293
State string `json:"state"`
294-
StopAtRatio bool `json:"stop_at_ratio"`
294+
StopAtRatio Bool `json:"stop_at_ratio"`
295295
StopRatio float64 `json:"stop_ratio"`
296296
TimeAdded float64 `json:"time_added"`
297297
TotalDone float64 `json:"total_done"`
@@ -366,3 +366,15 @@ type XferStatusCompat struct {
366366
Endpoints []interface{} `json:"endpoints"`
367367
} `json:"trackers"`
368368
}
369+
370+
// Bool provides a container and unmarshalling for fields that may be
371+
// boolean or numbrs in the WebUI API.
372+
type Bool bool
373+
374+
// UnmarshalJSON parses fields that may be numbers or booleans.
375+
// https://stackoverflow.com/questions/30856454/how-to-unmarshall-both-0-and-false-as-bool-from-json/56832346#56832346
376+
func (bit *Bool) UnmarshalJSON(b []byte) error {
377+
txt := string(b)
378+
*bit = Bool(txt == "1" || txt == "true")
379+
return nil
380+
}

0 commit comments

Comments
 (0)