Check for normal string and accept it.

This commit is contained in:
DN2 2019-01-10 01:32:17 -08:00
parent 016a39b9c3
commit b9e9bc37b0
2 changed files with 6 additions and 2 deletions

View File

@ -47,9 +47,10 @@ func (value *FlexInt) UnmarshalJSON(b []byte) error {
*value = FlexInt(i) *value = FlexInt(i)
return nil return nil
case string: 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) *value = FlexInt(j)
return err return nil
default: default:
return errors.New("Cannot unmarshal to FlexInt") return errors.New("Cannot unmarshal to FlexInt")
} }

View File

@ -12,6 +12,7 @@ func TestFlexInt(t *testing.T) {
a := assert.New(t) a := assert.New(t)
five := []byte(`{"channel": "5"}`) five := []byte(`{"channel": "5"}`)
seven := []byte(`{"channel": 7}`) seven := []byte(`{"channel": 7}`)
auto := []byte(`{"channel": "auto"}`)
type reply struct { type reply struct {
Channel FlexInt `json:"channel"` Channel FlexInt `json:"channel"`
} }
@ -20,4 +21,6 @@ func TestFlexInt(t *testing.T) {
a.EqualValues(FlexInt(5), r.Channel) a.EqualValues(FlexInt(5), r.Channel)
a.Nil(json.Unmarshal(seven, &r)) a.Nil(json.Unmarshal(seven, &r))
a.EqualValues(FlexInt(7), r.Channel) a.EqualValues(FlexInt(7), r.Channel)
a.NotNil(json.Unmarshal(auto, &r))
a.EqualValues(FlexInt(0), r.Channel)
} }