Skip to content

feat: Integrate GolangCi linter #105

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

Merged
merged 1 commit into from
May 18, 2025
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
10 changes: 3 additions & 7 deletions .github/workflows/code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,15 @@ jobs:
- uses: actions/setup-go@v5.5.0 # immutable action, safe to use the versions
with:
go-version-file: go.mod
- name: gofmt
run: diff -u <(echo -n) <(gofmt -l . )
- name: show diff
if: ${{ failure() }}
run: git diff
- run: go vet ./...
- uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0
with:
version: latest

UnitTestJob:
runs-on: ubuntu-latest
strategy:
matrix:
go:
- "1.21"
- "1.22"
- "1.23"
- "1.24"
Expand Down
106 changes: 106 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
version: "2"

linters:
default: none

enable: # keep in ascending order
- asasalint
- asciicheck
- bodyclose
- canonicalheader
- containedctx
- contextcheck
- copyloopvar
- decorder
- dogsled
- dupword
- durationcheck
- err113
- errcheck
- errchkjson
- errname
- errorlint
- exptostd
- fatcontext
- gocheckcompilerdirectives
- gochecksumtype
- goconst
- gocritic
- goprintffuncname
- gosec
- gosmopolitan
- govet
- iface
- inamedparam
- ineffassign
- intrange
- makezero
- mirror
- misspell
- musttag
- nestif
- nilerr
- nilnesserr
- nilnil
- noctx
- nolintlint
- perfsprint
- prealloc
- predeclared
- reassign
- recvcheck
- sloglint
- staticcheck
- tagalign
- testableexamples
- testifylint
- testpackage
- thelper
- tparallel
- unconvert
- unparam
- unused
- usestdlibvars
- usetesting
- wastedassign
- whitespace

settings:
misspell:
locale: US
nestif:
min-complexity: 12
goconst:
min-len: 3
min-occurrences: 3
prealloc:
for-loops: true
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- whyNoLint

exclusions:
warn-unused: true
rules:
- path: _test\.go
linters:
- gosec

formatters:
enable:
- gci
- gofumpt
settings:
gci:
sections:
- standard
- default
- prefix(github.com/sv-tools/openapi)
gofumpt:
extra-rules: true
2 changes: 1 addition & 1 deletion bool_or_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type testAD struct {
AP *openapi.BoolOrSchema `json:"ap,omitempty" yaml:"ap,omitempty"`
AP *openapi.BoolOrSchema `json:"ap,omitempty" yaml:"ap,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
}

Expand Down
3 changes: 2 additions & 1 deletion callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openapi

import (
"encoding/json"

"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -57,7 +58,7 @@ func (o *Callback) validateSpec(location string, validator *Validator) []*valida
for k, v := range o.Paths {
errs = append(errs, v.validateSpec(joinLoc(location, k), validator)...)
}
return nil
return errs
}

func (o *Callback) Add(expression string, item *RefOrSpec[Extendable[PathItem]]) *Callback {
Expand Down
2 changes: 1 addition & 1 deletion components.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (o *Components) Add(name string, v any) *Components {
return o
}

var namePattern = regexp.MustCompile(`^[a-zA-Z0-9\.\-_]+$`)
var namePattern = regexp.MustCompile(`^[a-zA-Z0-9.\-_]+$`)

func (o *Components) validateSpec(location string, validator *Validator) []*validationError {
var errs []*validationError
Expand Down
64 changes: 52 additions & 12 deletions components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ import (
func TestComponents_Add(t *testing.T) {
for _, tt := range []struct {
name string
create func() (string, any)
create func(tb testing.TB) (string, any)
check func(tb testing.TB, c *openapi.Components)
}{
{
name: "schema ref or spec",
create: func() (string, any) {
create: func(tb testing.TB) (string, any) {
tb.Helper()

o := openapi.NewSchemaBuilder().Title("test").Build()
return "testSchema", o
},
check: func(tb testing.TB, c *openapi.Components) {
tb.Helper()

require.Len(tb, c.Schemas, 1)
require.NotNil(tb, c.Schemas["testSchema"])
require.NotNil(tb, c.Schemas["testSchema"].Spec)
Expand All @@ -29,11 +33,15 @@ func TestComponents_Add(t *testing.T) {
},
{
name: "response spec",
create: func() (string, any) {
create: func(tb testing.TB) (string, any) {
tb.Helper()

o := openapi.NewResponseBuilder().Description("test").Build()
return "testResponse", o
},
check: func(tb testing.TB, c *openapi.Components) {
tb.Helper()

require.Len(tb, c.Responses, 1)
require.NotNil(tb, c.Responses["testResponse"])
require.NotNil(tb, c.Responses["testResponse"].Spec)
Expand All @@ -43,11 +51,15 @@ func TestComponents_Add(t *testing.T) {
},
{
name: "parameter spec",
create: func() (string, any) {
create: func(tb testing.TB) (string, any) {
tb.Helper()

o := openapi.NewParameterBuilder().Description("test").Build()
return "testParameter", o
},
check: func(tb testing.TB, c *openapi.Components) {
tb.Helper()

require.Len(tb, c.Parameters, 1)
require.NotNil(tb, c.Parameters["testParameter"])
require.NotNil(tb, c.Parameters["testParameter"].Spec)
Expand All @@ -57,11 +69,15 @@ func TestComponents_Add(t *testing.T) {
},
{
name: "examples spec",
create: func() (string, any) {
create: func(tb testing.TB) (string, any) {
tb.Helper()

o := openapi.NewExampleBuilder().Description("test").Build()
return "testExamples", o
},
check: func(tb testing.TB, c *openapi.Components) {
tb.Helper()

require.Len(tb, c.Examples, 1)
require.NotNil(tb, c.Examples["testExamples"])
require.NotNil(tb, c.Examples["testExamples"].Spec)
Expand All @@ -71,11 +87,15 @@ func TestComponents_Add(t *testing.T) {
},
{
name: "request body spec",
create: func() (string, any) {
create: func(tb testing.TB) (string, any) {
tb.Helper()

o := openapi.NewRequestBodyBuilder().Description("test").Build()
return "testRequestBodies", o
},
check: func(tb testing.TB, c *openapi.Components) {
tb.Helper()

require.Len(tb, c.RequestBodies, 1)
require.NotNil(tb, c.RequestBodies["testRequestBodies"])
require.NotNil(tb, c.RequestBodies["testRequestBodies"].Spec)
Expand All @@ -85,11 +105,15 @@ func TestComponents_Add(t *testing.T) {
},
{
name: "headers spec",
create: func() (string, any) {
create: func(tb testing.TB) (string, any) {
tb.Helper()

o := openapi.NewHeaderBuilder().Description("test").Build()
return "testHeader", o
},
check: func(tb testing.TB, c *openapi.Components) {
tb.Helper()

require.Len(tb, c.Headers, 1)
require.NotNil(tb, c.Headers["testHeader"])
require.NotNil(tb, c.Headers["testHeader"].Spec)
Expand All @@ -99,13 +123,17 @@ func TestComponents_Add(t *testing.T) {
},
{
name: "security schemes spec",
create: func() (string, any) {
create: func(tb testing.TB) (string, any) {
tb.Helper()

o := &openapi.SecurityScheme{
Description: "test",
}
return "testSecurityScheme", openapi.NewRefOrExtSpec[openapi.SecurityScheme](o)
},
check: func(tb testing.TB, c *openapi.Components) {
tb.Helper()

require.Len(tb, c.SecuritySchemes, 1)
require.NotNil(tb, c.SecuritySchemes["testSecurityScheme"])
require.NotNil(tb, c.SecuritySchemes["testSecurityScheme"].Spec)
Expand All @@ -115,11 +143,15 @@ func TestComponents_Add(t *testing.T) {
},
{
name: "link spec",
create: func() (string, any) {
create: func(tb testing.TB) (string, any) {
tb.Helper()

o := openapi.NewLinkBuilder().Description("test").Build()
return "testLink", o
},
check: func(tb testing.TB, c *openapi.Components) {
tb.Helper()

require.Len(tb, c.Links, 1)
require.NotNil(tb, c.Links["testLink"])
require.NotNil(tb, c.Links["testLink"].Spec)
Expand All @@ -129,14 +161,18 @@ func TestComponents_Add(t *testing.T) {
},
{
name: "callback spec",
create: func() (string, any) {
create: func(tb testing.TB) (string, any) {
tb.Helper()

o := openapi.NewCallbackBuilder().AddPathItem(
"testPath",
openapi.NewPathItemBuilder().Description("test").Build(),
).Build()
return "testCallback", o
},
check: func(tb testing.TB, c *openapi.Components) {
tb.Helper()

require.Len(tb, c.Callbacks, 1)
require.NotNil(tb, c.Callbacks["testCallback"])
require.NotNil(tb, c.Callbacks["testCallback"].Spec)
Expand All @@ -151,11 +187,15 @@ func TestComponents_Add(t *testing.T) {
},
{
name: "path item spec",
create: func() (string, any) {
create: func(tb testing.TB) (string, any) {
tb.Helper()

o := openapi.NewPathItemBuilder().Description("test").Build()
return "testPathItem", o
},
check: func(tb testing.TB, c *openapi.Components) {
tb.Helper()

require.Len(tb, c.Paths, 1)
require.NotNil(tb, c.Paths["testPathItem"])
require.NotNil(tb, c.Paths["testPathItem"].Spec)
Expand All @@ -165,7 +205,7 @@ func TestComponents_Add(t *testing.T) {
},
} {
t.Run(tt.name, func(t *testing.T) {
name, obj := tt.create()
name, obj := tt.create(t)
tt.check(t, (&openapi.Components{}).Add(name, obj))
})
}
Expand Down
2 changes: 1 addition & 1 deletion contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Contact struct {
Email string `json:"email,omitempty" yaml:"email,omitempty"`
}

func (o *Contact) validateSpec(location string, validator *Validator) []*validationError {
func (o *Contact) validateSpec(location string, _ *Validator) []*validationError {
var errs []*validationError
if err := checkURL(o.URL); err != nil {
errs = append(errs, newValidationError(joinLoc(location, "url"), err))
Expand Down
2 changes: 1 addition & 1 deletion example.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Example struct {
ExternalValue string `json:"externalValue,omitempty" yaml:"externalValue,omitempty"`
}

func (o *Example) validateSpec(location string, validator *Validator) []*validationError {
func (o *Example) validateSpec(location string, _ *Validator) []*validationError {
var errs []*validationError
if o.Value != nil && o.ExternalValue != "" {
errs = append(errs, newValidationError(joinLoc(location, "value&externalValue"), ErrMutuallyExclusive))
Expand Down
Loading