mirror of
https://github.com/unpoller/unpoller.git
synced 2026-10-18 22:40:03 +02:00
Check for normal string and accept it.
This commit is contained in:
@@ -47,9 +47,10 @@ func (value *FlexInt) UnmarshalJSON(b []byte) error {
|
||||
*value = FlexInt(i)
|
||||
return nil
|
||||
case string:
|
||||
j, err := strconv.Atoi(i)
|
||||
// If it's a string like the word "auto" just set the integer to 0 and proceed.
|
||||
j, _ := strconv.Atoi(i)
|
||||
*value = FlexInt(j)
|
||||
return err
|
||||
return nil
|
||||
default:
|
||||
return errors.New("Cannot unmarshal to FlexInt")
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ func TestFlexInt(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
five := []byte(`{"channel": "5"}`)
|
||||
seven := []byte(`{"channel": 7}`)
|
||||
auto := []byte(`{"channel": "auto"}`)
|
||||
type reply struct {
|
||||
Channel FlexInt `json:"channel"`
|
||||
}
|
||||
@@ -20,4 +21,6 @@ func TestFlexInt(t *testing.T) {
|
||||
a.EqualValues(FlexInt(5), r.Channel)
|
||||
a.Nil(json.Unmarshal(seven, &r))
|
||||
a.EqualValues(FlexInt(7), r.Channel)
|
||||
a.NotNil(json.Unmarshal(auto, &r))
|
||||
a.EqualValues(FlexInt(0), r.Channel)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user