Skip to content

Commit 4f6b6fa

Browse files
committed
refactor: minor cleanup (errors.Is/As, unused params)
1 parent 9500f27 commit 4f6b6fa

File tree

11 files changed

+24
-21
lines changed

11 files changed

+24
-21
lines changed

bind_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ func testBindArrayOkay(t *testing.T, r io.Reader, query url.Values, ctype string
725725
rec := httptest.NewRecorder()
726726
c := e.NewContext(req, rec)
727727
req.Header.Set(HeaderContentType, ctype)
728-
u := []user{}
728+
var u []user
729729
err := c.Bind(&u)
730730
if assert.NoError(t, err) {
731731
assert.Equal(t, 1, len(u))

context_generic.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
package echo
55

6-
import "errors"
6+
import (
7+
"errors"
8+
)
79

810
// ErrNonExistentKey is error that is returned when key does not exist
911
var ErrNonExistentKey = errors.New("non existent key")
@@ -36,7 +38,7 @@ func ContextGet[T any](c *Context, key string) (T, error) {
3638
// is missing. Returns ErrInvalidKeyType error if the value is not castable to type T.
3739
func ContextGetOr[T any](c *Context, key string, defaultValue T) (T, error) {
3840
typed, err := ContextGet[T](c, key)
39-
if err == ErrNonExistentKey {
41+
if errors.Is(err, ErrNonExistentKey) {
4042
return defaultValue, nil
4143
}
4244
return typed, err

echotest/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (conf ContextConfig) ToContextRecorder(t *testing.T) (*echo.Context, *httpt
9595
conf.Request.Header = conf.Headers
9696
}
9797
if len(conf.FormValues) > 0 {
98-
body := strings.NewReader(url.Values(conf.FormValues).Encode())
98+
body := strings.NewReader(conf.FormValues.Encode())
9999
conf.Request.Body = io.NopCloser(body)
100100
conf.Request.ContentLength = int64(body.Len())
101101

middleware/compress.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func (w *gzipResponseWriter) Flush() {
193193
}
194194

195195
if gw, ok := w.Writer.(*gzip.Writer); ok {
196-
gw.Flush()
196+
_ = gw.Flush()
197197
}
198198
_ = http.NewResponseController(w.ResponseWriter).Flush()
199199
}

middleware/cors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,11 @@ func (config CORSConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
286286
}, nil
287287
}
288288

289-
func (config CORSConfig) starAllowOriginFunc(c *echo.Context, origin string) (string, bool, error) {
289+
func (config CORSConfig) starAllowOriginFunc(_ *echo.Context, _ string) (string, bool, error) {
290290
return "*", true, nil
291291
}
292292

293-
func (config CORSConfig) defaultAllowOriginFunc(c *echo.Context, origin string) (string, bool, error) {
293+
func (config CORSConfig) defaultAllowOriginFunc(_ *echo.Context, origin string) (string, bool, error) {
294294
for _, allowedOrigin := range config.AllowOrigins {
295295
if strings.EqualFold(allowedOrigin, origin) {
296296
return allowedOrigin, true, nil

middleware/middleware.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func rewriteURL(rewriteRegex map[*regexp.Regexp]string, req *http.Request) error
8484
}
8585

8686
// DefaultSkipper returns false which processes the middleware.
87-
func DefaultSkipper(c *echo.Context) bool {
87+
func DefaultSkipper(_ *echo.Context) bool {
8888
return false
8989
}
9090

middleware/proxy.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func (b *commonBalancer) RemoveTarget(name string) bool {
234234
// Next randomly returns an upstream target.
235235
//
236236
// Note: `nil` is returned in case upstream target list is empty.
237-
func (b *randomBalancer) Next(c *echo.Context) (*ProxyTarget, error) {
237+
func (b *randomBalancer) Next(_ *echo.Context) (*ProxyTarget, error) {
238238
b.mutex.Lock()
239239
defer b.mutex.Unlock()
240240
if len(b.targets) == 0 {
@@ -314,7 +314,8 @@ func (config ProxyConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
314314
}
315315
if config.RetryFilter == nil {
316316
config.RetryFilter = func(c *echo.Context, e error) bool {
317-
if httpErr, ok := e.(*echo.HTTPError); ok {
317+
var httpErr *echo.HTTPError
318+
if errors.As(e, &httpErr) {
318319
return httpErr.Code == http.StatusBadGateway
319320
}
320321
return false

middleware/rate_limiter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func NewRateLimiterMemoryStoreWithConfig(config RateLimiterMemoryStoreConfig) (s
210210
store.expiresIn = DefaultRateLimiterMemoryStoreConfig.ExpiresIn
211211
}
212212
if config.Burst == 0 {
213-
store.burst = int(math.Max(1, math.Ceil(float64(config.Rate))))
213+
store.burst = int(math.Max(1, math.Ceil(config.Rate)))
214214
}
215215
store.visitors = make(map[string]*Visitor)
216216
store.timeNow = time.Now

renderer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ type TemplateRenderer struct {
2727
}
2828

2929
// Render renders the template with given data.
30-
func (t *TemplateRenderer) Render(c *Context, w io.Writer, name string, data any) error {
30+
func (t *TemplateRenderer) Render(_ *Context, w io.Writer, name string, data any) error {
3131
return t.Template.ExecuteTemplate(w, name, data)
3232
}

router.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -885,10 +885,10 @@ func (r *DefaultRouter) Route(c *Context) HandlerFunc {
885885
// NOTE: this case (backtracking from static node to previous any node) can not happen by current any matching logic. Any node is end of search currently
886886
//} else if nk == anyKind {
887887
// goto Any
888-
} else {
889-
// Not found (this should never be possible for static node we are looking currently)
890-
break
891888
}
889+
890+
// Not found (this should never be possible for static node we are looking currently)
891+
break
892892
}
893893

894894
// The full prefix has matched, remove the prefix from the remaining search
@@ -977,10 +977,10 @@ func (r *DefaultRouter) Route(c *Context) HandlerFunc {
977977
goto Param
978978
} else if nk == anyKind {
979979
goto Any
980-
} else {
981-
// Not found
982-
break
983980
}
981+
982+
// Not found
983+
break
984984
}
985985

986986
if currentNode == nil && previousBestMatchNode == nil {

0 commit comments

Comments
 (0)