Skip to content

Commit

Permalink
Merge pull request #3 from golift/dn2_fix_bug
Browse files Browse the repository at this point in the history
Fix number/bool unmarshal bug.
  • Loading branch information
davidnewhall authored Jul 3, 2019
2 parents 8fba78e + 61d3e70 commit b5b0792
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,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 Bool `json:"stop_at_ratio"`
StopRatio float64 `json:"stop_ratio"`
TimeAdded float64 `json:"time_added"`
TotalDone float64 `json:"total_done"`
Expand Down Expand Up @@ -291,7 +291,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 Bool `json:"stop_at_ratio"`
StopRatio float64 `json:"stop_ratio"`
TimeAdded float64 `json:"time_added"`
TotalDone float64 `json:"total_done"`
Expand Down Expand Up @@ -366,3 +366,15 @@ type XferStatusCompat struct {
Endpoints []interface{} `json:"endpoints"`
} `json:"trackers"`
}

// Bool provides a container and unmarshalling for fields that may be
// boolean or numbrs in the WebUI API.
type Bool bool

// UnmarshalJSON parses fields that may be numbers or booleans.
// 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
}

0 comments on commit b5b0792

Please sign in to comment.