Skip to content

Commit

Permalink
refactor: linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
SuddenGunter committed Feb 6, 2022
1 parent df988e8 commit 1b4b013
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 18 deletions.
74 changes: 61 additions & 13 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ linters-settings:
gofumpt:
# Whether or not to use the extra rules extending gofmt.
extra-rules: true
golint:
min-confidence: 0
lll:
# Mininal line length to report.
line-length: 120
# Tab 'legth' when counting length of the line.
# Minimal line length to report.
line-length: 150
# Tab 'length' when counting length of the line.
tab-width: 1
misspell:
# Locale to use when checking for misspellings.
Expand All @@ -31,6 +29,32 @@ linters-settings:
range-loops: true
# Report preallocation suggestions on for loops.
for-loops: true
testpackage:
# Regexp pattern to skip files.
skip-regexp: "(whitebox)_test\\.go"
wsl:
# If true append is only allowed to be cuddled if appending value is matching variables, fields or types on line above
strict-append: true
# Allow calls and assignments to be cuddled as long as the lines have any matching variables, fields or types.
allow-assign-and-call: false
# Allow multiline assignments to be cuddled.
allow-multiline-assign: false
# Allow declarations (var) to be cuddled.
allow-cuddle-declarations: false
# Allow trailing comments in ending of blocks.
allow-trailing-comment: false
# Force newlines in end of case at this limit (0 = never).
force-case-trailing-whitespace: 0
# Force cuddling of err checks with err var assignment.
force-err-cuddling: true
# Allow leading comments to be separated with empty lines.
allow-separated-leading-comment: false
staticcheck:
go: "1.17"
checks: ["all"]
govet:
# report about shadowed variables
check-shadowing: true

linters:
# please, do not use `enable-all`: it's deprecated and will be removed soon.
Expand All @@ -40,28 +64,52 @@ linters:
- dogsled
- gocognit
- gocyclo
- gocritic
- godot
- gofumpt
- revive
- ifshort
- lll
- wsl
- misspell
- nestif
- noctx # Doesn't have any config for linters-settings section.
- prealloc
- staticcheck
- testpackage
- wsl
- ineffassign
- govet

issues:
include:
# Disable excluding of issues about comments from golint.
- EXC0002
# Disable excluding of issues about comments from revive.
- EXC0012
# ineffective break statement. Did you mean to break out of the outer loop? (staticcheck)
- EXC0005
exclude-rules:
- path: app.providers.go
- path: transaction/query.go
text: "variable 'zero' is only used in the if-statement"

# Disable some linters for tests.
- path: _test\.go
linters:
- revive
- wsl
- golint
- ifshort
# Disable reporting some particular issues.
- linters:
- golint
text: "unless it's in another file for this package"
- wsl
text: "if statements should only be cuddled with assignments"
- linters:
- wsl
text: "declarations should never be cuddled"
text: "return statements should not be cuddled if block has more than two lines"
- linters:
- revive
text: "unless it's in another file for this package"
# Exclude issues for long lines with go:generate
- linters:
- lll
source: "^//go:generate "
- linters:
- govet
text: 'declaration of "(err|ctx)" shadows declaration at'
1 change: 1 addition & 0 deletions account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestAccountStorageSave_WithProductionSchema_NoErrorReturned(t *testing.T) {

err = row.Scan(&balance)

require.NoError(t, err)
assert.Equal(t, int64(20000), balance)
}

Expand Down
1 change: 1 addition & 0 deletions app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/rs/zerolog/log"
)

// Initialize dependencies graph.
func Initialize() (*App, error) {
cfg, err := config.FromEnv()
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions importer/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const (
timeout = 30 * time.Second
)

// Worker is the app/Worker interface implementation, so importer lifecycle can be managed with other parts of the
// app lifecycle.
type Worker struct {
im *Importer
accountID string
Expand All @@ -21,12 +23,14 @@ type Worker struct {
wg *sync.WaitGroup
}

// NewWorker creates new instance of Worker.
func NewWorker(im *Importer, accountID string) *Worker {
ctx, cancel := context.WithCancel(context.Background())

return &Worker{im: im, accountID: accountID, ctx: ctx, cancel: cancel, wg: &sync.WaitGroup{}}
}

// Start importer worker. Blocks until Close() is called.
func (w *Worker) Start() {
ticker := time.NewTicker(waitBeforeRuns)

Expand All @@ -53,6 +57,7 @@ func (w *Worker) executeWithTimeout(ctx context.Context) {
w.im.Import(timeoutCtx, w.accountID)
}

// Close importer worker, blocks until import is fully stopped.
func (w Worker) Close() error {
w.ctx.Done()
w.wg.Wait()
Expand Down
5 changes: 5 additions & 0 deletions storage/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ package storage

import "github.com/jackc/pgx/v4/pgxpool"

// Worker is a dummy app/Worker interface implementation, so DB pool can be managed with other parts of the
// app lifecycle.
type Worker struct {
pool *pgxpool.Pool
}

// NewWorker creates new instance of a Worker.
func NewWorker(pool *pgxpool.Pool) *Worker {
return &Worker{pool: pool}
}

// Start is a stub implementation for app/Worker interface. Does nothing.
func (w *Worker) Start() {}

// Close pgx connection pool.
func (w *Worker) Close() error {
w.pool.Close()
return nil
Expand Down
2 changes: 1 addition & 1 deletion transaction/category/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

const (
// common categories
// common categories.

// Default is the ID of category, that must be used for all new imported transactions.
Default = int32(1)
Expand Down
5 changes: 3 additions & 2 deletions transaction/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ func (q Query) appendToSQL(sqlBuilder *strings.Builder, sqlParams []interface{})
sqlBuilder.WriteString(fmt.Sprintf(`"categoryID" = $%v `, len(sqlParams)))
}

zeroTimeValue := time.Time{}
if q.LastUpdatedAt != zeroTimeValue {
//ifshort:i
zero := time.Time{}
if q.LastUpdatedAt != zero {
if len(sqlParams) > 0 {
sqlBuilder.WriteString("AND ")
}
Expand Down
9 changes: 7 additions & 2 deletions transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,11 @@ func (s *Repository) Get(ctx context.Context, query Query, page Page) ([]Transac
sqlParams := make([]interface{}, 0)

sqlBuilder.WriteString("select * from transaction ")

sqlParams = query.appendToSQL(sqlBuilder, sqlParams)

sqlBuilder.WriteString(`order by "time" desc `)

sqlParams = page.appendToSQL(sqlBuilder, sqlParams)

vsx := sqlBuilder.String()
Expand Down Expand Up @@ -269,8 +272,10 @@ func (s *Repository) GetReport(ctx context.Context, from, to time.Time) (map[int
result := make(map[int32]int64)

for rows.Next() {
var categoryID int32
var amount int64
var (
categoryID int32
amount int64
)

err := rows.Scan(
&categoryID,
Expand Down

0 comments on commit 1b4b013

Please sign in to comment.