Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
System-Glitch committed Aug 22, 2024
1 parent d93dc30 commit 363a677
Show file tree
Hide file tree
Showing 56 changed files with 15 additions and 130 deletions.
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ linters-settings:
disable:
- require-error
- error-nil
- formatter
revive:
rules:
- name: var-naming
Expand All @@ -31,7 +32,7 @@ linters:
- govet
- unused
- errcheck
- exportloopref
- copyloopvar
- gosimple
- ineffassign
- staticcheck
Expand Down
8 changes: 4 additions & 4 deletions auth/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (a *BasicAuthenticator[T]) Authenticate(request *goyave.Request) (*T, error
if a.Optional {
return nil, nil
}
return nil, fmt.Errorf(request.Lang.Get("auth.no-credentials-provided"))
return nil, fmt.Errorf("%s", request.Lang.Get("auth.no-credentials-provided"))
}

user, err := a.UserService.FindByUsername(request.Context(), username)
Expand All @@ -78,7 +78,7 @@ func (a *BasicAuthenticator[T]) Authenticate(request *goyave.Request) (*T, error
}

if notFound || bcrypt.CompareHashAndPassword([]byte(pass.String()), []byte(password)) != nil {
return nil, fmt.Errorf(request.Lang.Get("auth.invalid-credentials"))
return nil, fmt.Errorf("%s", request.Lang.Get("auth.invalid-credentials"))
}

return user, nil
Expand Down Expand Up @@ -118,12 +118,12 @@ func (a *ConfigBasicAuthenticator) Authenticate(request *goyave.Request) (*Basic
username, password, ok := request.BasicAuth()

if !ok {
return nil, fmt.Errorf(request.Lang.Get("auth.no-credentials-provided"))
return nil, fmt.Errorf("%s", request.Lang.Get("auth.no-credentials-provided"))
}

if subtle.ConstantTimeCompare([]byte(a.Config().GetString("auth.basic.username")), []byte(username)) != 1 ||
subtle.ConstantTimeCompare([]byte(a.Config().GetString("auth.basic.password")), []byte(password)) != 1 {
return nil, fmt.Errorf(request.Lang.Get("auth.invalid-credentials"))
return nil, fmt.Errorf("%s", request.Lang.Get("auth.invalid-credentials"))
}

return &BasicUser{
Expand Down
10 changes: 5 additions & 5 deletions auth/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (a *JWTAuthenticator[T]) Authenticate(request *goyave.Request) (*T, error)
if a.Optional {
return nil, nil
}
return nil, fmt.Errorf(request.Lang.Get("auth.no-credentials-provided"))
return nil, fmt.Errorf("%s", request.Lang.Get("auth.no-credentials-provided"))
}

token, err := jwt.Parse(tokenString, a.keyFunc)
Expand All @@ -247,7 +247,7 @@ func (a *JWTAuthenticator[T]) Authenticate(request *goyave.Request) (*T, error)
user, err := a.UserService.FindByUsername(request.Context(), claims[claimName])
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fmt.Errorf(request.Lang.Get("auth.invalid-credentials"))
return nil, fmt.Errorf("%s", request.Lang.Get("auth.invalid-credentials"))
}
panic(errorutil.New(err))
}
Expand Down Expand Up @@ -291,9 +291,9 @@ func (a *JWTAuthenticator[T]) keyFunc(token *jwt.Token) (any, error) {

func (a *JWTAuthenticator[T]) makeError(language *lang.Language, bitfield uint32) error {
if bitfield&jwt.ValidationErrorNotValidYet != 0 {
return fmt.Errorf(language.Get("auth.jwt-not-valid-yet"))
return fmt.Errorf("%s", language.Get("auth.jwt-not-valid-yet"))
} else if bitfield&jwt.ValidationErrorExpired != 0 {
return fmt.Errorf(language.Get("auth.jwt-expired"))
return fmt.Errorf("%s", language.Get("auth.jwt-expired"))
}
return fmt.Errorf(language.Get("auth.jwt-invalid"))
return fmt.Errorf("%s", language.Get("auth.jwt-invalid"))
}
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func (o object) validate(key string) error {
}

if !valid {
return fmt.Errorf(message)
return fmt.Errorf("%s", message)
}
return nil
}
Expand Down
1 change: 0 additions & 1 deletion database/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ func TestLogger(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
buf := bytes.NewBuffer(make([]byte, 0, 1024))
slogger := slog.New(stdslog.NewJSONHandler(buf, &stdslog.HandlerOptions{Level: c.level}))
Expand Down
1 change: 0 additions & 1 deletion middleware/compress/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ func TestEncoderPriority(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.acceptEncoding, func(t *testing.T) {
middleware := &Middleware{
Encoders: c.encoders,
Expand Down
3 changes: 0 additions & 3 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ func TestLanguageMiddleware(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
executed := false
handler := middleware.Handle(func(_ *Response, req *Request) {
Expand Down Expand Up @@ -479,7 +478,6 @@ func TestValidateMiddleware(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
cfg := config.LoadDefault()
if c.hasDB {
Expand Down Expand Up @@ -642,7 +640,6 @@ func TestCORSMiddleware(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
middleware := &corsMiddleware{}
handler := middleware.Handle(func(resp *Response, _ *Request) {
Expand Down
4 changes: 3 additions & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/stretchr/testify/assert"
)

type requestTestKey struct{}

func TestRequest(t *testing.T) {
t.Run("NewRequest", func(t *testing.T) {
httpReq := httptest.NewRequest(http.MethodGet, "/test", nil)
Expand Down Expand Up @@ -83,7 +85,7 @@ func TestRequest(t *testing.T) {
}
assert.Equal(t, httpReq.Context(), ctx)

key := struct{}{}
key := requestTestKey{}
r2 := r.WithContext(context.WithValue(ctx, key, "value"))
assert.Equal(t, r, r2)

Expand Down
1 change: 0 additions & 1 deletion response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ func TestResponse(t *testing.T) {
}

for _, c := range cases {
c := c
resp, recorder := newTestReponse()
logBuffer := &bytes.Buffer{}
resp.server.Logger = slog.New(slog.NewHandler(false, logBuffer))
Expand Down
1 change: 0 additions & 1 deletion route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ func TestRoute(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(fmt.Sprintf("%s_%s", c.method, c.uri), func(t *testing.T) {
match := routeMatch{currentPath: c.uri}
assert.Equal(t, c.expectedResult, c.route.match(c.method, &match))
Expand Down
2 changes: 0 additions & 2 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ func TestRouter(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
recorder := httptest.NewRecorder()
rawRequest := httptest.NewRequest(c.requestMethod, c.requestURL, nil)
Expand Down Expand Up @@ -553,7 +552,6 @@ func TestRouter(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(fmt.Sprintf("%s_%s", c.method, strings.ReplaceAll(c.path, "/", "_")), func(t *testing.T) {
match := routeMatch{currentPath: c.path}
router.match(c.method, &match)
Expand Down
8 changes: 0 additions & 8 deletions slog/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func TestNewHandler(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(fmt.Sprintf("devMode_%v", c.devMode), func(t *testing.T) {
assert.Equal(t, c.want, NewHandler(c.devMode, c.w))
})
Expand All @@ -67,7 +66,6 @@ func TestDevModeHandler(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
buf := bytes.NewBuffer(make([]byte, 0, 10))
handler := NewDevModeHandler(buf, c.opts)
Expand Down Expand Up @@ -120,7 +118,6 @@ func TestDevModeHandler(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(fmt.Sprintf("%s_%s", c.opts.Level, c.level), func(t *testing.T) {
buf := bytes.NewBuffer(make([]byte, 0, 10))
handler := NewDevModeHandler(buf, c.opts)
Expand Down Expand Up @@ -170,7 +167,6 @@ func TestDevModeHandler(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
handler := NewDevModeHandler(buf, &DevModeHandlerOptions{})
handler.attrs = c.baseAttrs
Expand Down Expand Up @@ -213,7 +209,6 @@ func TestDevModeHandler(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
handler := NewDevModeHandler(buf, &DevModeHandlerOptions{})
handler.attrs = c.baseAttrs
Expand Down Expand Up @@ -245,7 +240,6 @@ func TestDevModeHandlerFormat(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.level.String(), func(t *testing.T) {
assert.Equal(t, c.want, levelColor(c.level))
})
Expand All @@ -271,7 +265,6 @@ func TestDevModeHandlerFormat(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.level.String(), func(t *testing.T) {
assert.Equal(t, c.want, messageColor(c.level))
})
Expand Down Expand Up @@ -433,7 +426,6 @@ func TestDevModeHandlerFormat(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
buf := bytes.NewBuffer(make([]byte, 0, 1024))
handler := NewDevModeHandler(buf, &DevModeHandlerOptions{Level: slog.LevelDebug})
Expand Down
2 changes: 0 additions & 2 deletions slog/slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func TestLogger(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
c.f(nil, pc, "message", slog.String("attr", "val"))

Expand Down Expand Up @@ -152,7 +151,6 @@ func TestLogger(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
c.f()
assert.Regexp(t, c.want, buf.String())
Expand Down
2 changes: 0 additions & 2 deletions static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func TestCleanStaticPath(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.want, func(t *testing.T) {
f, err := fs.Sub(&osfs.FS{}, c.directory)
require.NoError(t, err)
Expand Down Expand Up @@ -96,7 +95,6 @@ func TestStaticHandler(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.uri, func(t *testing.T) {
cfg := config.LoadDefault()
srv, err := New(Options{Config: cfg})
Expand Down
5 changes: 0 additions & 5 deletions util/errors/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func TestErrors(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
errs := toErr(c.reason)
assert.Equal(t, c.expected, errs)
Expand Down Expand Up @@ -95,7 +94,6 @@ func TestErrors(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
err := &Error{reasons: c.reasons}
assert.Equal(t, c.expected, err.Error())
Expand Down Expand Up @@ -133,7 +131,6 @@ func TestErrors(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
assert.Regexp(t, c.expected, c.err.String())
})
Expand All @@ -151,7 +148,6 @@ func TestErrors(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
assert.Regexp(t, c.expected, c.err.FileLine())
})
Expand Down Expand Up @@ -180,7 +176,6 @@ func TestErrors(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
res, err := json.Marshal(c.err)
if c.expectedErr {
Expand Down
1 change: 0 additions & 1 deletion util/session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ func TestGormSession(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
db := DB(c.ctx, fallback)
assert.Equal(t, c.expect, db)
Expand Down
2 changes: 0 additions & 2 deletions util/typeutil/typeutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func TestConvert(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run("TestStruct", func(t *testing.T) {
res, err := Convert[*TestStruct](c.value)
assert.Equal(t, c.want, res)
Expand Down Expand Up @@ -405,7 +404,6 @@ func TestCopy(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(c.desc, func(t *testing.T) {
if c.wantPanic {
assert.Panics(t, func() {
Expand Down
3 changes: 0 additions & 3 deletions util/typeutil/undefined_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ func TestUndefined(t *testing.T) {
}

for _, c := range cases {
c := c
v, err := c.undefined.Value()
assert.Equal(t, c.want, v)
if c.wantErr {
Expand All @@ -129,7 +128,6 @@ func TestUndefined(t *testing.T) {
}

for _, c := range cases {
c := c
assert.Equal(t, c.want, c.undefined.CopyValue())
}
})
Expand All @@ -151,7 +149,6 @@ func TestUndefined(t *testing.T) {
}

for _, c := range cases {
c := c
err := c.undefined.Scan(c.value)
if c.wantErr != nil {
require.ErrorContains(t, err, c.wantErr.Error())
Expand Down
4 changes: 0 additions & 4 deletions validation/after_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func TestAfterValidator(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(fmt.Sprintf("Validate_%v_%t", c.value, c.want), func(t *testing.T) {
v := After(c.ref)
assert.Equal(t, c.want, v.Validate(&Context{
Expand Down Expand Up @@ -82,7 +81,6 @@ func TestAfterEqualValidator(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(fmt.Sprintf("Validate_%v_%t", c.value, c.want), func(t *testing.T) {
v := AfterEqual(c.ref)
assert.Equal(t, c.want, v.Validate(&Context{
Expand Down Expand Up @@ -138,7 +136,6 @@ func TestAfterFieldValidator(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(fmt.Sprintf("Validate_%v_%t", c.value, c.want), func(t *testing.T) {
v := AfterField(path)
assert.Equal(t, c.want, v.Validate(&Context{
Expand Down Expand Up @@ -200,7 +197,6 @@ func TestAfterEqualFieldValidator(t *testing.T) {
}

for _, c := range cases {
c := c
t.Run(fmt.Sprintf("Validate_%v_%t", c.value, c.want), func(t *testing.T) {
v := AfterEqualField(path)
assert.Equal(t, c.want, v.Validate(&Context{
Expand Down
Loading

0 comments on commit 363a677

Please sign in to comment.