Skip to content

Commit 03d9298

Browse files
kumapower17aldas
authored andcommitted
refactor: modernize code usage using gofix
Modernizes the codebase using the Go 1.26 gofix tool to adopt newer idioms and library features while maintaining compatibility with the current toolchain.
1 parent 46f2b8b commit 03d9298

15 files changed

Lines changed: 60 additions & 72 deletions

bind.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func bindData(destination any, data map[string][]string, tag string, dataFiles m
188188
typeField := typ.Field(i)
189189
structField := val.Field(i)
190190
if typeField.Anonymous {
191-
if structField.Kind() == reflect.Ptr {
191+
if structField.Kind() == reflect.Pointer {
192192
structField = structField.Elem()
193193
}
194194
}
@@ -273,7 +273,7 @@ func bindData(destination any, data map[string][]string, tag string, dataFiles m
273273
sliceOf := structField.Type().Elem().Kind()
274274
numElems := len(inputValue)
275275
slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
276-
for j := 0; j < numElems; j++ {
276+
for j := range numElems {
277277
if err := setWithProperType(sliceOf, inputValue[j], slice.Index(j)); err != nil {
278278
return err
279279
}
@@ -297,7 +297,7 @@ func setWithProperType(valueKind reflect.Kind, val string, structField reflect.V
297297
}
298298

299299
switch valueKind {
300-
case reflect.Ptr:
300+
case reflect.Pointer:
301301
return setWithProperType(structField.Elem().Kind(), val, structField.Elem())
302302
case reflect.Int:
303303
return setIntField(val, 0, structField)
@@ -334,7 +334,7 @@ func setWithProperType(valueKind reflect.Kind, val string, structField reflect.V
334334
}
335335

336336
func unmarshalInputsToField(valueKind reflect.Kind, values []string, field reflect.Value) (bool, error) {
337-
if valueKind == reflect.Ptr {
337+
if valueKind == reflect.Pointer {
338338
if field.IsNil() {
339339
field.Set(reflect.New(field.Type().Elem()))
340340
}
@@ -350,7 +350,7 @@ func unmarshalInputsToField(valueKind reflect.Kind, values []string, field refle
350350
}
351351

352352
func unmarshalInputToField(valueKind reflect.Kind, val string, field reflect.Value, formatTag string) (bool, error) {
353-
if valueKind == reflect.Ptr {
353+
if valueKind == reflect.Pointer {
354354
if field.IsNil() {
355355
field.Set(reflect.New(field.Type().Elem()))
356356
}

bind_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ func TestBindUnmarshalTypeError(t *testing.T) {
641641

642642
func TestBindSetWithProperType(t *testing.T) {
643643
ts := new(bindTestStruct)
644-
typ := reflect.TypeOf(ts).Elem()
644+
typ := reflect.TypeFor[bindTestStruct]()
645645
val := reflect.ValueOf(ts).Elem()
646646
for i := 0; i < typ.NumField(); i++ {
647647
typeField := typ.Field(i)
@@ -662,7 +662,7 @@ func TestBindSetWithProperType(t *testing.T) {
662662
Bar bytes.Buffer
663663
}
664664
v := &foo{}
665-
typ = reflect.TypeOf(v).Elem()
665+
typ = reflect.TypeFor[foo]()
666666
val = reflect.ValueOf(v).Elem()
667667
assert.Error(t, setWithProperType(typ.Field(0).Type.Kind(), "5", val.Field(0)))
668668
}

context_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func TestContextStream(t *testing.T) {
178178
r, w := io.Pipe()
179179
go func() {
180180
defer w.Close()
181-
for i := 0; i < 3; i++ {
181+
for i := range 3 {
182182
fmt.Fprintf(w, "data: index %v\n\n", i)
183183
time.Sleep(5 * time.Millisecond)
184184
}

echo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ func (ce *customError) StatusCode() int {
11091109
}
11101110

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

11151115
func (ce *customError) Error() string {

middleware/basic_auth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ func (config BasicAuthConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
133133
lastError = echo.ErrBadRequest.Wrap(errDecode)
134134
continue
135135
}
136-
idx := bytes.IndexByte(b, ':')
137-
if idx >= 0 {
138-
valid, errValidate := config.Validator(c, string(b[:idx]), string(b[idx+1:]))
136+
before, after, ok := bytes.Cut(b, []byte{':'})
137+
if ok {
138+
valid, errValidate := config.Validator(c, string(before), string(after))
139139
if errValidate != nil {
140140
lastError = errValidate
141141
} else if valid {

middleware/body_dump_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ func TestBodyDump_ClientGetsFullResponse(t *testing.T) {
381381

382382
h := func(c *echo.Context) error {
383383
// Write response in chunks to test incremental writes
384-
for i := 0; i < 4; i++ {
384+
for range 4 {
385385
c.Response().Write([]byte(strings.Repeat("DATA", 125)))
386386
}
387387
return nil

middleware/proxy.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"io"
12+
"maps"
1213
"math/rand"
1314
"net"
1415
"net/http"
@@ -330,9 +331,7 @@ func (config ProxyConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
330331
if config.RegexRewrite == nil {
331332
config.RegexRewrite = make(map[*regexp.Regexp]string)
332333
}
333-
for k, v := range rewriteRulesRegex(config.Rewrite) {
334-
config.RegexRewrite[k] = v
335-
}
334+
maps.Copy(config.RegexRewrite, rewriteRulesRegex(config.Rewrite))
336335
}
337336

338337
return func(next echo.HandlerFunc) echo.HandlerFunc {

middleware/proxy_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -676,15 +676,13 @@ func TestProxyRetryWithBackendTimeout(t *testing.T) {
676676
))
677677

678678
var wg sync.WaitGroup
679-
for i := 0; i < 20; i++ {
680-
wg.Add(1)
681-
go func() {
682-
defer wg.Done()
679+
for range 20 {
680+
wg.Go(func() {
683681
req := httptest.NewRequest(http.MethodGet, "/", nil)
684682
rec := httptest.NewRecorder()
685683
e.ServeHTTP(rec, req)
686684
assert.Equal(t, 200, rec.Code)
687-
}()
685+
})
688686
}
689687

690688
wg.Wait()

middleware/rate_limiter_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ func TestRateLimiterMemoryStore_FractionalRateDefaultBurst(t *testing.T) {
461461

462462
func generateAddressList(count int) []string {
463463
addrs := make([]string, count)
464-
for i := 0; i < count; i++ {
464+
for i := range count {
465465
addrs[i] = randomString(15)
466466
}
467467
return addrs
@@ -477,7 +477,7 @@ func run(wg *sync.WaitGroup, store RateLimiterStore, addrs []string, max int, b
477477
func benchmarkStore(store RateLimiterStore, parallel int, max int, b *testing.B) {
478478
addrs := generateAddressList(max)
479479
wg := &sync.WaitGroup{}
480-
for i := 0; i < parallel; i++ {
480+
for range parallel {
481481
wg.Add(1)
482482
go run(wg, store, addrs, max, b)
483483
}
@@ -553,11 +553,9 @@ func TestRateLimiterMemoryStore_ConcurrentAccess(t *testing.T) {
553553
var wg sync.WaitGroup
554554
var allowedCount, deniedCount int32
555555

556-
for i := 0; i < goroutines; i++ {
557-
wg.Add(1)
558-
go func() {
559-
defer wg.Done()
560-
for j := 0; j < requestsPerGoroutine; j++ {
556+
for range goroutines {
557+
wg.Go(func() {
558+
for range requestsPerGoroutine {
561559
allowed, err := store.Allow("test-user")
562560
assert.NoError(t, err)
563561
if allowed {
@@ -567,7 +565,7 @@ func TestRateLimiterMemoryStore_ConcurrentAccess(t *testing.T) {
567565
}
568566
time.Sleep(time.Millisecond)
569567
}
570-
}()
568+
})
571569
}
572570

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

601-
for i := 0; i < goroutines; i++ {
599+
for i := range goroutines {
602600
wg.Add(1)
603601
go func(routineID int) {
604602
defer wg.Done()
605-
for j := 0; j < requestsPerGoroutine; j++ {
603+
for range requestsPerGoroutine {
606604
identifier := identifiers[routineID%len(identifiers)]
607605
_, err := store.Allow(identifier)
608606
assert.NoError(t, err)

middleware/request_logger_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ func TestRequestLoggerOK(t *testing.T) {
4646
rec := httptest.NewRecorder()
4747
e.ServeHTTP(rec, req)
4848

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

54-
expect := map[string]interface{}{
54+
expect := map[string]any{
5555
"level": "INFO",
5656
"msg": "REQUEST",
5757
"method": "POST",
@@ -88,12 +88,12 @@ func TestRequestLoggerError(t *testing.T) {
8888
rec := httptest.NewRecorder()
8989
e.ServeHTTP(rec, req)
9090

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

96-
expect := map[string]interface{}{
96+
expect := map[string]any{
9797
"level": "ERROR",
9898
"msg": "REQUEST_ERROR",
9999
"method": "GET",

0 commit comments

Comments
 (0)