fix: nil pointer issues

This commit is contained in:
Wlad Meixner 2020-05-14 22:16:54 +02:00
parent 5480599807
commit 368729d88f
7 changed files with 33 additions and 35 deletions

View File

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

8
go.mod Normal file
View File

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

16
go.sum Normal file
View File

@ -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=

View File

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

View File

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

27
main.go
View File

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

View File

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