Skip to content
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: 5 additions & 5 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func bindData(destination any, data map[string][]string, tag string, dataFiles m
typeField := typ.Field(i)
structField := val.Field(i)
if typeField.Anonymous {
if structField.Kind() == reflect.Ptr {
if structField.Kind() == reflect.Pointer {
structField = structField.Elem()
}
}
Expand Down Expand Up @@ -273,7 +273,7 @@ func bindData(destination any, data map[string][]string, tag string, dataFiles m
sliceOf := structField.Type().Elem().Kind()
numElems := len(inputValue)
slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
for j := 0; j < numElems; j++ {
for j := range numElems {
if err := setWithProperType(sliceOf, inputValue[j], slice.Index(j)); err != nil {
return err
}
Expand All @@ -297,7 +297,7 @@ func setWithProperType(valueKind reflect.Kind, val string, structField reflect.V
}

switch valueKind {
case reflect.Ptr:
case reflect.Pointer:
return setWithProperType(structField.Elem().Kind(), val, structField.Elem())
case reflect.Int:
return setIntField(val, 0, structField)
Expand Down Expand Up @@ -334,7 +334,7 @@ func setWithProperType(valueKind reflect.Kind, val string, structField reflect.V
}

func unmarshalInputsToField(valueKind reflect.Kind, values []string, field reflect.Value) (bool, error) {
if valueKind == reflect.Ptr {
if valueKind == reflect.Pointer {
if field.IsNil() {
field.Set(reflect.New(field.Type().Elem()))
}
Expand All @@ -350,7 +350,7 @@ func unmarshalInputsToField(valueKind reflect.Kind, values []string, field refle
}

func unmarshalInputToField(valueKind reflect.Kind, val string, field reflect.Value, formatTag string) (bool, error) {
if valueKind == reflect.Ptr {
if valueKind == reflect.Pointer {
if field.IsNil() {
field.Set(reflect.New(field.Type().Elem()))
}
Expand Down
4 changes: 2 additions & 2 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ func TestBindUnmarshalTypeError(t *testing.T) {

func TestBindSetWithProperType(t *testing.T) {
ts := new(bindTestStruct)
typ := reflect.TypeOf(ts).Elem()
typ := reflect.TypeFor[bindTestStruct]()
val := reflect.ValueOf(ts).Elem()
for i := 0; i < typ.NumField(); i++ {
typeField := typ.Field(i)
Expand All @@ -662,7 +662,7 @@ func TestBindSetWithProperType(t *testing.T) {
Bar bytes.Buffer
}
v := &foo{}
typ = reflect.TypeOf(v).Elem()
typ = reflect.TypeFor[foo]()
val = reflect.ValueOf(v).Elem()
assert.Error(t, setWithProperType(typ.Field(0).Type.Kind(), "5", val.Field(0)))
}
Expand Down
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func TestContextStream(t *testing.T) {
r, w := io.Pipe()
go func() {
defer w.Close()
for i := 0; i < 3; i++ {
for i := range 3 {
fmt.Fprintf(w, "data: index %v\n\n", i)
time.Sleep(5 * time.Millisecond)
}
Expand Down
2 changes: 1 addition & 1 deletion echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ func (ce *customError) StatusCode() int {
}

func (ce *customError) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`{"x":"%v"}`, ce.Message)), nil
return fmt.Appendf(nil, `{"x":"%v"}`, ce.Message), nil
}

func (ce *customError) Error() string {
Expand Down
6 changes: 3 additions & 3 deletions middleware/basic_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ func (config BasicAuthConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
lastError = echo.ErrBadRequest.Wrap(errDecode)
continue
}
idx := bytes.IndexByte(b, ':')
if idx >= 0 {
valid, errValidate := config.Validator(c, string(b[:idx]), string(b[idx+1:]))
before, after, ok := bytes.Cut(b, []byte{':'})
if ok {
valid, errValidate := config.Validator(c, string(before), string(after))
if errValidate != nil {
lastError = errValidate
} else if valid {
Expand Down
2 changes: 1 addition & 1 deletion middleware/body_dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func TestBodyDump_ClientGetsFullResponse(t *testing.T) {

h := func(c *echo.Context) error {
// Write response in chunks to test incremental writes
for i := 0; i < 4; i++ {
for range 4 {
c.Response().Write([]byte(strings.Repeat("DATA", 125)))
}
return nil
Expand Down
5 changes: 2 additions & 3 deletions middleware/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"io"
"maps"
"math/rand"
"net"
"net/http"
Expand Down Expand Up @@ -330,9 +331,7 @@ func (config ProxyConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
if config.RegexRewrite == nil {
config.RegexRewrite = make(map[*regexp.Regexp]string)
}
for k, v := range rewriteRulesRegex(config.Rewrite) {
config.RegexRewrite[k] = v
}
maps.Copy(config.RegexRewrite, rewriteRulesRegex(config.Rewrite))
}

return func(next echo.HandlerFunc) echo.HandlerFunc {
Expand Down
8 changes: 3 additions & 5 deletions middleware/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,15 +676,13 @@ func TestProxyRetryWithBackendTimeout(t *testing.T) {
))

var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for range 20 {
wg.Go(func() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, 200, rec.Code)
}()
})
}

wg.Wait()
Expand Down
18 changes: 8 additions & 10 deletions middleware/rate_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func TestRateLimiterMemoryStore_FractionalRateDefaultBurst(t *testing.T) {

func generateAddressList(count int) []string {
addrs := make([]string, count)
for i := 0; i < count; i++ {
for i := range count {
addrs[i] = randomString(15)
}
return addrs
Expand All @@ -477,7 +477,7 @@ func run(wg *sync.WaitGroup, store RateLimiterStore, addrs []string, max int, b
func benchmarkStore(store RateLimiterStore, parallel int, max int, b *testing.B) {
addrs := generateAddressList(max)
wg := &sync.WaitGroup{}
for i := 0; i < parallel; i++ {
for range parallel {
wg.Add(1)
go run(wg, store, addrs, max, b)
}
Expand Down Expand Up @@ -553,11 +553,9 @@ func TestRateLimiterMemoryStore_ConcurrentAccess(t *testing.T) {
var wg sync.WaitGroup
var allowedCount, deniedCount int32

for i := 0; i < goroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < requestsPerGoroutine; j++ {
for range goroutines {
wg.Go(func() {
for range requestsPerGoroutine {
allowed, err := store.Allow("test-user")
assert.NoError(t, err)
if allowed {
Expand All @@ -567,7 +565,7 @@ func TestRateLimiterMemoryStore_ConcurrentAccess(t *testing.T) {
}
time.Sleep(time.Millisecond)
}
}()
})
}

wg.Wait()
Expand Down Expand Up @@ -598,11 +596,11 @@ func TestRateLimiterMemoryStore_RaceDetection(t *testing.T) {
var wg sync.WaitGroup
identifiers := []string{"user1", "user2", "user3", "user4", "user5"}

for i := 0; i < goroutines; i++ {
for i := range goroutines {
wg.Add(1)
go func(routineID int) {
defer wg.Done()
for j := 0; j < requestsPerGoroutine; j++ {
for range requestsPerGoroutine {
identifier := identifiers[routineID%len(identifiers)]
_, err := store.Allow(identifier)
assert.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions middleware/request_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ func TestRequestLoggerOK(t *testing.T) {
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)

logAttrs := map[string]interface{}{}
logAttrs := map[string]any{}
assert.NoError(t, json.Unmarshal(buf.Bytes(), &logAttrs))
logAttrs["latency"] = 123
logAttrs["time"] = "x"

expect := map[string]interface{}{
expect := map[string]any{
"level": "INFO",
"msg": "REQUEST",
"method": "POST",
Expand Down Expand Up @@ -88,12 +88,12 @@ func TestRequestLoggerError(t *testing.T) {
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)

logAttrs := map[string]interface{}{}
logAttrs := map[string]any{}
assert.NoError(t, json.Unmarshal(buf.Bytes(), &logAttrs))
logAttrs["latency"] = 123
logAttrs["time"] = "x"

expect := map[string]interface{}{
expect := map[string]any{
"level": "ERROR",
"msg": "REQUEST_ERROR",
"method": "GET",
Expand Down
5 changes: 2 additions & 3 deletions middleware/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package middleware

import (
"errors"
"maps"
"regexp"

"github.com/labstack/echo/v5"
Expand Down Expand Up @@ -61,9 +62,7 @@ func (config RewriteConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
if config.RegexRules == nil {
config.RegexRules = make(map[*regexp.Regexp]string)
}
for k, v := range rewriteRulesRegex(config.Rules) {
config.RegexRules[k] = v
}
maps.Copy(config.RegexRules, rewriteRulesRegex(config.Rules))

return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c *echo.Context) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion middleware/rewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestRewriteWithConfigPreMiddleware_Issue1143(t *testing.T) {
return c.String(http.StatusOK, "eng")
})

for i := 0; i < 100; i++ {
for range 100 {
req := httptest.NewRequest(http.MethodGet, "/api/v1/mgmt/proj/test/agt", nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
Expand Down
2 changes: 1 addition & 1 deletion middleware/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestRandomStringBias(t *testing.T) {
counts := make(map[rune]int)
var count int64

for i := 0; i < loop; i++ {
for range loop {
s := randomString(slen)
require.Equal(t, slen, len(s))
for _, b := range s {
Expand Down
Loading
Loading