Check for normal string and accept it.

This commit is contained in:
DN2 2019-01-10 01:32:17 -08:00
parent de97eea523
commit ed628f7129
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)
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")
}

View File

@ -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)
}