Save the integer and the string.

This commit is contained in:
DN2 2019-01-11 01:35:56 -08:00
parent 364fede31c
commit f4e1debebd
3 changed files with 27 additions and 16 deletions

View File

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

View File

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

View File

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