Move FlexInt into main file, add small test.
This commit is contained in:
parent
5eea11e008
commit
a92fb9f3ef
|
|
@ -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(p.Channel.Value),
|
"channel": strconv.Itoa(int(p.Channel)),
|
||||||
"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.Value,
|
"tx_power": p.TxPower,
|
||||||
"tx_power_mode": p.TxPowerMode,
|
"tx_power_mode": p.TxPowerMode,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,36 +1,5 @@
|
||||||
package unidev
|
package unidev
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
// FlexInt provides a container and unmarshalling for fields that may be
|
|
||||||
// numbers or strings in the Unifi API
|
|
||||||
type FlexInt struct {
|
|
||||||
Value int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this FlexInt) UnmarshalJSON(b []byte) error {
|
|
||||||
var unk interface{}
|
|
||||||
err := json.Unmarshal(b, &unk)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
switch i := unk.(type) {
|
|
||||||
case float64:
|
|
||||||
this.Value = int(i)
|
|
||||||
return nil
|
|
||||||
case string:
|
|
||||||
this.Value, err = strconv.Atoi(i)
|
|
||||||
return err
|
|
||||||
default:
|
|
||||||
return errors.New("Cannot unmarshal to FlexInt")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// UAP is a Unifi Access Point.
|
// UAP is a Unifi Access Point.
|
||||||
type UAP struct {
|
type UAP struct {
|
||||||
/* This was auto generated and then slowly edited by hand
|
/* This was auto generated and then slowly edited by hand
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,11 @@ package unidev
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
influx "github.com/influxdata/influxdb/client/v2"
|
influx "github.com/influxdata/influxdb/client/v2"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
@ -30,8 +32,28 @@ type AuthedReq struct {
|
||||||
baseURL string
|
baseURL string
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringInt is used to unmarshal quoted integers in JSON responses.
|
// FlexInt provides a container and unmarshalling for fields that may be
|
||||||
type StringInt int
|
// numbers or strings in the Unifi API
|
||||||
|
type FlexInt int
|
||||||
|
|
||||||
|
// UnmarshalJSON converts a string or number to an integer.
|
||||||
|
func (value *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)
|
||||||
|
return nil
|
||||||
|
case string:
|
||||||
|
j, err := strconv.Atoi(i)
|
||||||
|
*value = FlexInt(j)
|
||||||
|
return err
|
||||||
|
default:
|
||||||
|
return errors.New("Cannot unmarshal to FlexInt")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// AuthController creates a http.Client with authenticated cookies.
|
// AuthController creates a http.Client with authenticated cookies.
|
||||||
// Used to make additional, authenticated requests to the APIs.
|
// Used to make additional, authenticated requests to the APIs.
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
package unidev
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFlexInt(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
a := assert.New(t)
|
||||||
|
five := []byte(`{"channel": "5"}`)
|
||||||
|
seven := []byte(`{"channel": 7}`)
|
||||||
|
type reply struct {
|
||||||
|
Channel FlexInt `json:"channel"`
|
||||||
|
}
|
||||||
|
var r reply
|
||||||
|
a.Nil(json.Unmarshal(five, &r))
|
||||||
|
a.EqualValues(FlexInt(5), r.Channel)
|
||||||
|
a.Nil(json.Unmarshal(seven, &r))
|
||||||
|
a.EqualValues(FlexInt(7), r.Channel)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue