Skip to content

Commit

Permalink
fix(linter): fix golangci-lint warnings across substation (#32)
Browse files Browse the repository at this point in the history
* fix(linter): fix golangci-lint warnings across substation

* handle or explicitly ignore all errors
* remove redundant function calls/conversions
* rename predeclared `caps` identifier
* tools: gofmt -> gofumpt, and staticcheck -> golangci-lint

* fixup! fix(linter): fix golangci-lint warnings across substation

Address code review comments.

Changes include:
- Undoing spurious whitespace changes
- Rename variables with modifiers related to `cap` like newCap
- Adding variable naming conventions to docs

* docs: move first "Environment Variables" heading under "Design"
  • Loading branch information
shellcromancer authored Oct 4, 2022
1 parent d8df4e2 commit 9b7e077
Show file tree
Hide file tree
Showing 112 changed files with 1,562 additions and 1,361 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ jobs:
- name: tests
run: go test -timeout 30s -v ./...

- name: lint
uses: dominikh/staticcheck-action@v1.2.0
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
install-go: false
checks: "all,-ST1000"
version: v1.49

python:
runs-on: ubuntu-latest
Expand Down
139 changes: 139 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# This code is licensed under the terms of the MIT license.

## Config for golangci-lint v1.49.0 based on https://gist.github.com/maratori/47a4d00457a92aa426dbd48a18776322

run:
# Timeout for analysis, e.g. 30s, 5m.
# Default: 1m
timeout: 3m


# This file contains only configs which differ from defaults.
# All possible options can be found here https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml
linters-settings:
cyclop:
# The maximal code complexity to report.
# Default: 10
max-complexity: 30

errcheck:
# Report about not checking of errors in type assertions: `a := b.(MyStruct)`.
# Such cases aren't reported by default.
# Default: false
check-type-assertions: true

gocognit:
# Minimal code complexity to report
# Default: 30
min-complexity: 45

# gomodguard:
# blocked:
# # List of blocked modules.
# # Default: []
# modules:
# - github.com/golang/protobuf:
# recommendations:
# - google.golang.org/protobuf
# reason: "see https://developers.google.com/protocol-buffers/docs/reference/go/faq#modules"

nakedret:
# Make an issue if func has more lines of code than this setting, and it has naked returns.
# Default: 30
max-func-lines: 0

nolintlint:
# Exclude following linters from requiring an explanation.
# Default: []
allow-no-explanation: [ gocognit ]
# Enable to require an explanation of nonzero length after each nolint directive.
# Default: false
require-explanation: true
# Enable to require nolint directives to mention the specific linter being suppressed.
# Default: false
require-specific: true

rowserrcheck:
# database/sql is always checked
# Default: []
packages:
- github.com/jmoiron/sqlx

tenv:
# The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures.
# Otherwise, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked.
# Default: false
all: true


linters:
disable-all: true
enable:
## enabled by default
- errcheck # checking for unchecked errors, these unchecked errors can be critical bugs in some cases
- gosimple # specializes in simplifying a code
- govet # reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
- ineffassign # detects when assignments to existing variables are not used
- staticcheck # is a go vet on steroids, applying a ton of static analysis checks
- typecheck # like the front-end of a Go compiler, parses and type-checks Go code
- unused # checks for unused constants, variables, functions and types

## disabled by default
- bidichk # checks for dangerous unicode character sequences
- bodyclose # checks whether HTTP response body is closed successfully
- cyclop # checks function and package cyclomatic complexity
- durationcheck # checks for two durations multiplied together
- errname # checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error
- execinquery # checks query string in Query function which reads your Go src files and warning it finds
- exhaustive # checks exhaustiveness of enum switch statements
- gocognit # computes and checks the cognitive complexity of functions
- gocyclo # computes and checks the cyclomatic complexity of functions
- gomoddirectives # manages the use of 'replace', 'retract', and 'excludes' directives in go.mod
- gomodguard # allow and block lists linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations
- goprintffuncname # checks that printf-like functions are named with f at the end
- nakedret # finds naked returns in functions greater than a specified function length
- nestif # reports deeply nested if statements
- nilerr # finds the code that returns nil even if it checks that the error is not nil
- noctx # finds sending http request without context.Context
- nolintlint # reports ill-formed or insufficient nolint directives
- nosprintfhostport # checks for misuse of Sprintf to construct a host with port in a URL
- predeclared # finds code that shadows one of Go's predeclared identifiers
- reassign # checks that package variables are not reassigned
- rowserrcheck # checks whether Err of rows is checked successfully
- sqlclosecheck # checks that sql.Rows and sql.Stmt are closed
- tenv # detects using os.Setenv instead of t.Setenv since Go1.17
- tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes
- unconvert # removes unnecessary type conversions
- unparam # reports unused function parameters
- usestdlibvars # detects the possibility to use variables/constants from the Go standard library
- wastedassign # finds wasted assignment statements
- whitespace # detects leading and trailing whitespace
- misspell # finds commonly misspelled English words in comments

issues:
# Maximum count of issues with the same text.
# Set to 0 to disable.
# Default: 3
max-same-issues: 50

exclude:
- 'declaration of "(err|ctx)" shadows declaration at'

exclude-rules:
- source: "^//\\s*go:generate\\s"
linters: [ lll ]
- source: "(noinspection|TODO)"
linters: [ godot ]
- source: "//noinspection"
linters: [ gocritic ]
- source: "^\\s+if _, ok := err\\.\\([^.]+\\.InternalError\\); ok {"
linters: [ errorlint ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx
- wrapcheck
9 changes: 7 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"terminal.integrated.defaultProfile.linux": "bash",
"go.formatTool": "gofmt",
"go.useLanguageServer": true,
"gopls": {
"formatting.gofumpt": true,
},
"go.formatTool": "gofumpt",
"go.inferGopath": false,
"go.lintFlags": ["-checks=all,-ST1000"],
"go.lintOnSave": "workspace",
"go.lintTool": "golangci-lint",
"cSpell.enabled": false
}
16 changes: 10 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ We rely on contributors to test changes before they are submitted as pull reques

### Design Patterns

#### Environment Variables

Substation uses environment variables to customize runtime settings of the system (e.g., concurrency is controlled by `SUBSTATION_CONCURRENCY`).

Custom applications should implement their own runtime settings as required; for example, the [AWS Lambda application](/cmd/aws/lambda/substation/) uses `SUBSTATION_HANDLER` to manage [invocation settings](https://docs.aws.amazon.com/lambda/latest/dg/lambda-invocation.html).

##### Configurations

Substation uses a single configuration pattern for all components in the system (see `Config` in [config/config.go](/config/config.go)). This pattern is highly reusable and should be nested to create complex configurations supported by Jsonnet. Below is an example that shows how configurations should be designed:
Expand Down Expand Up @@ -86,12 +92,6 @@ Factories are the preferred method for allowing users to customize the system. E

### Naming Conventions

#### Environment Variables

Substation uses environment variables to customize runtime settings of the system (e.g., concurrency is controlled by `SUBSTATION_CONCURRENCY`).

Custom applications should implement their own runtime settings as required; for example, the [AWS Lambda application](/cmd/aws/lambda/substation/) uses `SUBSTATION_HANDLER` to manage [invocation settings](https://docs.aws.amazon.com/lambda/latest/dg/lambda-invocation.html).

#### Errors

Errors should always start with `err` (or `Err`, if they are public) and be defined as constants using [internal/errors](/internal/errors/errors.go).
Expand All @@ -104,6 +104,10 @@ Environment variable keys and values specific to the Substation application shou

Any environment variable that changes a default runtime setting should always start with SUBSTATION (for example, SUBSTATION_CONCURRENCY).

#### Application Variables

Variable names should always follow conventions from [Effective Go](https://go.dev/doc/effective_go#names), the [Go Code Review Comments](https://github.com/golang/go/wiki/CodeReviewComments#variable-names) and avoid [predeclared identifiers](https://go.dev/ref/spec#Predeclared_identifiers). For example `capsule` is used instead of `cap` to avoid shadowing the capacity function, modifiers and plural usage are `newCapsule`, and `capsules`.

#### Source Metadata

Sources that [add metadata during encapsulation](/config/README.md) should use lowerCamelCase for their JSON keys.
Expand Down
4 changes: 2 additions & 2 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func (sub *Substation) Concurrency() int {
}

// Send writes encapsulated data into the Transform channel.
func (sub *Substation) Send(cap config.Capsule) {
sub.Channels.Transform.Send(cap)
func (sub *Substation) Send(capsule config.Capsule) {
sub.Channels.Transform.Send(capsule)
}

/*
Expand Down
13 changes: 8 additions & 5 deletions cmd/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ func TestAppLeaks(t *testing.T) {

for _, test := range appLeaksTest {
sub := New()
json.Unmarshal(test.config, &sub.Config)
err := json.Unmarshal(test.config, &sub.Config)
if err != nil {
t.Fatal(err)
}

group, ctx := errgroup.WithContext(context.TODO())

Expand All @@ -134,15 +137,15 @@ func TestAppLeaks(t *testing.T) {

// ingest
group.Go(func() error {
cap := config.NewCapsule()
cap.SetData([]byte(`{"foo":"bar"}`))
capsule := config.NewCapsule()
capsule.SetData([]byte(`{"foo":"bar"}`))

for w := 0; w < 10; w++ {
select {
case <-ctx.Done():
return ctx.Err()
default:
sub.Send(cap)
sub.Send(capsule)
}
}

Expand All @@ -154,6 +157,6 @@ func TestAppLeaks(t *testing.T) {

// block without checking for errors
// this test only checks for leaks
sub.Block(ctx, group)
_ = sub.Block(ctx, group)
}
}
13 changes: 8 additions & 5 deletions cmd/aws/lambda/autoscaling/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ const (
autoscalePercentage = 50.0
)

var cloudwatchAPI cloudwatch.API
var kinesisAPI kinesis.API
var (
cloudwatchAPI cloudwatch.API
kinesisAPI kinesis.API
)

func init() {
cloudwatchAPI.Setup()
Expand Down Expand Up @@ -55,7 +57,8 @@ func handler(ctx context.Context, snsEvent events.SNSEvent) error {
if err != nil {
return fmt.Errorf("handler: %v", err)
}
log.WithField("alarm", alarmName).WithField("stream", stream).WithField("count", shards).Info("retrieved active shard count")
log.WithField("alarm", alarmName).WithField("stream", stream).WithField("count", shards).
Info("retrieved active shard count")

var newShards int64
if strings.Contains(alarmName, "upscale") {
Expand Down Expand Up @@ -126,10 +129,10 @@ func handler(ctx context.Context, snsEvent events.SNSEvent) error {
return nil
}

func downscale(shards float64, pct float64) int64 {
func downscale(shards, pct float64) int64 {
return int64(math.Ceil(shards - (shards * (pct / 100))))
}

func upscale(shards float64, pct float64) int64 {
func upscale(shards, pct float64) int64 {
return int64(math.Ceil(shards + (shards * (pct / 100))))
}
Loading

0 comments on commit 9b7e077

Please sign in to comment.