Skip to content

Commit

Permalink
fix by linter (#97)
Browse files Browse the repository at this point in the history
* fix by linter

* update lint version

* update lint version
  • Loading branch information
RomaBilka authored Dec 8, 2022
1 parent 6580d22 commit fc74736
Show file tree
Hide file tree
Showing 28 changed files with 264 additions and 261 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.18.x
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.46
version: v1.50.0
19 changes: 18 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
run:
timeout: 3m
go: '1.18'
skip-dirs:
skip-files:
- "./*_test.go$"
linters:
enable:
- whitespace
- gocritic
- prealloc
- unparam
- usestdlibvars
- goimports
linters-settings:
forbidigo:
# Forbid the following identifiers (list of regexp).
# Default: ["^(fmt\\.Print(|f|ln)|print|println)$"]
forbid:
- ^print.*$
- 'fmt\.Print.*'
exclude_godoc_examples: false
exclude_godoc_examples: false
gocritic:
enabled-tags:
- diagnostic
- style
- opinionated
- experimental
2 changes: 1 addition & 1 deletion api/lambda/handlers/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestHandleLambdaEvent(t *testing.T) {
tracker := &parcelTrackerMock{}
tc.setupTrackerMock(tracker)

req := events.APIGatewayProxyRequest{Body: fmt.Sprintf(`{"track_id":["%s"]}`, tc.trackId)}
req := events.APIGatewayProxyRequest{Body: fmt.Sprintf(`{"track_id":[%q]}`, tc.trackId)}
gotResp, gotErr := Tracking(tracker, 10)(context.Background(), req)

assert.Equal(t, tc.expResp, gotResp)
Expand Down
6 changes: 3 additions & 3 deletions api/rest/handlers/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package handlers
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestHandleRest(t *testing.T) {
tracker := &parcelTrackerMock{}
tc.setupTrackerMock(tracker)

data := strings.NewReader(fmt.Sprintf(`{"track_id":["%s"]}`, tc.testId))
data := strings.NewReader(fmt.Sprintf(`{"track_id":[%q]}`, tc.testId))

req, err := http.NewRequest(tc.method, "", data)
assert.NoError(t, err)
Expand All @@ -87,7 +87,7 @@ func TestHandleRest(t *testing.T) {
res := rec.Result()
defer res.Body.Close()

gotResp, err := ioutil.ReadAll(res.Body)
gotResp, err := io.ReadAll(res.Body)

assert.NoError(t, err)
assert.Equal(t, tc.expResp, string(gotResp))
Expand Down
2 changes: 1 addition & 1 deletion cmd/rest-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {

g, gCtx := errgroup.WithContext(ctx)
g.Go(func() error {
fmt.Println("Server is listening...")
fmt.Print("Server is listening...")
return httpServer.ListenAndServe()
})
g.Go(func() error {
Expand Down
9 changes: 2 additions & 7 deletions logic/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,9 @@ func (p ParcelsTracker) TrackParcels(_ context.Context, parcelIds []string) (map
for {
select {
case err := <-chanErr:
{
return nil, err
}
return nil, err
case idsToCarriers := <-chanIdsToCarriers:
{
go track(idsToCarriers, chanParcels, chanErr)
}
go track(idsToCarriers, chanParcels, chanErr)
case parcels := <-chanParcels:
return parcels, nil
}
Expand All @@ -87,7 +83,6 @@ func (p ParcelsTracker) matchParcelIdsToCarriers(parcelIds []string, chanIdsToCa
defer mu.Unlock()
idsToCarriers[carrier] = append(idsToCarriers[carrier], parcelId)
}(parcelId)

}

wg.Wait()
Expand Down
4 changes: 2 additions & 2 deletions pkg/determine-delivery/carriers/dhl/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package dhl

import (
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -43,7 +43,7 @@ func TestApi_TrackByTrackingNumber(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(testCase.status)

b, err := ioutil.ReadFile(testCase.file)
b, err := os.ReadFile(testCase.file)
if err != nil {
t.Fatal(err)
}
Expand Down
56 changes: 28 additions & 28 deletions pkg/determine-delivery/carriers/dhl/carrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,53 @@ import (
)

var patterns = map[string]*regexp.Regexp{
//It starts with 000
// It starts with 000
"start000": regexp.MustCompile(`^000`),

//It starts with JVGL
// It starts with JVGL
"startJVGL": regexp.MustCompile(`(?i)^JVGL`),

//It starts with GM
// It starts with GM
"startGM": regexp.MustCompile(`(?i)^GM`),

//It starts with LX
// It starts with LX
"startLX": regexp.MustCompile(`(?i)^LX`),

//It starts with RX
// It starts with RX
"startRX": regexp.MustCompile(`(?i)^RX`),

//It starts with 3S
// It starts with 3S
"start3S": regexp.MustCompile(`(?i)^3S`),

//It starts with JJD
// It starts with JJD
"startJJD": regexp.MustCompile(`(?i)^JJD`),

//Starts with 1 number, followed by 2 letters and 4 to 6 numbers
//Example: 1AB12345
"pattern1": regexp.MustCompile(`(?i)^[\d]{1}[a-z]{2}[\d]{4,6}$`),
// Starts with 1 number, followed by 2 letters and 4 to 6 numbers
// Example: 1AB12345
"pattern1": regexp.MustCompile(`(?i)^\d[a-z]{2}\d{4,6}$`),

//Starts with 3 to 4 letters
//Example: ABC123456
"pattern2": regexp.MustCompile(`(?i)^[a-z]{3}[\d]{6}$`),
// Starts with 3 to 4 letters
// Example: ABC123456
"pattern2": regexp.MustCompile(`(?i)^[a-z]{3}\d{6}$`),

//Starts with 3-digit carrier code, followed by hyphen (-), followed by the 8-digit masterbill number.
//Example: 123-12345678
"pattern3": regexp.MustCompile(`^[\d]{3}-[\d]{8}$`),
// Starts with 3-digit carrier code, followed by hyphen (-), followed by the 8-digit masterbill number.
// Example: 123-12345678
"pattern3": regexp.MustCompile(`^\d{3}-\d{8}$`),

//Order Code: starts with 2 to 3 letters, followed by hyphen (-), 2 to 3 letters, hyphen (-) and 7 numbers
//Example: ABC-DE-1234567
"pattern4": regexp.MustCompile(`(?i)^[a-z]{2,3}-[a-z]{2,3}-[\d]{7}$`),
// Order Code: starts with 2 to 3 letters, followed by hyphen (-), 2 to 3 letters, hyphen (-) and 7 numbers
// Example: ABC-DE-1234567
"pattern4": regexp.MustCompile(`(?i)^[a-z]{2,3}-[a-z]{2,3}-\d{7}$`),

//Starts with 4 numbers, followed by hyphen (-) and 5 numbers
//Example: 1234-12345
"pattern5": regexp.MustCompile(`^[\d]{4}-[\d]{5}$`),
// Starts with 4 numbers, followed by hyphen (-) and 5 numbers
// Example: 1234-12345
"pattern5": regexp.MustCompile(`^\d{4}-\d{5}$`),

//Numeric only with the length 7,9,10 or 14
//Example: 123456789
//9 and 10 in UPS as well !!!
"numbers7": regexp.MustCompile(`^[\d]{7}$`),
"numbers9_10": regexp.MustCompile(`^[\d]{9,10}$`),
"numbers14": regexp.MustCompile(`^[\d]{14}$`),
// Numeric only with the length 7,9,10 or 14
// Example: 123456789
// 9 and 10 in UPS as well !!!
"numbers7": regexp.MustCompile(`^\d{7}$`),
"numbers9_10": regexp.MustCompile(`^\d{9,10}$`),
"numbers14": regexp.MustCompile(`^\d{14}$`),
}

const layout = "2006-01-02T15:04:05Z"
Expand Down
4 changes: 2 additions & 2 deletions pkg/determine-delivery/carriers/fedex/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (api *Api) TrackByTrackingNumber(trackingRequest TrackingRequest) (*Trackin
return nil, err
}

if err = getErrors(trackingResponse.Errors); err != nil {
if err := getErrors(trackingResponse.Errors); err != nil {
return nil, err
}

Expand Down Expand Up @@ -142,7 +142,7 @@ func (api *Api) authorize() (string, error) {
if err := json.Unmarshal(response, res); err != nil {
return "", err
}
if err = getErrors(res.Errors); err != nil {
if err := getErrors(res.Errors); err != nil {
return "", err
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/determine-delivery/carriers/fedex/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package fedex

import (
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

Expand Down Expand Up @@ -54,7 +54,7 @@ func TestApi_TrackByTrackingNumber(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(testCase.status)

b, err := ioutil.ReadFile(testCase.file)
b, err := os.ReadFile(testCase.file)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestApi_authorize(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadFile(testCase.file)
b, err := os.ReadFile(testCase.file)
if err != nil {
t.Fatal(err)
}
Expand Down
29 changes: 12 additions & 17 deletions pkg/determine-delivery/carriers/fedex/carrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
)

var patterns = map[string]*regexp.Regexp{
//Numeric only with the length 12
"numbers12": regexp.MustCompile(`^[\d]{12}$`),
//Numeric only with the length 15
"numbers15": regexp.MustCompile(`^[\d]{15}$`),
//Numeric only with the length 20
"numbers20": regexp.MustCompile(`^[\d]{20}$`),
//Numeric only with the length 22
//22 in UPS as well !!!
"numbers22": regexp.MustCompile(`^[\d]{22}$`),
// Numeric only with the length 12
"numbers12": regexp.MustCompile(`^\d{12}$`),
// Numeric only with the length 15
"numbers15": regexp.MustCompile(`^\d{15}$`),
// Numeric only with the length 20
"numbers20": regexp.MustCompile(`^\d{20}$`),
// Numeric only with the length 22
// 22 in UPS as well !!!
"numbers22": regexp.MustCompile(`^\d{22}$`),
}

type api interface {
Expand Down Expand Up @@ -83,22 +83,17 @@ func (c *Carrier) track(trackNumbers []string, chanParcels chan []carriers.Parce
return
}

p, err := prepareResponse(response)
if err != nil {
chanErr <- err
return
}
mu.Lock()
defer mu.Unlock()
parcels = append(parcels, p...)
parcels = append(parcels, prepareResponse(response)...)
}(trackNumber)
}

wg.Wait()
chanParcels <- parcels
}

func prepareResponse(response *TrackingResponse) ([]carriers.Parcel, error) {
func prepareResponse(response *TrackingResponse) []carriers.Parcel {
parcels := make([]carriers.Parcel, len(response.Output.CompleteTrackResults))
for i, d := range response.Output.CompleteTrackResults {
parcels[i] = carriers.Parcel{
Expand All @@ -108,7 +103,7 @@ func prepareResponse(response *TrackingResponse) ([]carriers.Parcel, error) {
DeliveryDate: d.TrackResults[0].EstimatedDeliveryTimeWindow.Window.Ends,
}
}
return parcels, nil
return parcels
}

func getPlaces(result TrackResult) []carriers.Place {
Expand Down
1 change: 0 additions & 1 deletion pkg/determine-delivery/carriers/fedex/carrier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func TestCarrier_Track(t *testing.T) {
name: "Ok response",
trackNumbers: []string{"12A12345", "12A12346"},
setupApiMock: func(api *apiMock, trackNumbers []string) {

trackingInfo := TrackingInfo{
TrackingNumberInfo: TrackingNumberInfo{
TrackingNumber: trackNumbers[0],
Expand Down
7 changes: 3 additions & 4 deletions pkg/determine-delivery/carriers/me/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"github.com/valyala/fasthttp"
)

const statusOk = "000" //Ok
const statusOk = "000" // Ok

var handleErrors = map[string]error{
"103": response_errors.NotFound, //Document not found
"104": response_errors.NotFound, //Directory not found
"103": response_errors.NotFound, // Document not found
"104": response_errors.NotFound, // Directory not found
}

type Api struct {
Expand Down Expand Up @@ -51,7 +51,6 @@ func (api *Api) TrackByTrackingNumber(trackNumber string) (*ShipmentsTrackRespon
}

if shipmentsTrackResponse.Errors.Code != statusOk {

if err, ok := handleErrors[shipmentsTrackResponse.Errors.Code]; ok {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/determine-delivery/carriers/me/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package me

import (
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -37,7 +37,7 @@ func TestApi_TrackByTrackingNumber(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)

b, err := ioutil.ReadFile(testCase.file)
b, err := os.ReadFile(testCase.file)
if err != nil {
t.Fatal(err)
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/determine-delivery/carriers/me/carrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
)

var patterns = map[string]*regexp.Regexp{
//Starts with CV, 9 numbers and 2 letters at the end
//CV999999999AA
"startCV": regexp.MustCompile(`(?i)^CV[\d]{9}[a-z]{2}$`),
// Starts with CV, 9 numbers and 2 letters at the end
// CV999999999AA
"startCV": regexp.MustCompile(`(?i)^CV\d{9}[a-z]{2}$`),

//Starts with MYCV, 9 numbers and 2 letters at the end
//MYCV999999999AA
"startMYCV": regexp.MustCompile(`(?i)^MYCV[\d]{9}[a-z]{2}$`),
// Starts with MYCV, 9 numbers and 2 letters at the end
// MYCV999999999AA
"startMYCV": regexp.MustCompile(`(?i)^MYCV\d{9}[a-z]{2}$`),
}

const layout = "2016-06-30 13: 42: 11"
Expand Down
Loading

0 comments on commit fc74736

Please sign in to comment.