Skip to content

Commit

Permalink
DE-1356 Add revive linter (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtopc authored Dec 4, 2024
1 parent 8fd2c2b commit b3b8931
Show file tree
Hide file tree
Showing 23 changed files with 528 additions and 107 deletions.
419 changes: 417 additions & 2 deletions .golangci.yml

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion attachments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ func findAcceptedMessage(mg mailgun.Mailgun, id string) (*events.Accepted, error
for it.Next(context.Background(), &page) {
for _, event := range page {
if event.GetName() == events.EventAccepted && event.GetID() == id {
return event.(*events.Accepted), nil
e, ok := event.(*events.Accepted)
if !ok {
return nil, fmt.Errorf("unexpected event type: %T", event)
}

return e, nil
}
}
}
Expand Down
21 changes: 10 additions & 11 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ func (ei *EventIterator) Err() error {
// Next retrieves the next page of events from the api. Returns false when there
// no more pages to retrieve or if there was an error. Use `.Err()` to retrieve
// the error
func (ei *EventIterator) Next(ctx context.Context, events *[]Event) bool {
func (ei *EventIterator) Next(ctx context.Context, ee *[]Event) bool {
if ei.err != nil {
return false
}
ei.err = ei.fetch(ctx, ei.Paging.Next)
if ei.err != nil {
return false
}
*events, ei.err = ParseEvents(ei.Items)
*ee, ei.err = ParseEvents(ei.Items)
if ei.err != nil {
return false
}
Expand All @@ -111,38 +111,38 @@ func (ei *EventIterator) Next(ctx context.Context, events *[]Event) bool {
// First retrieves the first page of events from the api. Returns false if there
// was an error. It also sets the iterator object to the first page.
// Use `.Err()` to retrieve the error.
func (ei *EventIterator) First(ctx context.Context, events *[]Event) bool {
func (ei *EventIterator) First(ctx context.Context, ee *[]Event) bool {
if ei.err != nil {
return false
}
ei.err = ei.fetch(ctx, ei.Paging.First)
if ei.err != nil {
return false
}
*events, ei.err = ParseEvents(ei.Items)
*ee, ei.err = ParseEvents(ei.Items)
return true
}

// Last retrieves the last page of events from the api.
// Calling Last() is invalid unless you first call First() or Next()
// Returns false if there was an error. It also sets the iterator object
// to the last page. Use `.Err()` to retrieve the error.
func (ei *EventIterator) Last(ctx context.Context, events *[]Event) bool {
func (ei *EventIterator) Last(ctx context.Context, ee *[]Event) bool {
if ei.err != nil {
return false
}
ei.err = ei.fetch(ctx, ei.Paging.Last)
if ei.err != nil {
return false
}
*events, ei.err = ParseEvents(ei.Items)
*ee, ei.err = ParseEvents(ei.Items)
return true
}

// Previous retrieves the previous page of events from the api. Returns false when there
// no more pages to retrieve or if there was an error. Use `.Err()` to retrieve
// the error if any
func (ei *EventIterator) Previous(ctx context.Context, events *[]Event) bool {
func (ei *EventIterator) Previous(ctx context.Context, ee *[]Event) bool {
if ei.err != nil {
return false
}
Expand All @@ -153,7 +153,7 @@ func (ei *EventIterator) Previous(ctx context.Context, events *[]Event) bool {
if ei.err != nil {
return false
}
*events, ei.err = ParseEvents(ei.Items)
*ee, ei.err = ParseEvents(ei.Items)

return len(ei.Items) != 0
}
Expand Down Expand Up @@ -234,7 +234,7 @@ func (ep *EventPoller) Err() error {
return ep.err
}

func (ep *EventPoller) Poll(ctx context.Context, events *[]Event) bool {
func (ep *EventPoller) Poll(ctx context.Context, ee *[]Event) bool {
var currentPage string
var results []Event

Expand Down Expand Up @@ -266,7 +266,7 @@ func (ep *EventPoller) Poll(ctx context.Context, events *[]Event) bool {

// If we have events to return
if len(results) != 0 {
*events = results
*ee = results
return true
}

Expand All @@ -284,7 +284,6 @@ func (ep *EventPoller) Poll(ctx context.Context, events *[]Event) bool {
case <-timer.C:
}
}

}

// Given time.Time{} return a float64 as given in mailgun event timestamps
Expand Down
1 change: 0 additions & 1 deletion examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ func ExampleMailgunImpl_VerifyWebhookSignature() {
}

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

var payload mailgun.WebhookPayload
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
fmt.Printf("decode JSON error: %s", err)
Expand Down
6 changes: 3 additions & 3 deletions exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (mg *MailgunImpl) GetExportLink(ctx context.Context, id string) (string, er
c := mg.Client()

// Ensure the client doesn't attempt to retry
c.CheckRedirect = func(req *http.Request, via []*http.Request) error {
c.CheckRedirect = func(_ *http.Request, _ []*http.Request) error {
return errors.New("redirect")
}

Expand All @@ -84,10 +84,10 @@ func (mg *MailgunImpl) GetExportLink(ctx context.Context, id string) (string, er
if Debug {
if CaptureCurlOutput {
r.mu.Lock()
r.capturedCurlOutput = r.curlString(req, nil)
r.capturedCurlOutput = curlString(req, nil)
r.mu.Unlock()
} else {
fmt.Println(r.curlString(req, nil))
fmt.Println(curlString(req, nil))
}
}

Expand Down
65 changes: 32 additions & 33 deletions httphelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ type jsonEncodedPayload struct {
payload interface{}
}

func newHTTPRequest(url string) *httpRequest {
return &httpRequest{URL: url, Client: http.DefaultClient}
func newHTTPRequest(uri string) *httpRequest {
return &httpRequest{URL: uri, Client: http.DefaultClient}
}

func (r *httpRequest) addParameter(name, value string) {
Expand Down Expand Up @@ -109,11 +109,11 @@ func (j *jsonEncodedPayload) getPayloadBuffer() (*bytes.Buffer, error) {
return bytes.NewBuffer(b), nil
}

func (j *jsonEncodedPayload) getContentType() (string, error) {
func (*jsonEncodedPayload) getContentType() (string, error) {
return "application/json", nil
}

func (j *jsonEncodedPayload) getValues() []keyValuePair {
func (*jsonEncodedPayload) getValues() []keyValuePair {
return nil
}

Expand All @@ -134,7 +134,7 @@ func (f *urlEncodedPayload) getPayloadBuffer() (*bytes.Buffer, error) {
return bytes.NewBufferString(data.Encode()), nil
}

func (f *urlEncodedPayload) getContentType() (string, error) {
func (*urlEncodedPayload) getContentType() (string, error) {
return "application/x-www-form-urlencoded", nil
}

Expand Down Expand Up @@ -176,12 +176,13 @@ func (f *FormDataPayload) getPayloadBuffer() (*bytes.Buffer, error) {
defer writer.Close()

for _, keyVal := range f.Values {
if tmp, err := writer.CreateFormField(keyVal.key); err == nil {
_, err := tmp.Write([]byte(keyVal.value))
if err != nil {
return nil, err
}
} else {
tmp, err := writer.CreateFormField(keyVal.key)
if err != nil {
return nil, err
}

_, err = tmp.Write([]byte(keyVal.value))
if err != nil {
return nil, err
}
}
Expand All @@ -197,7 +198,7 @@ func (f *FormDataPayload) getPayloadBuffer() (*bytes.Buffer, error) {
return nil, err
}

// TODO(DE-1139): defer in a loop:
// TODO(DE-1373): defer in a loop:
defer fp.Close()

_, err = io.Copy(tmp, fp)
Expand All @@ -207,28 +208,29 @@ func (f *FormDataPayload) getPayloadBuffer() (*bytes.Buffer, error) {
}

for _, file := range f.ReadClosers {
if tmp, err := writer.CreateFormFile(file.key, file.name); err == nil {
// TODO(DE-1139): defer in a loop:
defer file.value.Close()
tmp, err := writer.CreateFormFile(file.key, file.name)
if err != nil {
return nil, err
}

_, err := io.Copy(tmp, file.value)
if err != nil {
return nil, err
}
} else {
// TODO(DE-1373): defer in a loop:
defer file.value.Close()

_, err = io.Copy(tmp, file.value)
if err != nil {
return nil, err
}
}

for _, buff := range f.Buffers {
if tmp, err := writer.CreateFormFile(buff.key, buff.name); err == nil {
r := bytes.NewReader(buff.value)
tmp, err := writer.CreateFormFile(buff.key, buff.name)
if err != nil {
return nil, err
}

_, err := io.Copy(tmp, r)
if err != nil {
return nil, err
}
} else {
r := bytes.NewReader(buff.value)
_, err = io.Copy(tmp, r)
if err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -327,10 +329,10 @@ func (r *httpRequest) makeRequest(ctx context.Context, method string, payload pa
if Debug {
if CaptureCurlOutput {
r.mu.Lock()
r.capturedCurlOutput = r.curlString(req, payload)
r.capturedCurlOutput = curlString(req, payload)
r.mu.Unlock()
} else {
fmt.Println(r.curlString(req, payload))
fmt.Println(curlString(req, payload))
}
}

Expand Down Expand Up @@ -383,8 +385,7 @@ func (r *httpRequest) generateUrlWithParameters() (string, error) {
return uri.String(), nil
}

func (r *httpRequest) curlString(req *http.Request, p payload) string {

func curlString(req *http.Request, p payload) string {
parts := []string{"curl", "-i", "-X", req.Method, req.URL.String()}
for key, value := range req.Header {
if key == "Authorization" {
Expand All @@ -399,8 +400,6 @@ func (r *httpRequest) curlString(req *http.Request, p payload) string {
parts = append(parts, fmt.Sprintf("-H \"Host: %s\"", req.Host))
}

// parts = append(parts, fmt.Sprintf(" --user '%s:%s'", r.BasicAuthUser, r.BasicAuthPassword))

if p != nil {
contentType, _ := p.getContentType()
if contentType == "application/json" {
Expand Down
2 changes: 1 addition & 1 deletion mailgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func (mg *MailgunImpl) SetAPIBase(address string) {

// AddOverrideHeader allows the user to specify additional headers that will be included in the HTTP request
// This is mostly useful for testing the Mailgun API hosted at a different endpoint.
func (mg *MailgunImpl) AddOverrideHeader(k string, v string) {
func (mg *MailgunImpl) AddOverrideHeader(k, v string) {
if mg.overrideHeaders == nil {
mg.overrideHeaders = make(map[string]string)
}
Expand Down
2 changes: 1 addition & 1 deletion mailgun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestInvalidBaseAPI(t *testing.T) {
}

func TestValidBaseAPI(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
var resp mailgun.DomainResponse
b, err := json.Marshal(resp)
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions members.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ var (
// This attribute may be used to filter the results returned by GetSubscribers().
// All, Subscribed, and Unsubscribed provides a convenient and readable syntax for specifying the scope of the search.
var (
All *bool = nil
Subscribed *bool = &yes
Unsubscribed *bool = &no
All *bool
Subscribed = &yes
Unsubscribed = &no
)

// A Member structure represents a member of the mailing list.
Expand Down
Loading

0 comments on commit b3b8931

Please sign in to comment.