From 39c6f6801e45c538300d7e390ebbcd091ed25a2b Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Sat, 24 Aug 2019 01:32:12 -0700 Subject: [PATCH] Bug fix on FlexInt=nil/null, remove errors pkg, minor cleanup. --- core/unifi/Gopkg.lock | 25 ++++++++++++------------- core/unifi/Gopkg.toml | 35 ----------------------------------- core/unifi/ids.go | 3 +-- core/unifi/types.go | 9 ++++++--- core/unifi/unifi.go | 26 +++++++++++++------------- 5 files changed, 32 insertions(+), 66 deletions(-) diff --git a/core/unifi/Gopkg.lock b/core/unifi/Gopkg.lock index 0ee3eca3..8c4a3916 100644 --- a/core/unifi/Gopkg.lock +++ b/core/unifi/Gopkg.lock @@ -19,15 +19,7 @@ "v2", ] pruneopts = "UT" - revision = "8ff2fc3824fcb533795f9a2f233275f0bb18d6c5" - -[[projects]] - digest = "1:cf31692c14422fa27c83a05292eb5cbe0fb2775972e8f1f8446a71549bd8980b" - name = "github.com/pkg/errors" - packages = ["."] - pruneopts = "UT" - revision = "ba968bfe8b2f7e042a574c888954fccecfa385b4" - version = "v0.8.1" + revision = "fc22c7df067eefd070157f157893fbce961d6359" [[projects]] digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe" @@ -38,19 +30,26 @@ version = "v1.0.0" [[projects]] - digest = "1:972c2427413d41a1e06ca4897e8528e5a1622894050e2f527b38ddf0f343f759" + digest = "1:8548c309c65a85933a625be5e7d52b6ac927ca30c56869fae58123b8a77a75e1" name = "github.com/stretchr/testify" packages = ["assert"] pruneopts = "UT" - revision = "ffdc059bfe9ce6a4e144ba849dbedead332c6053" - version = "v1.3.0" + revision = "221dbe5ed46703ee255b1da0dec05086f5035f62" + version = "v1.4.0" + +[[projects]] + digest = "1:4d2e5a73dc1500038e504a8d78b986630e3626dc027bc030ba5c75da257cdb96" + name = "gopkg.in/yaml.v2" + packages = ["."] + pruneopts = "UT" + revision = "51d6538a90f86fe93ac480b35f37b2be17fef232" + version = "v2.2.2" [solve-meta] analyzer-name = "dep" analyzer-version = 1 input-imports = [ "github.com/influxdata/influxdb1-client/v2", - "github.com/pkg/errors", "github.com/stretchr/testify/assert", ] solver-name = "gps-cdcl" diff --git a/core/unifi/Gopkg.toml b/core/unifi/Gopkg.toml index b38356cd..ab0e6c81 100644 --- a/core/unifi/Gopkg.toml +++ b/core/unifi/Gopkg.toml @@ -1,42 +1,7 @@ -# Gopkg.toml example -# -# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html -# for detailed Gopkg.toml documentation. -# -# required = ["github.com/user/thing/cmd/thing"] -# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] -# -# [[constraint]] -# name = "github.com/user/project" -# version = "1.0.0" -# -# [[constraint]] -# name = "github.com/user/project2" -# branch = "dev" -# source = "github.com/myfork/project2" -# -# [[override]] -# name = "github.com/x/y" -# version = "2.4.0" -# -# [prune] -# non-go = false -# go-tests = true -# unused-packages = true - - [[constraint]] branch = "master" name = "github.com/influxdata/influxdb1-client" -[[constraint]] - name = "github.com/pkg/errors" - version = "0.8.1" - -[[constraint]] - name = "github.com/stretchr/testify" - version = "1.3.0" - [prune] go-tests = true unused-packages = true diff --git a/core/unifi/ids.go b/core/unifi/ids.go index 634644a6..82dfc5b2 100644 --- a/core/unifi/ids.go +++ b/core/unifi/ids.go @@ -8,7 +8,6 @@ import ( "time" influx "github.com/influxdata/influxdb1-client/v2" - "github.com/pkg/errors" ) // IDSList contains a list that contains all of the IDS Events on a controller. @@ -124,7 +123,7 @@ func (u *Unifi) GetSiteIDS(site *Site, from, to time.Time) ([]*IDS, error) { return nil, err } if resp.StatusCode != http.StatusOK { - return nil, errors.Errorf("invalid status code from server %s", resp.Status) + return nil, fmt.Errorf("invalid status code from server %s", resp.Status) } if err := json.Unmarshal(body, &response); err != nil { return nil, err diff --git a/core/unifi/types.go b/core/unifi/types.go index ea323538..40d6d637 100644 --- a/core/unifi/types.go +++ b/core/unifi/types.go @@ -2,11 +2,10 @@ package unifi import ( "encoding/json" + "fmt" "net/http" "strconv" "strings" - - "github.com/pkg/errors" ) // This is a list of unifi API paths. @@ -90,8 +89,12 @@ func (f *FlexInt) UnmarshalJSON(b []byte) error { f.Txt = i f.Val, _ = strconv.ParseFloat(i, 64) return nil + case nil: + f.Txt = "0" + f.Val = 0 + return nil default: - return errors.New("Cannot unmarshal to FlexInt") + return fmt.Errorf("cannot unmarshal to FlexInt: %s", b) } } diff --git a/core/unifi/unifi.go b/core/unifi/unifi.go index 3404c55c..0b376248 100644 --- a/core/unifi/unifi.go +++ b/core/unifi/unifi.go @@ -16,8 +16,6 @@ import ( "net/http" "net/http/cookiejar" "strings" - - "github.com/pkg/errors" ) // NewUnifi creates a http.Client with authenticated cookies. @@ -26,7 +24,7 @@ import ( func NewUnifi(user, pass, url string, verifySSL bool) (*Unifi, error) { jar, err := cookiejar.New(nil) if err != nil { - return nil, errors.Wrap(err, "cookiejar.New(nil)") + return nil, err } u := &Unifi{baseURL: strings.TrimRight(url, "/"), Client: &http.Client{ @@ -44,21 +42,24 @@ func (u *Unifi) getController(user, pass string) error { // magic login. req, err := u.UniReq(LoginPath, fmt.Sprintf(`{"username":"%s","password":"%s"}`, user, pass)) if err != nil { - return errors.Wrap(err, "UniReq(LoginPath, json)") + return err } resp, err := u.Do(req) if err != nil { - return errors.Wrap(err, "authReq.Do(req)") + return err } defer func() { _, _ = io.Copy(ioutil.Discard, resp.Body) // avoid leaking. _ = resp.Body.Close() }() if resp.StatusCode != http.StatusOK { - return errors.Errorf("authentication failed (user: %s): %s (status: %s)", + return fmt.Errorf("authentication failed (user: %s): %s (status: %s)", user, u.baseURL+LoginPath, resp.Status) } - return errors.Wrap(u.getServer(), "unable to get server version") + if err := u.getServer(); err != nil { + return fmt.Errorf("unable to get server version: %v", err) + } + return nil } // getServer sets the controller's version and UUID. @@ -139,8 +140,7 @@ func (u *Unifi) GetData(methodPath string, v interface{}) error { if err != nil { return err } - err = json.Unmarshal(body, v) - return errors.Wrapf(err, "json.Unmarshal(%s)", methodPath) + return json.Unmarshal(body, v) } // UniReq is a small helper function that adds an Accept header. @@ -164,21 +164,21 @@ func (u *Unifi) UniReq(apiPath string, params string) (req *http.Request, err er func (u *Unifi) GetJSON(apiPath string) ([]byte, error) { req, err := u.UniReq(apiPath, "") if err != nil { - return []byte{}, errors.Wrapf(err, "c.UniReq(%s)", apiPath) + return []byte{}, err } resp, err := u.Do(req) if err != nil { - return []byte{}, errors.Wrapf(err, "c.Do(%s)", apiPath) + return []byte{}, err } defer func() { _ = resp.Body.Close() }() body, err := ioutil.ReadAll(resp.Body) if err != nil { - return body, errors.Wrapf(err, "ioutil.ReadAll(%s)", apiPath) + return body, err } if resp.StatusCode != http.StatusOK { - err = errors.Errorf("invalid status code from server %s", resp.Status) + err = fmt.Errorf("invalid status code from server %s", resp.Status) } return body, err }