Skip to content

Commit

Permalink
chore: fix ineffective assigns, enable ineffassign linter
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeWinikates committed Sep 8, 2023
1 parent 3126796 commit 94e2936
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ linters:
- gofmt
- gci
# - revive
# - ineffassign
- ineffassign
# - staticcheck
issues:
exclude-rules:
Expand Down
16 changes: 1 addition & 15 deletions internal/auth/csp/api_token_client.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package csp

import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -36,16 +33,5 @@ func (c *APITokenClient) GetAccessToken() (*AuthorizeResponse, error) {

defer resp.Body.Close()

if resp.StatusCode > 399 {
return nil, fmt.Errorf("authentication failed: %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
var cspResponse AuthorizeResponse
err = json.Unmarshal(body, &cspResponse)

if err != nil {
return nil, err
}
return &cspResponse, nil
return parseAuthorizeResponse(resp)
}
18 changes: 1 addition & 17 deletions internal/auth/csp/client_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package csp

import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
Expand All @@ -31,7 +28,6 @@ func (c *ClientCredentialsClient) GetAccessToken() (*AuthorizeResponse, error) {
}
requestBody := values.Encode()
req, err := http.NewRequest("POST", c.BaseURL+oauthPath, strings.NewReader(requestBody))

if err != nil {
return nil, err
}
Expand All @@ -40,23 +36,11 @@ func (c *ClientCredentialsClient) GetAccessToken() (*AuthorizeResponse, error) {
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

resp, err := client.Do(req)

if err != nil {
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode > 399 {
return nil, fmt.Errorf("authentication failed: %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
var cspResponse AuthorizeResponse
err = json.Unmarshal(body, &cspResponse)

if err != nil {
return nil, err
}
return &cspResponse, nil
return parseAuthorizeResponse(resp)
}
27 changes: 27 additions & 0 deletions internal/auth/csp/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package csp

import (
"encoding/json"
"fmt"
"io"
"net/http"
)

func parseAuthorizeResponse(resp *http.Response) (*AuthorizeResponse, error) {
if resp.StatusCode > 399 {
return nil, fmt.Errorf("authentication failed: %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var cspResponse AuthorizeResponse
err = json.Unmarshal(body, &cspResponse)
if err != nil {
return nil, err
}

return &cspResponse, nil
}
6 changes: 3 additions & 3 deletions internal/histogram/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ func TestHistogramLine(t *testing.T) {
1533529977, "test_source", map[string]string{"env": "test"}, "")
expected = "!M 1533529977 #20 30 \"request.latency\" source=\"test_source\" \"env\"=\"test\"\n" +
"!H 1533529977 #20 30 \"request.latency\" source=\"test_source\" \"env\"=\"test\"\n"
if len(line) != len(expected) {
t.Errorf("lines don't match. expected: %s, actual: %s", expected, line)
}

assert.Nil(t, err)
assert.Equal(t, expected, line)
}

func makeCentroids() []histogram.Centroid {
Expand Down
2 changes: 2 additions & 0 deletions senders/real_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestSendDirect(t *testing.T) {
directServer := startTestServer(false)
defer directServer.Close()
updatedUrl, err := url.Parse(directServer.URL)
assert.NoError(t, err)
updatedUrl.User = url.User(token)
wf, err := NewSender(updatedUrl.String())

Expand All @@ -36,6 +37,7 @@ func TestSendDirectWithTags(t *testing.T) {
defer directServer.Close()

updatedUrl, err := url.Parse(directServer.URL)
assert.NoError(t, err)
updatedUrl.User = url.User(token)
tags := map[string]string{"foo": "bar"}
wf, err := NewSender(updatedUrl.String(), SDKMetricsTags(tags))
Expand Down

0 comments on commit 94e2936

Please sign in to comment.