From c6b1b877fd13d6f74ff58e9c5d35fbe9c78c50cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20BIDON?= Date: Sat, 9 Aug 2025 19:43:38 +0200 Subject: [PATCH] chore(lint): updated linters, relinted code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Frédéric BIDON --- .golangci.yml | 2 ++ api_test.go | 30 +++++++++++++++--------------- middleware.go | 4 ++-- parsing.go | 36 ++++++++++++++++++------------------ 4 files changed, 37 insertions(+), 35 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 5006306..b4ded78 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -21,6 +21,7 @@ linters: - musttag - nestif - nlreturn + - noinlineerr - nonamedreturns - paralleltest - testpackage @@ -31,6 +32,7 @@ linters: - whitespace - wrapcheck - wsl + - wsl_v5 settings: dupl: threshold: 200 diff --git a/api_test.go b/api_test.go index 94fb08b..b508c28 100644 --- a/api_test.go +++ b/api_test.go @@ -207,37 +207,37 @@ func TestAPIErrors(t *testing.T) { err := New(402, "this failed %s", "yada") require.Error(t, err) assert.EqualValues(t, 402, err.Code()) - assert.EqualValues(t, "this failed yada", err.Error()) + assert.Equal(t, "this failed yada", err.Error()) err = NotFound("this failed %d", 1) require.Error(t, err) assert.EqualValues(t, http.StatusNotFound, err.Code()) - assert.EqualValues(t, "this failed 1", err.Error()) + assert.Equal(t, "this failed 1", err.Error()) err = NotFound("") require.Error(t, err) assert.EqualValues(t, http.StatusNotFound, err.Code()) - assert.EqualValues(t, "Not found", err.Error()) + assert.Equal(t, "Not found", err.Error()) err = NotImplemented("not implemented") require.Error(t, err) assert.EqualValues(t, http.StatusNotImplemented, err.Code()) - assert.EqualValues(t, "not implemented", err.Error()) + assert.Equal(t, "not implemented", err.Error()) err = MethodNotAllowed("GET", []string{"POST", "PUT"}) require.Error(t, err) assert.EqualValues(t, http.StatusMethodNotAllowed, err.Code()) - assert.EqualValues(t, "method GET is not allowed, but [POST,PUT] are", err.Error()) + assert.Equal(t, "method GET is not allowed, but [POST,PUT] are", err.Error()) err = InvalidContentType("application/saml", []string{"application/json", "application/x-yaml"}) require.Error(t, err) assert.EqualValues(t, http.StatusUnsupportedMediaType, err.Code()) - assert.EqualValues(t, "unsupported media type \"application/saml\", only [application/json application/x-yaml] are allowed", err.Error()) + assert.Equal(t, "unsupported media type \"application/saml\", only [application/json application/x-yaml] are allowed", err.Error()) err = InvalidResponseFormat("application/saml", []string{"application/json", "application/x-yaml"}) require.Error(t, err) assert.EqualValues(t, http.StatusNotAcceptable, err.Code()) - assert.EqualValues(t, "unsupported media type requested, only [application/json application/x-yaml] are available", err.Error()) + assert.Equal(t, "unsupported media type requested, only [application/json application/x-yaml] are available", err.Error()) } func TestValidateName(t *testing.T) { @@ -245,26 +245,26 @@ func TestValidateName(t *testing.T) { // unchanged vv := v.ValidateName("") - assert.EqualValues(t, "myValidation", vv.Name) - assert.EqualValues(t, "myMessage", vv.message) + assert.Equal(t, "myValidation", vv.Name) + assert.Equal(t, "myMessage", vv.message) // forced vv = v.ValidateName("myNewName") - assert.EqualValues(t, "myNewName.myValidation", vv.Name) - assert.EqualValues(t, "myNewName.myMessage", vv.message) + assert.Equal(t, "myNewName.myValidation", vv.Name) + assert.Equal(t, "myNewName.myMessage", vv.message) v.Name = "" v.message = "myMessage" // unchanged vv = v.ValidateName("") - assert.EqualValues(t, "", vv.Name) - assert.EqualValues(t, "myMessage", vv.message) + assert.Empty(t, vv.Name) + assert.Equal(t, "myMessage", vv.message) // forced vv = v.ValidateName("myNewName") - assert.EqualValues(t, "myNewName", vv.Name) - assert.EqualValues(t, "myNewNamemyMessage", vv.message) + assert.Equal(t, "myNewName", vv.Name) + assert.Equal(t, "myNewNamemyMessage", vv.message) } func TestMarshalJSON(t *testing.T) { diff --git a/middleware.go b/middleware.go index 67f8038..1b9f3a9 100644 --- a/middleware.go +++ b/middleware.go @@ -35,7 +35,7 @@ func (v *APIVerificationFailed) Error() string { hasSpecMissing := len(v.MissingSpecification) > 0 if hasRegMissing { - buf.WriteString(fmt.Sprintf("missing [%s] %s registrations", strings.Join(v.MissingRegistration, ", "), v.Section)) + fmt.Fprintf(buf, "missing [%s] %s registrations", strings.Join(v.MissingRegistration, ", "), v.Section) } if hasRegMissing && hasSpecMissing { @@ -43,7 +43,7 @@ func (v *APIVerificationFailed) Error() string { } if hasSpecMissing { - buf.WriteString(fmt.Sprintf("missing from spec file [%s] %s", strings.Join(v.MissingSpecification, ", "), v.Section)) + fmt.Fprintf(buf, "missing from spec file [%s] %s", strings.Join(v.MissingSpecification, ", "), v.Section) } return buf.String() diff --git a/parsing.go b/parsing.go index ce1ef9c..34930c0 100644 --- a/parsing.go +++ b/parsing.go @@ -30,6 +30,24 @@ type ParseError struct { message string } +// NewParseError creates a new parse error +func NewParseError(name, in, value string, reason error) *ParseError { + var msg string + if in == "" { + msg = fmt.Sprintf(parseErrorTemplContentNoIn, name, value, reason) + } else { + msg = fmt.Sprintf(parseErrorTemplContent, name, in, value, reason) + } + return &ParseError{ + code: http.StatusBadRequest, + Name: name, + In: in, + Value: value, + Reason: reason, + message: msg, + } +} + func (e *ParseError) Error() string { return e.message } @@ -59,21 +77,3 @@ const ( parseErrorTemplContent = `parsing %s %s from %q failed, because %s` parseErrorTemplContentNoIn = `parsing %s from %q failed, because %s` ) - -// NewParseError creates a new parse error -func NewParseError(name, in, value string, reason error) *ParseError { - var msg string - if in == "" { - msg = fmt.Sprintf(parseErrorTemplContentNoIn, name, value, reason) - } else { - msg = fmt.Sprintf(parseErrorTemplContent, name, in, value, reason) - } - return &ParseError{ - code: http.StatusBadRequest, - Name: name, - In: in, - Value: value, - Reason: reason, - message: msg, - } -}