Skip to content

Commit 2683807

Browse files
authored
DE-1133 Add linter and fix warnings (#123)
1 parent 974314f commit 2683807

13 files changed

+116
-37
lines changed

.github/workflows/main.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ jobs:
3636
- name: nilaway
3737
run: make nilaway
3838

39-
# TODO(vtopc):
40-
# - name: lint
41-
# run: make lint
39+
- name: lint
40+
run: make lint
4241

4342
- name: Test
4443
run: go test ./... -race -count=1

.golangci.yml

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
linters:
2+
# Please, do not use `enable-all`: it's deprecated and will be removed soon.
3+
# Inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint.
4+
# Full list of linters - https://golangci-lint.run/usage/linters
5+
disable-all: true
6+
enable:
7+
- typecheck
8+
- errcheck # Mandatory. Do not disable.
9+
- ineffassign # Mandatory. Do not disable.
10+
- staticcheck # Mandatory. Do not disable.
11+
- govet
12+
- gosimple
13+
- gosec
14+
- bodyclose # https://github.com/timakin/bodyclose
15+
- goimports
16+
- stylecheck
17+
- gocritic
18+
- gomodguard
19+
- nolintlint
20+
21+
# TODO:
22+
# - unused
23+
24+
# TODO(v5): enable
25+
# - noctx
26+
27+
linters-settings:
28+
errcheck:
29+
# List of functions to exclude from checking, where each entry is a single function to exclude.
30+
# See https://github.com/kisielk/errcheck#excluding-functions for details.
31+
exclude-functions:
32+
- (io.Closer).Close
33+
- (io.ReadCloser).Close
34+
35+
govet:
36+
enable-all: true
37+
disable:
38+
- shadow
39+
- fieldalignment
40+
41+
staticcheck:
42+
# SAxxxx checks in https://staticcheck.dev/docs/configuration/options/#checks
43+
# Example (to disable some checks): [ "all", "-SA1000", "-SA1001"]
44+
# Default: ["*"]
45+
# TODO(Go1.20+): enable SA1019
46+
checks: ["all", "-SA1019"]
47+
48+
stylecheck:
49+
# https://staticcheck.io/docs/options#checks
50+
# TODO(v5): enable all:
51+
checks: ["all", "-ST1003", "-ST1005"]
52+
53+
gomodguard:
54+
blocked:
55+
# List of blocked modules.
56+
modules:
57+
- github.com/pkg/errors:
58+
recommendations:
59+
- errors
60+
- github.com/mailgun/errors
61+
reason: "Deprecated"
62+
63+
issues:
64+
# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
65+
max-issues-per-linter: 0
66+
67+
# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
68+
max-same-issues: 50
69+
70+
run:
71+
# include test files or not, default is true
72+
tests: true
73+
74+
# Timeout for analysis, e.g. 30s, 5m.
75+
# Default: 1m
76+
timeout: 5m

Makefile

-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ GOLINT = $(GOPATH)/bin/golangci-lint
2323
$(GOLINT):
2424
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.61.0
2525

26-
.PHONY: lint-new
27-
lint-new: $(GOLINT)
28-
$(GOLINT) run -new-from-rev=master
29-
3026
.PHONY: lint
3127
lint: $(GOLINT)
3228
$(GOLINT) run

core.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func buildDataURL(baseURL string, info *DataRequest) string {
186186
if info.DataTypeID != 0 {
187187
DataTypeID := strconv.FormatInt(info.DataTypeID, 10)
188188
tokens = append(tokens, DataTypeID)
189-
} else if info.LastID == true {
189+
} else if info.LastID {
190190
tokens = append(tokens, "LAST")
191191
}
192192
return strings.Join(tokens, "/")

data_api.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,36 @@ import "strings"
55
// ListData issues a GET to list the specified data resource
66
// and stores the result in the value pointed to by res.
77
// Filters can be add via functional options.
8-
func (mj *Client) ListData(resource string, resp interface{}, options ...RequestOptions) (count, total int, err error) {
9-
url := buildDataURL(mj.apiBase, &DataRequest{SourceType: resource})
8+
func (c *Client) ListData(resource string, resp interface{}, options ...RequestOptions) (count, total int, err error) {
9+
url := buildDataURL(c.apiBase, &DataRequest{SourceType: resource})
1010
req, err := createRequest("GET", url, nil, nil, options...)
1111
if err != nil {
1212
return count, total, err
1313
}
1414

15-
return mj.httpClient.Send(req).Read(resp).Call()
15+
return c.httpClient.Send(req).Read(resp).Call()
1616
}
1717

1818
// GetData issues a GET to view a resource specifying an id
1919
// and stores the result in the value pointed to by res.
2020
// Filters can be add via functional options.
2121
// Without an specified SourceTypeID in MailjetDataRequest, it is the same as ListData.
22-
func (mj *Client) GetData(mdr *DataRequest, res interface{}, options ...RequestOptions) (err error) {
23-
url := buildDataURL(mj.apiBase, mdr)
22+
func (c *Client) GetData(mdr *DataRequest, res interface{}, options ...RequestOptions) (err error) {
23+
url := buildDataURL(c.apiBase, mdr)
2424
req, err := createRequest("GET", url, nil, nil, options...)
2525
if err != nil {
2626
return err
2727
}
2828

29-
_, _, err = mj.httpClient.Send(req).Read(res).Call()
29+
_, _, err = c.httpClient.Send(req).Read(res).Call()
3030
return err
3131
}
3232

3333
// PostData issues a POST to create a new data resource
3434
// and stores the result in the value pointed to by res.
3535
// Filters can be add via functional options.
36-
func (mj *Client) PostData(fmdr *FullDataRequest, res interface{}, options ...RequestOptions) (err error) {
37-
url := buildDataURL(mj.apiBase, fmdr.Info)
36+
func (c *Client) PostData(fmdr *FullDataRequest, res interface{}, options ...RequestOptions) (err error) {
37+
url := buildDataURL(c.apiBase, fmdr.Info)
3838
req, err := createRequest("POST", url, fmdr.Payload, nil, options...)
3939
if err != nil {
4040
return err
@@ -46,36 +46,36 @@ func (mj *Client) PostData(fmdr *FullDataRequest, res interface{}, options ...Re
4646
headers = map[string]string{"Content-Type": contentType}
4747
}
4848

49-
_, _, err = mj.httpClient.Send(req).With(headers).Read(res).Call()
49+
_, _, err = c.httpClient.Send(req).With(headers).Read(res).Call()
5050
return err
5151
}
5252

5353
// PutData is used to update a data resource.
5454
// Fields to be updated must be specified by the string array onlyFields.
5555
// If onlyFields is nil, all fields except these with the tag read_only, are updated.
5656
// Filters can be add via functional options.
57-
func (mj *Client) PutData(fmr *FullDataRequest, onlyFields []string, options ...RequestOptions) (err error) {
58-
url := buildDataURL(mj.apiBase, fmr.Info)
57+
func (c *Client) PutData(fmr *FullDataRequest, onlyFields []string, options ...RequestOptions) (err error) {
58+
url := buildDataURL(c.apiBase, fmr.Info)
5959
req, err := createRequest("PUT", url, fmr.Payload, onlyFields, options...)
6060
if err != nil {
6161
return err
6262
}
6363

6464
headers := map[string]string{"Content-Type": "application/json"}
65-
_, _, err = mj.httpClient.Send(req).With(headers).Call()
65+
_, _, err = c.httpClient.Send(req).With(headers).Call()
6666

6767
return err
6868
}
6969

7070
// DeleteData is used to delete a data resource.
71-
func (mj *Client) DeleteData(mdr *DataRequest, options ...RequestOptions) (err error) {
72-
url := buildDataURL(mj.apiBase, mdr)
71+
func (c *Client) DeleteData(mdr *DataRequest, options ...RequestOptions) (err error) {
72+
url := buildDataURL(c.apiBase, mdr)
7373
req, err := createRequest("DELETE", url, nil, nil, options...)
7474
if err != nil {
7575
return err
7676
}
7777

78-
_, _, err = mj.httpClient.Send(req).Call()
78+
_, _, err = c.httpClient.Send(req).Call()
7979

8080
return err
8181
}

example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"net/textproto"
66
"os"
77

8-
mailjet "github.com/mailjet/mailjet-apiv3-go/v4"
8+
"github.com/mailjet/mailjet-apiv3-go/v4"
99
"github.com/mailjet/mailjet-apiv3-go/v4/resources"
1010
)
1111

fixtures/fixtures.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ func New() *Fixtures {
3333
func (f *Fixtures) Read(v interface{}) error {
3434
for t, val := range f.data {
3535
if reflect.ValueOf(v).Type().String() == reflect.ValueOf(t).Type().String() {
36-
json.Unmarshal(val, v)
37-
return nil
36+
return json.Unmarshal(val, v)
3837
}
3938
}
4039
return errors.New("not found")

http_client_mock.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package mailjet
22

33
import (
44
"errors"
5+
"fmt"
6+
"log"
57
"net/http"
68

79
"github.com/mailjet/mailjet-apiv3-go/v4/fixtures"
@@ -30,7 +32,7 @@ func NewhttpClientMock(valid bool) *HTTPClientMock {
3032
validCreds: valid,
3133
fx: fixtures.New(),
3234
CallFunc: func() (int, int, error) {
33-
if valid == true {
35+
if valid {
3436
return 1, 1, nil
3537
}
3638
return 0, 0, errors.New("Unexpected error: Unexpected server response code: 401: EOF")
@@ -75,7 +77,11 @@ func (c *HTTPClientMock) With(headers map[string]string) HTTPClientInterface {
7577

7678
// Read allow you to bind the response received through the underlying http client
7779
func (c *HTTPClientMock) Read(response interface{}) HTTPClientInterface {
78-
c.fx.Read(response)
80+
err := c.fx.Read(response)
81+
if err != nil {
82+
log.Println(fmt.Errorf("c.fx.Read: %w", err))
83+
}
84+
7985
return c
8086
}
8187

mailjet_client.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ package mailjet
77
import (
88
"bytes"
99
"context"
10-
"fmt"
11-
1210
"encoding/json"
13-
11+
"fmt"
1412
"io"
1513
"log"
1614
"net/http"
@@ -123,7 +121,7 @@ func SetDebugOutput(w io.Writer) {
123121
// Sort applies the Sort filter to the request.
124122
func Sort(value string, order SortOrder) RequestOptions {
125123
if order == SortDesc {
126-
value = value + "+DESC"
124+
value += "+DESC"
127125
}
128126
return Filter("Sort", value)
129127
}

mailjet_client_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"testing"
1313
"time"
1414

15-
mailjet "github.com/mailjet/mailjet-apiv3-go/v4"
15+
"github.com/mailjet/mailjet-apiv3-go/v4"
1616
"github.com/mailjet/mailjet-apiv3-go/v4/resources"
1717
)
1818

@@ -59,14 +59,15 @@ func handle(path, response string) {
5959
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
6060
w.Header().Set("Content-Type", "application/json")
6161
w.WriteHeader(http.StatusOK)
62-
fmt.Fprintf(w, response)
62+
fmt.Fprint(w, response)
6363
})
6464
}
6565

6666
func randSeq(n int) string {
6767
rand.Seed(time.Now().UnixNano())
6868
b := make([]rune, n)
6969
for i := range b {
70+
//nolint:gosec // G404 crypto random is not required here
7071
b[i] = letters[rand.Intn(len(letters))]
7172
}
7273
return string(b)
@@ -588,6 +589,9 @@ func TestSendMailV31(t *testing.T) {
588589

589590
for _, test := range tests {
590591
t.Run(test.name, func(t *testing.T) {
592+
// TODO(Go1.22+): remove:
593+
messages := test.messages // https://go.dev/wiki/CommonMistakes
594+
591595
httpClientMocked := mailjet.NewhttpClientMock(true)
592596
httpClientMocked.SendMailV31Func = func(req *http.Request) (*http.Response, error) {
593597
if req.Header.Get("Content-Type") != "application/json" {
@@ -619,7 +623,7 @@ func TestSendMailV31(t *testing.T) {
619623

620624
m := mailjet.NewClient(httpClientMocked, mailjet.NewSMTPClientMock(true))
621625

622-
res, err := m.SendMailV31(&test.messages)
626+
res, err := m.SendMailV31(&messages)
623627
if !reflect.DeepEqual(err, test.wantErr) {
624628
t.Fatalf("Wanted error: %+v, got: %+v", err, test.wantErr)
625629
}

resources/resources.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ type MJMLContent struct {
10331033
type Toplinkclicked struct {
10341034
ClickedCount int64 `mailjet:"read_only"`
10351035
ID int64 `mailjet:"read_only"`
1036-
LinkID int64 `json:"LinkId"mailjet:"read_only"`
1036+
LinkID int64 `json:"LinkId" mailjet:"read_only"`
10371037
URL string `json:"Url" mailjet:"read_only"`
10381038
}
10391039

smtp_client_mock.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func NewSMTPClientMock(valid bool) *SMTPClientMock {
1818

1919
// SendMail wraps smtp.SendMail
2020
func (s SMTPClientMock) SendMail(from string, to []string, msg []byte) error {
21-
if s.valid == true {
21+
if s.valid {
2222
return nil
2323
}
2424
return errors.New("smtp send error")

tests/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func randSeq(n int) string {
1919
rand.Seed(time.Now().UnixNano())
2020
b := make([]rune, n)
2121
for i := range b {
22+
//nolint:gosec // G404 crypto random is not required here
2223
b[i] = letters[rand.Intn(len(letters))]
2324
}
2425
return string(b)

0 commit comments

Comments
 (0)