whatever
This commit is contained in:
parent
2ce0e76c85
commit
d6f1ca10c7
|
|
@ -93,18 +93,16 @@ func (f *FlexInt) UnmarshalJSON(b []byte) error {
|
||||||
case float64:
|
case float64:
|
||||||
f.Val = i
|
f.Val = i
|
||||||
f.Txt = strconv.FormatFloat(i, 'f', -1, 64)
|
f.Txt = strconv.FormatFloat(i, 'f', -1, 64)
|
||||||
return nil
|
|
||||||
case string:
|
case string:
|
||||||
f.Txt = i
|
f.Txt = i
|
||||||
f.Val, _ = strconv.ParseFloat(i, 64)
|
f.Val, _ = strconv.ParseFloat(i, 64)
|
||||||
return nil
|
|
||||||
case nil:
|
case nil:
|
||||||
f.Txt = "0"
|
f.Txt = "0"
|
||||||
f.Val = 0
|
f.Val = 0
|
||||||
return nil
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("cannot unmarshal to FlexInt: %s", b)
|
return fmt.Errorf("cannot unmarshal to FlexInt: %s", b)
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FlexBool provides a container and unmarshalling for fields that may be
|
// FlexBool provides a container and unmarshalling for fields that may be
|
||||||
|
|
|
||||||
|
|
@ -15,10 +15,11 @@ func TestFlexInt(t *testing.T) {
|
||||||
Seven FlexInt `json:"seven"`
|
Seven FlexInt `json:"seven"`
|
||||||
Auto FlexInt `json:"auto"`
|
Auto FlexInt `json:"auto"`
|
||||||
Channel FlexInt `json:"channel"`
|
Channel FlexInt `json:"channel"`
|
||||||
|
Nil FlexInt `json:"nil"`
|
||||||
}
|
}
|
||||||
var r testReply
|
var r testReply
|
||||||
// test unmarshalling the custom type three times with different values.
|
// test unmarshalling the custom type three times with different values.
|
||||||
a.Nil(json.Unmarshal([]byte(`{"five": "5", "seven": 7, "auto": "auto"}`), &r))
|
a.Nil(json.Unmarshal([]byte(`{"five": "5", "seven": 7, "auto": "auto", "nil": null}`), &r))
|
||||||
|
|
||||||
// test number in string.
|
// test number in string.
|
||||||
a.EqualValues(5, r.Five.Val)
|
a.EqualValues(5, r.Five.Val)
|
||||||
|
|
@ -33,4 +34,7 @@ func TestFlexInt(t *testing.T) {
|
||||||
a.NotNil(json.Unmarshal([]byte(`{"channel": {}}`), &r),
|
a.NotNil(json.Unmarshal([]byte(`{"channel": {}}`), &r),
|
||||||
"a non-string and non-number must produce an error.")
|
"a non-string and non-number must produce an error.")
|
||||||
a.EqualValues(0, r.Channel.Val)
|
a.EqualValues(0, r.Channel.Val)
|
||||||
|
// test null.
|
||||||
|
a.EqualValues(0, r.Nil.Val)
|
||||||
|
a.EqualValues("0", r.Nil.Txt)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue