Merge pull request #7 from davidnewhall/dn2_bugs
Keep String and Number for FlexInt type. Resolve uptime unmarhsal bug.
This commit is contained in:
		
						commit
						5b2864d63f
					
				|  | @ -62,7 +62,7 @@ func (u UAP) Points() ([]*influx.Point, error) { | ||||||
| 		"rx_bytes-d":                 u.RxBytesD, | 		"rx_bytes-d":                 u.RxBytesD, | ||||||
| 		"tx_bytes":                   u.TxBytes, | 		"tx_bytes":                   u.TxBytes, | ||||||
| 		"tx_bytes-d":                 u.TxBytesD, | 		"tx_bytes-d":                 u.TxBytesD, | ||||||
| 		"uptime":                     u.Uptime, | 		"uptime":                     u.Uptime.Number, | ||||||
| 		"considered_lost_at":         u.ConsideredLostAt, | 		"considered_lost_at":         u.ConsideredLostAt, | ||||||
| 		"next_heartbeat_at":          u.NextHeartbeatAt, | 		"next_heartbeat_at":          u.NextHeartbeatAt, | ||||||
| 		"scanning":                   u.Scanning, | 		"scanning":                   u.Scanning, | ||||||
|  | @ -181,7 +181,7 @@ func (u UAP) Points() ([]*influx.Point, error) { | ||||||
| 			"device_mac":   u.Mac, | 			"device_mac":   u.Mac, | ||||||
| 			"name":         p.Name, | 			"name":         p.Name, | ||||||
| 			"wlangroup_id": p.WlangroupID, | 			"wlangroup_id": p.WlangroupID, | ||||||
| 			"channel":      strconv.Itoa(int(p.Channel)), | 			"channel":      p.Channel.String, | ||||||
| 			"radio":        p.Radio, | 			"radio":        p.Radio, | ||||||
| 		} | 		} | ||||||
| 		fields := map[string]interface{}{ | 		fields := map[string]interface{}{ | ||||||
|  | @ -196,7 +196,7 @@ func (u UAP) Points() ([]*influx.Point, error) { | ||||||
| 			"min_txpower":          p.MinTxpower, | 			"min_txpower":          p.MinTxpower, | ||||||
| 			"nss":                  p.Nss, | 			"nss":                  p.Nss, | ||||||
| 			"radio_caps":           p.RadioCaps, | 			"radio_caps":           p.RadioCaps, | ||||||
| 			"tx_power":             p.TxPower, | 			"tx_power":             p.TxPower.Number, | ||||||
| 			"tx_power_mode":        p.TxPowerMode, | 			"tx_power_mode":        p.TxPowerMode, | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -306,7 +306,7 @@ type UAP struct { | ||||||
| 		UplinkRemotePort int     `json:"uplink_remote_port"` | 		UplinkRemotePort int     `json:"uplink_remote_port"` | ||||||
| 	} `json:"uplink"` | 	} `json:"uplink"` | ||||||
| 	UplinkTable []interface{} `json:"uplink_table"` | 	UplinkTable []interface{} `json:"uplink_table"` | ||||||
| 	Uptime      float64       `json:"uptime"` | 	Uptime      FlexInt       `json:"uptime"` | ||||||
| 	UserNumSta  int           `json:"user-num_sta"` | 	UserNumSta  int           `json:"user-num_sta"` | ||||||
| 	VapTable    []struct { | 	VapTable    []struct { | ||||||
| 		ApMac               string  `json:"ap_mac"` | 		ApMac               string  `json:"ap_mac"` | ||||||
|  |  | ||||||
|  | @ -34,22 +34,25 @@ type AuthedReq struct { | ||||||
| 
 | 
 | ||||||
| // FlexInt provides a container and unmarshalling for fields that may be
 | // FlexInt provides a container and unmarshalling for fields that may be
 | ||||||
| // numbers or strings in the Unifi API
 | // numbers or strings in the Unifi API
 | ||||||
| type FlexInt int | type FlexInt struct { | ||||||
|  | 	Number float64 | ||||||
|  | 	String string | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| // UnmarshalJSON converts a string or number to an integer.
 | // UnmarshalJSON converts a string or number to an integer.
 | ||||||
| func (value *FlexInt) UnmarshalJSON(b []byte) error { | func (f *FlexInt) UnmarshalJSON(b []byte) error { | ||||||
| 	var unk interface{} | 	var unk interface{} | ||||||
| 	if err := json.Unmarshal(b, &unk); err != nil { | 	if err := json.Unmarshal(b, &unk); err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	switch i := unk.(type) { | 	switch i := unk.(type) { | ||||||
| 	case float64: | 	case float64: | ||||||
| 		*value = FlexInt(i) | 		f.Number = i | ||||||
|  | 		f.String = strconv.FormatFloat(i, 'f', -1, 64) | ||||||
| 		return nil | 		return nil | ||||||
| 	case string: | 	case string: | ||||||
| 		// If it's a string like the word "auto" just set the integer to 0 and proceed.
 | 		f.String = i | ||||||
| 		j, _ := strconv.Atoi(i) | 		f.Number, _ = strconv.ParseFloat(i, 64) | ||||||
| 		*value = FlexInt(j) |  | ||||||
| 		return nil | 		return nil | ||||||
| 	default: | 	default: | ||||||
| 		return errors.New("Cannot unmarshal to FlexInt") | 		return errors.New("Cannot unmarshal to FlexInt") | ||||||
|  |  | ||||||
|  | @ -10,15 +10,23 @@ import ( | ||||||
| func TestFlexInt(t *testing.T) { | func TestFlexInt(t *testing.T) { | ||||||
| 	t.Parallel() | 	t.Parallel() | ||||||
| 	a := assert.New(t) | 	a := assert.New(t) | ||||||
| 	type reply struct { | 	type testReply struct { | ||||||
| 		Channel FlexInt `json:"channel"` | 		Five  FlexInt `json:"five"` | ||||||
|  | 		Seven FlexInt `json:"seven"` | ||||||
|  | 		Auto  FlexInt `json:"auto"` | ||||||
| 	} | 	} | ||||||
| 	var r reply | 	var r testReply | ||||||
| 	a.Nil(json.Unmarshal([]byte(`{"channel": "5"}`), &r)) | 	// test unmarshalling the custom type three times with different values.
 | ||||||
| 	a.EqualValues(FlexInt(5), r.Channel) | 	a.Nil(json.Unmarshal([]byte(`{"five": "5", "seven": 7, "auto": "auto"}`), &r)) | ||||||
| 	a.Nil(json.Unmarshal([]byte(`{"channel": 7}`), &r)) | 	// test number in string.
 | ||||||
| 	a.EqualValues(FlexInt(7), r.Channel) | 	a.EqualValues(5, r.Five.Number) | ||||||
|  | 	a.EqualValues("5", r.Five.String) | ||||||
|  | 	// test number.
 | ||||||
|  | 	a.EqualValues(7, r.Seven.Number) | ||||||
|  | 	a.EqualValues("7", r.Seven.String) | ||||||
|  | 	// test string.
 | ||||||
| 	a.Nil(json.Unmarshal([]byte(`{"channel": "auto"}`), &r), | 	a.Nil(json.Unmarshal([]byte(`{"channel": "auto"}`), &r), | ||||||
| 		"a regular string must not produce an unmarshal error") | 		"a regular string must not produce an unmarshal error") | ||||||
| 	a.EqualValues(FlexInt(0), r.Channel) | 	a.EqualValues(0, r.Auto.Number) | ||||||
|  | 	a.EqualValues("auto", r.Auto.String) | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue