refactor: apply golangci-lint findings
All checks were successful
concourse-ci/lint/golangci-lint Lint Go files

This commit is contained in:
Peter 2023-01-31 21:14:34 +01:00
parent 1568bf925d
commit ec31dd14d4
Signed by: prskr
GPG key ID: C1DB5D2E8DB512F9
6 changed files with 177 additions and 12 deletions

View file

@ -21,4 +21,4 @@ run:
args:
- -ce
- |
go run gotest.tools/gotestsum@latest ./...
go run gotest.tools/gotestsum@latest -f pkgname-and-test-fails -- -race -shuffle=on ./...

142
.golangci.yml Normal file
View file

@ -0,0 +1,142 @@
linters-settings:
dupl:
threshold: 100
funlen:
lines: 100
statements: 50
gci:
local-prefixes: code.icb4dc0.de/prskr/gapr
gosec:
excludes:
- G404
goconst:
min-len: 2
min-occurrences: 2
gocritic:
enabled-tags:
- diagnostic
- opinionated
- performance
disabled-checks:
- ifElseChain
- octalLiteral
- wrapperFunc
# see https://github.com/golangci/golangci-lint/issues/2649
- hugeParam
- rangeValCopy
# settings:
# hugeParam:
# sizeThreshold: 200
gocyclo:
min-complexity: 15
goimports:
local-prefixes: code.icb4dc0.de/prskr/gapr
golint:
min-confidence: 0
gomnd:
settings:
mnd:
# don't include the "operation" and "assign"
checks:
- argument
- case
- condition
- return
gomoddirectives:
replace-allow-list: [ ]
govet:
check-shadowing: true
enable-all: true
disable:
- fieldalignment
# see https://github.com/golangci/golangci-lint/issues/2649
- nilness
- unusedwrite
importas:
no-unaliased: true
alias: [ ]
lll:
line-length: 140
misspell:
locale: US
nolintlint:
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
allow-unused: false # report any unused nolint directives
require-explanation: false # don't require an explanation for nolint directives
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
linters:
disable-all: true
enable:
- contextcheck
- dogsled
- dupl
- errcheck
- errchkjson
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocognit
- goconst
# - gocritic
- gocyclo
- godox
- gofumpt
- goimports
- gomoddirectives
- gomnd
- gosec
- gosimple
- govet
- importas
- ineffassign
- ireturn
- lll
- misspell
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- paralleltest
- prealloc
- predeclared
- promlinter
- staticcheck
- stylecheck
- testpackage
- thelper
# - typecheck
- unconvert
- unparam
- whitespace
- unused
- wastedassign
issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
- path: _test\.go
linters:
- dupl
- funlen
- gocognit
- gomnd
- govet
- dupl
- path: magefiles/.*\.go
linters:
- typecheck
run:
build-tags: [ ]
skip-dirs: [ ]
skip-files:
- ".*\\.pb\\.go$"
- ".*.mock.\\.go$"
modules-download-mode: readonly
go: "1.18"
timeout: 10m

View file

@ -44,6 +44,7 @@ func (g *Gapr) Map(input any) (any, error) {
return nil, ErrNotSupportedType
}
//nolint:exhaustive // handled with default case
switch t.Kind() {
case reflect.Map:
return g.mapMap(t, v)
@ -114,7 +115,7 @@ func (g *Gapr) mapMap(t reflect.Type, v reflect.Value) (any, error) {
func (g *Gapr) mapSliceOrArray(v reflect.Value) (any, error) {
var (
length = v.Len()
target = reflect.ValueOf(make([]any, length, length))
target = reflect.ValueOf(make([]any, length))
)
for i := 0; i < length; i++ {
@ -170,6 +171,7 @@ func canMap(t reflect.Type, v reflect.Value) bool {
t = reflect.TypeOf(v.Interface())
}
//nolint:exhaustive // handled with default case
switch t.Kind() {
case reflect.Struct, reflect.Map, reflect.Slice, reflect.Array:
return true

View file

@ -8,6 +8,8 @@ import (
)
func TestGapr_Map(t *testing.T) {
t.Parallel()
type args struct {
input any
opts []gapr.Option
@ -148,12 +150,20 @@ func TestGapr_Map(t *testing.T) {
},
},
},
want: nil,
want: map[string]any{
"Hello": map[string]any{
"givenName": "Ted",
"Surname": "Tester",
},
},
wantErr: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
g := gapr.New(tt.args.opts...)
got, err := g.Map(tt.args.input)
if (got == nil) == (err == nil) {

View file

@ -16,6 +16,9 @@ func (f KeyMapperFunc) MapKey(orig string) string {
}
var (
identityKeyMapper KeyMapper = KeyMapperFunc(func(orig string) string {
return orig
})
LowercaseKeyMapper KeyMapper = KeyMapperFunc(func(orig string) string {
return strings.ToLower(orig)
})
@ -29,8 +32,8 @@ var (
PascalCaseKeyMapper = genericFirstKeyMapper(unicode.ToUpper)
)
func genericFirstKeyMapper(m func(r rune) rune) KeyMapper {
return KeyMapperFunc(func(orig string) string {
func genericFirstKeyMapper(m func(r rune) rune) KeyMapperFunc {
return func(orig string) string {
if len(orig) < 1 {
return ""
}
@ -43,5 +46,5 @@ func genericFirstKeyMapper(m func(r rune) rune) KeyMapper {
}
return string(mapped)
})
}
}

View file

@ -2,22 +2,29 @@ package gapr
import "time"
func WithRandomSeed(i int64) Option {
return optionFunc(func(o *options) {
func WithRandomSeed(i int64) optionFunc {
return func(o *options) {
o.RandomSeed = i
})
}
}
func WithTagName(tag string) Option {
return optionFunc(func(o *options) {
func WithTagName(tag string) optionFunc {
return func(o *options) {
o.TagName = tag
})
}
}
func WithMapper(mapper KeyMapper) optionFunc {
return func(o *options) {
o.Mapper = mapper
}
}
func defaultOptions() options {
return options{
TagName: "gapr",
RandomSeed: time.Now().UTC().Unix(),
Mapper: identityKeyMapper,
}
}
@ -34,4 +41,5 @@ func (f optionFunc) apply(o *options) {
type options struct {
TagName string
RandomSeed int64
Mapper KeyMapper
}