Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Lint config and lint fixes #24

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- main

env:
GOLANGCI_LINT_VERSION: v1.55
GOLANGCI_LINT_VERSION: v1.58

jobs:
test:
Expand Down
158 changes: 158 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
run:
timeout: 5m

issues:
max-issues-per-linter: 0
max-same-issues: 0
exclude-rules:
- path: _test\.go
linters:
- bodyclose
- dupl
- errcheck
- errorlint
- funlen
- goconst
- gocyclo
- goerr113
- gosec
- lll
- noctx
- prealloc
- unparam

output:
uniq-by-line: true
sort-results: true

linters:
disable-all: true
enable:
- asciicheck
- depguard
- durationcheck
- errcheck
- errorlint
- exhaustive
- exportloopref
- gocritic
- gofmt
- goimports
- gomoddirectives
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- makezero
- megacheck
- misspell
- nakedret
- nakedret
- nilerr
- noctx
- nolintlint
- prealloc
- revive
- staticcheck
- stylecheck
- testifylint
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- usestdlibvars
- unused
- wastedassign
- whitespace

linters-settings:
depguard:
rules:
main:
deny:
- pkg: github.com/pkg/errors
desc: Use "errors" from std lib instead.
- pkg: golang.org/x/exp/slices
desc: Use "slices" from std lib instead.
- pkg: golang.org/x/exp/maps
desc: Use "maps" from std lib instead.
errorlint:
errorf: true
asserts: false
comparison: true
errcheck:
check-type-assertions: true
check-blank: false
exhaustive:
default-signifies-exhaustive: true
gci:
sections:
- standard
- default
- prefix(github.com/sumup/x)
- dot
skip-generated: false
gocritic:
disabled-checks:
- sloppyReassign
- whyNoLint
enabled-tags:
- diagnostic
- style
- performance
settings:
hugeParam:
sizeThreshold: 512
gofmt:
simplify: true
goimports:
local-prefixes: github.com/sumup/x
gomoddirectives:
replace-local: true
gosimple:
checks: ["all"]
govet:
enable-all: true
disable:
- fieldalignment
nakedret:
max-func-lines: 10
misspell:
locale: US
prealloc:
simple: true
range-loops: true
for-loops: false
revive:
rules:
- name: blank-imports
disabled: true
nolintlint:
allow-unused: false
require-explanation: true
require-specific: true
staticcheck:
checks: ['all']
stylecheck:
checks: ['all']
unparam:
check-exported: false
unused: {}
whitespace:
multi-if: false
multi-func: false
testifylint:
enable:
- bool-compare
- compares
- empty
- error-is-as
- error-nil
- expected-actual
- float-compare
- len
- suite-dont-use-pkg
- suite-extra-assert-call
- suite-thelper
4 changes: 2 additions & 2 deletions base32/base32.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func EncodeTo(dst []byte, src [16]byte, alphabet string) {

// Encode encodes the src [16]byte into a base32 string with the given alphabet.
// The alphabet must be 32 bytes long. If the alphabet is shorter, the function
// will panic. It's the callers responsiblity to ensure the alphabet is valid.
// will panic. It's the callers responsibility to ensure the alphabet is valid.
//
// Direct usage is discouraged. Use EncodeUpper or EncodeLower instead.
func Encode(src [16]byte, alphabet string) string {
Expand Down Expand Up @@ -163,7 +163,7 @@ func DecodeLower(s string) ([]byte, error) {
}

// Decode decodes a given base32 string into a 16-byte slice. The second argument is a index lookup table, that
// must be 256 bytes long. If the table is shorter, the function will panic. It's the callers responsiblity to
// must be 256 bytes long. If the table is shorter, the function will panic. It's the callers responsibility to
// ensure the table is valid.
//
// Direct usage is discouraged. Use DecodeUpper or DecodeLower instead.
Expand Down
12 changes: 12 additions & 0 deletions encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func TestTypeID_Pgx_Scan(t *testing.T) {
codec := pgtype.TextCodec{}

t.Run("pgx binary scan", func(t *testing.T) {
t.Parallel()

t.Run("valid string bytes", func(t *testing.T) {
var target UserID
err := codec.PlanScan(pgtypeMap, pgtype.TextOID, pgtype.BinaryFormatCode, &target).
Expand All @@ -44,6 +46,8 @@ func TestTypeID_Pgx_Scan(t *testing.T) {
})

t.Run("pgx text scan", func(t *testing.T) {
t.Parallel()

var target UserID
err := codec.PlanScan(pgtypeMap, pgtype.TextOID, pgtype.TextFormatCode, &target).
Scan([]byte(userIDStr), &target)
Expand All @@ -55,6 +59,8 @@ func TestTypeID_Pgx_Scan(t *testing.T) {
}
})
t.Run("error on invalid byte count", func(t *testing.T) {
t.Parallel()

var target UserID
err := codec.PlanScan(pgtypeMap, pgtype.UUIDOID, pgtype.BinaryFormatCode, &target).
Scan([]byte("2345-2222-111-222"), &target)
Expand All @@ -64,6 +70,8 @@ func TestTypeID_Pgx_Scan(t *testing.T) {
})

t.Run("error on nil scan", func(t *testing.T) {
t.Parallel()

var target UserID
err := codec.PlanScan(pgtypeMap, pgtype.UUIDOID, pgtype.BinaryFormatCode, &target).
Scan(nil, &target)
Expand All @@ -88,6 +96,8 @@ func TestTypeID_Pgx_Value(t *testing.T) {
codec := pgtype.TextCodec{}

t.Run("binary encoding", func(t *testing.T) {
t.Parallel()

var buf []byte
newBuf, err := codec.PlanEncode(pgtypeMap, pgtype.TextOID, pgtype.BinaryFormatCode, original).
Encode(original, buf)
Expand All @@ -100,6 +110,8 @@ func TestTypeID_Pgx_Value(t *testing.T) {
})

t.Run("text encoding", func(t *testing.T) {
t.Parallel()

var buf []byte
newBuf, err := codec.PlanEncode(pgtypeMap, pgtype.TextOID, pgtype.TextFormatCode, original).
Encode(original, buf)
Expand Down
2 changes: 1 addition & 1 deletion generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type processor struct {
b32EncodeTo func([]byte, uuid.UUID)
// b32Decode decode a UUID using the resp. base32 decoding.
b32Decode func(string) (uuid.UUID, error)
// Generates a new universal unique identifer.
// Generates a new universal unique identifier.
generateUUID func() (uuid.UUID, error)
}

Expand Down
16 changes: 11 additions & 5 deletions typeid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func TestTypeID_ToFrom(t *testing.T) {
}

func runToFromQuickTests[T idImplementation[P], P Prefix](t *testing.T) {
t.Helper()
t.Parallel()

t.Run("from string", func(t *testing.T) {
Expand Down Expand Up @@ -123,6 +124,7 @@ func runToFromQuickTests[T idImplementation[P], P Prefix](t *testing.T) {
}

func fromStringTester[T idImplementation[P], P Prefix](t *testing.T) func(wid wrappedID[T, P]) bool {
t.Helper()
return func(wid wrappedID[T, P]) bool {
parsedID, err := FromString[T](wid.ID().String())
if err != nil {
Expand All @@ -133,6 +135,7 @@ func fromStringTester[T idImplementation[P], P Prefix](t *testing.T) func(wid wr
}

func fromUUIDTester[T idImplementation[P], P Prefix](t *testing.T) func(wid wrappedID[T, P]) bool {
t.Helper()
return func(wid wrappedID[T, P]) bool {
parsedID, err := FromUUID[T](wid.ID().UUID())
if err != nil {
Expand All @@ -143,6 +146,7 @@ func fromUUIDTester[T idImplementation[P], P Prefix](t *testing.T) func(wid wrap
}

func fromUUIDStringTester[T idImplementation[P], P Prefix](t *testing.T) func(wid wrappedID[T, P]) bool {
t.Helper()
return func(wid wrappedID[T, P]) bool {
parsedID, err := FromUUIDStr[T](wid.ID().UUID().String())
if err != nil {
Expand All @@ -153,6 +157,7 @@ func fromUUIDStringTester[T idImplementation[P], P Prefix](t *testing.T) func(wi
}

func fromUUIDBytesTester[T idImplementation[P], P Prefix](t *testing.T) func(wid wrappedID[T, P]) bool {
t.Helper()
return func(wid wrappedID[T, P]) bool {
parsedID, err := FromUUIDBytes[T](wid.id.UUID().Bytes())
if err != nil {
Expand All @@ -172,26 +177,27 @@ func (w wrappedID[T, P]) ID() T {
return w.id
}

func (w wrappedID[T, P]) Generate(rand *rand.Rand, _ int) reflect.Value {
func (w wrappedID[T, P]) Generate(rnd *rand.Rand, _ int) reflect.Value {
// gen the processor to determine the UUID version to use
procGenUUID, err := (T{}).processor().generateUUID()
if err != nil {
panic(err)
}
version := procGenUUID.Version()

uuidGen := uuid.NewGenWithOptions(uuid.WithRandomReader(rand))
uuidGen := uuid.NewGenWithOptions(uuid.WithRandomReader(rnd))
var uid uuid.UUID

if version == uuid.V4 {
switch version {
case uuid.V4:
if uid, err = uuidGen.NewV4(); err != nil {
panic("failed to generate uuid v4")
}
} else if version == uuid.V7 {
case uuid.V7:
if uid, err = uuidGen.NewV7(); err != nil {
panic("failed to generate uuid v7")
}
} else {
default:
panic("unknown uuid version")
}

Expand Down