diff --git a/.concourse/tasks/test.yml b/.concourse/tasks/test.yml index 50467c9..fda54dc 100644 --- a/.concourse/tasks/test.yml +++ b/.concourse/tasks/test.yml @@ -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 ./... diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..8b80ccd --- /dev/null +++ b/.golangci.yml @@ -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 diff --git a/gapr.go b/gapr.go index 44ea539..8276640 100644 --- a/gapr.go +++ b/gapr.go @@ -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 diff --git a/gapr_test.go b/gapr_test.go index 6408ee5..b09a09d 100644 --- a/gapr_test.go +++ b/gapr_test.go @@ -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) { diff --git a/keys.go b/keys.go index 29375a1..e29ad86 100644 --- a/keys.go +++ b/keys.go @@ -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) - }) + } } diff --git a/options.go b/options.go index f67d9e2..563d1be 100644 --- a/options.go +++ b/options.go @@ -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 }