Skip to content

Commit

Permalink
all(style) : fix linter issues for errcheck, ineffassign, unused and …
Browse files Browse the repository at this point in the history
…fix errors (hybridgroup#950)
  • Loading branch information
gen2thomas committed Jul 6, 2023
1 parent d459fc0 commit 712a213
Show file tree
Hide file tree
Showing 167 changed files with 2,593 additions and 2,347 deletions.
9 changes: 0 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ linters:
# Enable specific linter
# https://golangci-lint.run/usage/linters/#enabled-by-default
# note: typecheck can not be disabled, it is used to check code compilation
#
# TODO: this default linters needs to be disabled to run successfully, we have to fix
# all issues step by step to enable at least the default linters
disable:
- errcheck
- ineffassign
#- staticcheck
- unused

enable:
- nolintlint

Expand Down
26 changes: 19 additions & 7 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ func NewAPI(m *gobot.Master) *API {

go func() {
if a.Cert != "" && a.Key != "" {
http.ListenAndServeTLS(a.Host+":"+a.Port, a.Cert, a.Key, nil)
if err := http.ListenAndServeTLS(a.Host+":"+a.Port, a.Cert, a.Key, nil); err != nil {
panic(err)
}
} else {
log.Println("WARNING: API using insecure connection. " +
"We recommend using an SSL certificate with Gobot.")
http.ListenAndServe(a.Host+":"+a.Port, nil)
if err := http.ListenAndServe(a.Host+":"+a.Port, nil); err != nil {
panic(err)
}
}
}()
},
Expand Down Expand Up @@ -177,7 +181,9 @@ func (a *API) robeaux(res http.ResponseWriter, req *http.Request) {
} else if t[len(t)-1] == "html" {
res.Header().Set("Content-Type", "text/html; charset=utf-8")
}
res.Write(buf)
if _, err := res.Write(buf); err != nil {
panic(err)
}
}

// mcp returns MCP route handler.
Expand Down Expand Up @@ -261,10 +267,12 @@ func (a *API) robotDeviceEvent(res http.ResponseWriter, req *http.Request) {
if event := a.master.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")).(gobot.Eventer).
Event(req.URL.Query().Get(":event")); len(event) > 0 {
device.(gobot.Eventer).On(event, func(data interface{}) {
if err := device.(gobot.Eventer).On(event, func(data interface{}) {
d, _ := json.Marshal(data)
dataChan <- string(d)
})
}); err != nil {
panic(err)
}

for {
select {
Expand Down Expand Up @@ -363,7 +371,9 @@ func (a *API) executeCommand(f func(map[string]interface{}) interface{},
) {

body := make(map[string]interface{})
json.NewDecoder(req.Body).Decode(&body)
if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
panic(err)
}

if f != nil {
a.writeJSON(map[string]interface{}{"result": f(body)}, res)
Expand All @@ -376,7 +386,9 @@ func (a *API) executeCommand(f func(map[string]interface{}) interface{},
func (a *API) writeJSON(j interface{}, res http.ResponseWriter) {
data, _ := json.Marshal(j)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
if _, err := res.Write(data); err != nil {
panic(err)
}
}

// Debug add handler to api that prints each request
Expand Down
52 changes: 26 additions & 26 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestMcp(t *testing.T) {
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Refute(t, body["MCP"].(map[string]interface{})["robots"], nil)
gobottest.Refute(t, body["MCP"].(map[string]interface{})["commands"], nil)
}
Expand All @@ -103,7 +103,7 @@ func TestMcpCommands(t *testing.T) {
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["commands"], []interface{}{"TestFunction"})
}

Expand All @@ -120,7 +120,7 @@ func TestExecuteMcpCommand(t *testing.T) {
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body.(map[string]interface{})["result"], "hey Beep Boop")

// unknown command
Expand All @@ -132,7 +132,7 @@ func TestExecuteMcpCommand(t *testing.T) {
response = httptest.NewRecorder()
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body.(map[string]interface{})["error"], "Unknown Command")
}

Expand All @@ -143,7 +143,7 @@ func TestRobots(t *testing.T) {
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, len(body["robots"].([]interface{})), 3)
}

Expand All @@ -156,14 +156,14 @@ func TestRobot(t *testing.T) {
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["robot"].(map[string]interface{})["name"].(string), "Robot1")

// unknown robot
request, _ = http.NewRequest("GET", "/api/robots/UnknownRobot1", nil)
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
}

Expand All @@ -176,14 +176,14 @@ func TestRobotDevices(t *testing.T) {
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, len(body["devices"].([]interface{})), 3)

// unknown robot
request, _ = http.NewRequest("GET", "/api/robots/UnknownRobot1/devices", nil)
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
}

Expand All @@ -196,14 +196,14 @@ func TestRobotCommands(t *testing.T) {
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["commands"], []interface{}{"robotTestFunction"})

// unknown robot
request, _ = http.NewRequest("GET", "/api/robots/UnknownRobot1/commands", nil)
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
}

Expand All @@ -219,7 +219,7 @@ func TestExecuteRobotCommand(t *testing.T) {
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body.(map[string]interface{})["result"], "hey Robot1, Beep Boop")

// unknown command
Expand All @@ -231,7 +231,7 @@ func TestExecuteRobotCommand(t *testing.T) {
response = httptest.NewRecorder()
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body.(map[string]interface{})["error"], "Unknown Command")

// uknown robot
Expand All @@ -242,7 +242,7 @@ func TestExecuteRobotCommand(t *testing.T) {
request.Header.Add("Content-Type", "application/json")
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body.(map[string]interface{})["error"], "No Robot found with the name UnknownRobot1")
}

Expand All @@ -258,15 +258,15 @@ func TestRobotDevice(t *testing.T) {
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["device"].(map[string]interface{})["name"].(string), "Device1")

// unknown device
request, _ = http.NewRequest("GET",
"/api/robots/Robot1/devices/UnknownDevice1", nil)
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["error"], "No Device found with the name UnknownDevice1")
}

Expand All @@ -282,7 +282,7 @@ func TestRobotDeviceCommands(t *testing.T) {
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, len(body["commands"].([]interface{})), 2)

// unknown device
Expand All @@ -291,7 +291,7 @@ func TestRobotDeviceCommands(t *testing.T) {
nil,
)
a.ServeHTTP(response, request)
json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["error"], "No Device found with the name UnknownDevice1")
}

Expand All @@ -308,7 +308,7 @@ func TestExecuteRobotDeviceCommand(t *testing.T) {
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body.(map[string]interface{})["result"].(string), "hello human")

// unknown command
Expand All @@ -320,7 +320,7 @@ func TestExecuteRobotDeviceCommand(t *testing.T) {
response = httptest.NewRecorder()
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body.(map[string]interface{})["error"], "Unknown Command")

// unknown device
Expand All @@ -331,7 +331,7 @@ func TestExecuteRobotDeviceCommand(t *testing.T) {
request.Header.Add("Content-Type", "application/json")
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body.(map[string]interface{})["error"], "No Device found with the name UnknownDevice1")

}
Expand All @@ -345,14 +345,14 @@ func TestRobotConnections(t *testing.T) {
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, len(body["connections"].([]interface{})), 3)

// unknown robot
request, _ = http.NewRequest("GET", "/api/robots/UnknownRobot1/connections", nil)
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
}

Expand All @@ -368,7 +368,7 @@ func TestRobotConnection(t *testing.T) {
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["connection"].(map[string]interface{})["name"].(string), "Connection1")

// unknown connection
Expand All @@ -377,7 +377,7 @@ func TestRobotConnection(t *testing.T) {
nil,
)
a.ServeHTTP(response, request)
json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["error"], "No Connection found with the name UnknownConnection1")
}

Expand Down Expand Up @@ -426,7 +426,7 @@ func TestRobotDeviceEvent(t *testing.T) {
response, _ := http.Get(server.URL + eventsURL + "UnknownEvent")

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
_ = json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["error"], "No Event found with the name UnknownEvent")
}

Expand Down
10 changes: 5 additions & 5 deletions drivers/aio/analog_sensor_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ func TestAnalogSensorDriverStart(t *testing.T) {
d.SetScaler(func(input int) float64 { return float64(input * input) })

// expect data to be received
d.Once(d.Event(Data), func(data interface{}) {
_ = d.Once(d.Event(Data), func(data interface{}) {
gobottest.Assert(t, data.(int), 100)
sem <- true
})

d.Once(d.Event(Value), func(data interface{}) {
_ = d.Once(d.Event(Value), func(data interface{}) {
gobottest.Assert(t, data.(float64), 10000.0)
sem <- true
})
Expand All @@ -122,7 +122,7 @@ func TestAnalogSensorDriverStart(t *testing.T) {
}

// expect error to be received
d.Once(d.Event(Error), func(data interface{}) {
_ = d.Once(d.Event(Error), func(data interface{}) {
gobottest.Assert(t, data.(error).Error(), "read error")
sem <- true
})
Expand All @@ -140,11 +140,11 @@ func TestAnalogSensorDriverStart(t *testing.T) {
}

// send a halt message
d.Once(d.Event(Data), func(data interface{}) {
_ = d.Once(d.Event(Data), func(data interface{}) {
sem <- true
})

d.Once(d.Event(Value), func(data interface{}) {
_ = d.Once(d.Event(Value), func(data interface{}) {
sem <- true
})

Expand Down
Loading

0 comments on commit 712a213

Please sign in to comment.