Save the integer and the string.
This commit is contained in:
parent
2cc358de9b
commit
1a8443e19a
|
|
@ -181,7 +181,7 @@ func (u UAP) Points() ([]*influx.Point, error) {
|
|||
"device_mac": u.Mac,
|
||||
"name": p.Name,
|
||||
"wlangroup_id": p.WlangroupID,
|
||||
"channel": strconv.Itoa(int(p.Channel)),
|
||||
"channel": p.Channel.String,
|
||||
"radio": p.Radio,
|
||||
}
|
||||
fields := map[string]interface{}{
|
||||
|
|
@ -196,7 +196,7 @@ func (u UAP) Points() ([]*influx.Point, error) {
|
|||
"min_txpower": p.MinTxpower,
|
||||
"nss": p.Nss,
|
||||
"radio_caps": p.RadioCaps,
|
||||
"tx_power": p.TxPower,
|
||||
"tx_power": p.TxPower.Number,
|
||||
"tx_power_mode": p.TxPowerMode,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -34,22 +34,25 @@ type AuthedReq struct {
|
|||
|
||||
// FlexInt provides a container and unmarshalling for fields that may be
|
||||
// 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.
|
||||
func (value *FlexInt) UnmarshalJSON(b []byte) error {
|
||||
func (f *FlexInt) UnmarshalJSON(b []byte) error {
|
||||
var unk interface{}
|
||||
if err := json.Unmarshal(b, &unk); err != nil {
|
||||
return err
|
||||
}
|
||||
switch i := unk.(type) {
|
||||
case float64:
|
||||
*value = FlexInt(i)
|
||||
f.Number = i
|
||||
f.String = strconv.FormatFloat(i, 'f', -1, 64)
|
||||
return nil
|
||||
case string:
|
||||
// If it's a string like the word "auto" just set the integer to 0 and proceed.
|
||||
j, _ := strconv.Atoi(i)
|
||||
*value = FlexInt(j)
|
||||
f.String = i
|
||||
f.Number, _ = strconv.ParseFloat(i, 64)
|
||||
return nil
|
||||
default:
|
||||
return errors.New("Cannot unmarshal to FlexInt")
|
||||
|
|
|
|||
|
|
@ -10,15 +10,23 @@ import (
|
|||
func TestFlexInt(t *testing.T) {
|
||||
t.Parallel()
|
||||
a := assert.New(t)
|
||||
type reply struct {
|
||||
Channel FlexInt `json:"channel"`
|
||||
type testReply struct {
|
||||
Five FlexInt `json:"five"`
|
||||
Seven FlexInt `json:"seven"`
|
||||
Auto FlexInt `json:"auto"`
|
||||
}
|
||||
var r reply
|
||||
a.Nil(json.Unmarshal([]byte(`{"channel": "5"}`), &r))
|
||||
a.EqualValues(FlexInt(5), r.Channel)
|
||||
a.Nil(json.Unmarshal([]byte(`{"channel": 7}`), &r))
|
||||
a.EqualValues(FlexInt(7), r.Channel)
|
||||
var r testReply
|
||||
// test unmarshalling the custom type three times with different values.
|
||||
a.Nil(json.Unmarshal([]byte(`{"five": "5", "seven": 7, "auto": "auto"}`), &r))
|
||||
// test number in string.
|
||||
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 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