Check for normal string and accept it.
This commit is contained in:
		
							parent
							
								
									de97eea523
								
							
						
					
					
						commit
						ed628f7129
					
				|  | @ -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") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -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) | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue