Skip to content

Commit 0143b9d

Browse files
authored
fix(lint): resolve staticcheck issues and improve code quality (#2941)
* refactor: apply De Morgan’s law to boolean expressions
1 parent 607f385 commit 0143b9d

File tree

7 files changed

+45
-37
lines changed

7 files changed

+45
-37
lines changed

bind.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func bindData(destination any, data map[string][]string, tag string, dataFiles m
155155
isElemInterface := k == reflect.Interface
156156
isElemString := k == reflect.String
157157
isElemSliceOfStrings := k == reflect.Slice && typ.Elem().Elem().Kind() == reflect.String
158-
if !(isElemSliceOfStrings || isElemString || isElemInterface) {
158+
if !isElemSliceOfStrings && !isElemString && !isElemInterface {
159159
return nil
160160
}
161161
if val.IsNil() {

binder_generic.go

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ const (
4949
// It returns the typed value and an error if binding fails. Returns ErrNonExistentKey if parameter not found.
5050
//
5151
// Empty String Handling:
52-
// If the parameter exists but has an empty value, the zero value of type T is returned
53-
// with no error. For example, a path parameter with value "" returns (0, nil) for int types.
54-
// This differs from standard library behavior where parsing empty strings returns errors.
55-
// To treat empty values as errors, validate the result separately or check the raw value.
52+
//
53+
// If the parameter exists but has an empty value, the zero value of type T is returned
54+
// with no error. For example, a path parameter with value "" returns (0, nil) for int types.
55+
// This differs from standard library behavior where parsing empty strings returns errors.
56+
// To treat empty values as errors, validate the result separately or check the raw value.
5657
//
5758
// See ParseValue for supported types and options
5859
func PathParam[T any](c *Context, paramName string, opts ...any) (T, error) {
@@ -74,10 +75,11 @@ func PathParam[T any](c *Context, paramName string, opts ...any) (T, error) {
7475
// Returns an error only if parsing fails (e.g., "abc" for int type).
7576
//
7677
// Example:
77-
// id, err := echo.PathParamOr[int](c, "id", 0)
78-
// // If "id" is missing: returns (0, nil)
79-
// // If "id" is "123": returns (123, nil)
80-
// // If "id" is "abc": returns (0, BindingError)
78+
//
79+
// id, err := echo.PathParamOr[int](c, "id", 0)
80+
// // If "id" is missing: returns (0, nil)
81+
// // If "id" is "123": returns (123, nil)
82+
// // If "id" is "abc": returns (0, BindingError)
8183
//
8284
// See ParseValue for supported types and options
8385
func PathParamOr[T any](c *Context, paramName string, defaultValue T, opts ...any) (T, error) {
@@ -97,10 +99,11 @@ func PathParamOr[T any](c *Context, paramName string, defaultValue T, opts ...an
9799
// It returns the typed value and an error if binding fails. Returns ErrNonExistentKey if parameter not found.
98100
//
99101
// Empty String Handling:
100-
// If the parameter exists but has an empty value (?key=), the zero value of type T is returned
101-
// with no error. For example, "?count=" returns (0, nil) for int types.
102-
// This differs from standard library behavior where parsing empty strings returns errors.
103-
// To treat empty values as errors, validate the result separately or check the raw value.
102+
//
103+
// If the parameter exists but has an empty value (?key=), the zero value of type T is returned
104+
// with no error. For example, "?count=" returns (0, nil) for int types.
105+
// This differs from standard library behavior where parsing empty strings returns errors.
106+
// To treat empty values as errors, validate the result separately or check the raw value.
104107
//
105108
// Behavior Summary:
106109
// - Missing key (?other=value): returns (zero, ErrNonExistentKey)
@@ -131,10 +134,11 @@ func QueryParam[T any](c *Context, key string, opts ...any) (T, error) {
131134
// Returns an error only if parsing fails (e.g., "abc" for int type).
132135
//
133136
// Example:
134-
// page, err := echo.QueryParamOr[int](c, "page", 1)
135-
// // If "page" is missing: returns (1, nil)
136-
// // If "page" is "5": returns (5, nil)
137-
// // If "page" is "abc": returns (1, BindingError)
137+
//
138+
// page, err := echo.QueryParamOr[int](c, "page", 1)
139+
// // If "page" is missing: returns (1, nil)
140+
// // If "page" is "5": returns (5, nil)
141+
// // If "page" is "abc": returns (1, BindingError)
138142
//
139143
// See ParseValue for supported types and options
140144
func QueryParamOr[T any](c *Context, key string, defaultValue T, opts ...any) (T, error) {
@@ -175,10 +179,11 @@ func QueryParams[T any](c *Context, key string, opts ...any) ([]T, error) {
175179
// Returns an error only if parsing any value fails.
176180
//
177181
// Example:
178-
// ids, err := echo.QueryParamsOr[int](c, "ids", []int{})
179-
// // If "ids" is missing: returns ([], nil)
180-
// // If "ids" is "1&ids=2": returns ([1, 2], nil)
181-
// // If "ids" contains "abc": returns ([], BindingError)
182+
//
183+
// ids, err := echo.QueryParamsOr[int](c, "ids", []int{})
184+
// // If "ids" is missing: returns ([], nil)
185+
// // If "ids" is "1&ids=2": returns ([1, 2], nil)
186+
// // If "ids" contains "abc": returns ([], BindingError)
182187
//
183188
// See ParseValues for supported types and options
184189
func QueryParamsOr[T any](c *Context, key string, defaultValue []T, opts ...any) ([]T, error) {
@@ -198,10 +203,11 @@ func QueryParamsOr[T any](c *Context, key string, defaultValue []T, opts ...any)
198203
// It returns the typed value and an error if binding fails. Returns ErrNonExistentKey if parameter not found.
199204
//
200205
// Empty String Handling:
201-
// If the form field exists but has an empty value, the zero value of type T is returned
202-
// with no error. For example, an empty form field returns (0, nil) for int types.
203-
// This differs from standard library behavior where parsing empty strings returns errors.
204-
// To treat empty values as errors, validate the result separately or check the raw value.
206+
//
207+
// If the form field exists but has an empty value, the zero value of type T is returned
208+
// with no error. For example, an empty form field returns (0, nil) for int types.
209+
// This differs from standard library behavior where parsing empty strings returns errors.
210+
// To treat empty values as errors, validate the result separately or check the raw value.
205211
//
206212
// See ParseValue for supported types and options
207213
func FormValue[T any](c *Context, key string, opts ...any) (T, error) {
@@ -232,10 +238,11 @@ func FormValue[T any](c *Context, key string, opts ...any) (T, error) {
232238
// Returns an error only if parsing fails or form parsing errors occur.
233239
//
234240
// Example:
235-
// limit, err := echo.FormValueOr[int](c, "limit", 100)
236-
// // If "limit" is missing: returns (100, nil)
237-
// // If "limit" is "50": returns (50, nil)
238-
// // If "limit" is "abc": returns (100, BindingError)
241+
//
242+
// limit, err := echo.FormValueOr[int](c, "limit", 100)
243+
// // If "limit" is missing: returns (100, nil)
244+
// // If "limit" is "50": returns (50, nil)
245+
// // If "limit" is "abc": returns (100, BindingError)
239246
//
240247
// See ParseValue for supported types and options
241248
func FormValueOr[T any](c *Context, key string, defaultValue T, opts ...any) (T, error) {
@@ -284,9 +291,10 @@ func FormValues[T any](c *Context, key string, opts ...any) ([]T, error) {
284291
// Returns an error only if parsing any value fails or form parsing errors occur.
285292
//
286293
// Example:
287-
// tags, err := echo.FormValuesOr[string](c, "tags", []string{})
288-
// // If "tags" is missing: returns ([], nil)
289-
// // If form parsing fails: returns (nil, error)
294+
//
295+
// tags, err := echo.FormValuesOr[string](c, "tags", []string{})
296+
// // If "tags" is missing: returns ([], nil)
297+
// // If form parsing fails: returns (nil, error)
290298
//
291299
// See ParseValues for supported types and options
292300
func FormValuesOr[T any](c *Context, key string, defaultValue []T, opts ...any) ([]T, error) {

middleware/basic_auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (config BasicAuthConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
121121
if i >= limit {
122122
break
123123
}
124-
if !(len(auth) > l+1 && strings.EqualFold(auth[:l], basic)) {
124+
if len(auth) <= l+1 || !strings.EqualFold(auth[:l], basic) {
125125
continue
126126
}
127127
i++

middleware/static.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func (config StaticConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
250250
}
251251

252252
var he echo.HTTPStatusCoder
253-
if !(errors.As(err, &he) && config.HTML5 && he.StatusCode() == http.StatusNotFound) {
253+
if !errors.As(err, &he) || !config.HTML5 || he.StatusCode() != http.StatusNotFound {
254254
return err
255255
}
256256
// is case HTML5 mode is enabled + echo 404 we serve index to the client

response.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func UnwrapResponse(rw http.ResponseWriter) (*Response, error) {
136136
type delayedStatusWriter struct {
137137
http.ResponseWriter
138138
committed bool
139-
status int
139+
status int
140140
}
141141

142142
func (w *delayedStatusWriter) WriteHeader(statusCode int) {

route.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (r RouteInfo) Reverse(pathValues ...any) string {
8585
// in case of `*` wildcard or `:` (unescaped colon) param we replace everything till next slash or end of path
8686
for ; i < l && r.Path[i] != '/'; i++ {
8787
}
88-
uri.WriteString(fmt.Sprintf("%v", pathValues[n]))
88+
fmt.Fprintf(uri, "%v", pathValues[n])
8989
n++
9090
}
9191
if i < l {

router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ func (r *DefaultRouter) Route(c *Context) HandlerFunc {
995995
var rInfo *RouteInfo
996996
if matchedRouteMethod != nil {
997997
rHandler = matchedRouteMethod.handler
998-
rPath = matchedRouteMethod.RouteInfo.Path
998+
rPath = matchedRouteMethod.Path
999999
rInfo = matchedRouteMethod.RouteInfo
10001000
} else {
10011001
// use previous match as basis. although we have no matching handler we have path match.

0 commit comments

Comments
 (0)