From 368729d88f500b85234a099d2558d600a360c01f Mon Sep 17 00:00:00 2001 From: Wlad Meixner Date: Thu, 14 May 2020 22:16:54 +0200 Subject: [PATCH] fix: nil pointer issues --- bridge.go | 3 --- go.mod | 8 ++++++++ go.sum | 16 ++++++++++++++++ groups.go | 4 ++-- lights.go | 3 ++- main.go | 27 --------------------------- state.go | 7 +++++-- 7 files changed, 33 insertions(+), 35 deletions(-) create mode 100644 go.mod create mode 100644 go.sum delete mode 100644 main.go diff --git a/bridge.go b/bridge.go index ce3ae85..efa53f1 100644 --- a/bridge.go +++ b/bridge.go @@ -71,7 +71,6 @@ func (b *Bridge) deleteFromBridge(endpoint string, payload interface{}) (*http.R return b.sendToBridge(endpoint, http.MethodDelete, payload) } - func (b *Bridge) sendToBridge(endpoint string, method string, payload interface{}) (*http.Response, error) { data, errMarhshal := json.Marshal(payload) if errMarhshal != nil { @@ -109,7 +108,6 @@ func (b *Bridge) getRawResponse(endpoint string) ([]byte, error) { func (b *Bridge) getFromBridge(endpoint string) (*http.Response, error) { uri := b.getBridgeAPIURI() + endpoint - req, err := http.NewRequest("GET", uri, nil) if err != nil { return nil, err @@ -124,7 +122,6 @@ func (b *Bridge) getFromBridge(endpoint string) (*http.Response, error) { // check http responses if res.StatusCode != http.StatusOK { return nil, errors.New("Hue responded with error" + res.Status + fmt.Sprint(res.StatusCode)) - } return res, nil diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d7d8657 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/gosticks/go-hue-interface + +go 1.13 + +require ( + github.com/sergi/go-diff v1.1.0 + gopkg.in/yaml.v2 v2.3.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a8f50ce --- /dev/null +++ b/go.sum @@ -0,0 +1,16 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= +github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/groups.go b/groups.go index c1edddd..af970ca 100644 --- a/groups.go +++ b/groups.go @@ -266,7 +266,7 @@ func (b *Bridge) createGroup(group *Group) (string, error) { if len(result) == 1 { return result[0].Success.ID, nil - } else { - return "", fmt.Errorf("could not create group, bridge did not return new group id") } + return "", fmt.Errorf("could not create group, bridge did not return new group id") + } diff --git a/lights.go b/lights.go index c391fbf..9d944d9 100644 --- a/lights.go +++ b/lights.go @@ -111,12 +111,13 @@ func (b *Bridge) SetLightState(id string, state *LightState) (result *BridgeResp // GetLights returns all the hue lights func (b *Bridge) GetLights() (result map[string]*Light, err error) { + result = make(map[string]*Light) res, errCom := b.getFromBridge("/lights") if errCom != nil { return nil, errCom } // Unmarshal data - errDecode := json.NewDecoder(res.Body).Decode(result) + errDecode := json.NewDecoder(res.Body).Decode(&result) if errDecode != nil { return nil, errDecode } diff --git a/main.go b/main.go deleted file mode 100644 index c679a0b..0000000 --- a/main.go +++ /dev/null @@ -1,27 +0,0 @@ -package hue - -import ( - "fmt" -) - -const VERSION = "0.1.2" - -func main() { - fmt.Printf("go-hue-interface v. %s \n", VERSION) - - config := &Config{ - Username: "nX1ye7AMQoQswiiJdxyw-92-RNhIwicXiQRg7AtF", - BridgeAddr: "192.168.178.46", - BridgeAddrScheme: "http", - } - - bridge := NewBridge(config) - - fmt.Println("Created bridge ", bridge) - - // errCom := bridge.ToggleLight("2", false) - // if errCom != nil { - // fmt.Println("[ERROR]" + errCom.Error()) - // } - //fmt.Println(test) -} diff --git a/state.go b/state.go index 16ee5d1..1f3fe84 100644 --- a/state.go +++ b/state.go @@ -21,9 +21,12 @@ func (bs *BridgeState) String() string { } // GetState returns the current hue state -func (b *Bridge) GetState() (*BridgeState, error) { - state := &BridgeState{} +func (b *Bridge) GetState() (state *BridgeState, err error) { + state = &BridgeState{} res, err := b.getFromBridge("") + if err != nil { + return + } // Unmarshal data errDecode := json.NewDecoder(res.Body).Decode(state)