Skip to content

Commit 6b618ae

Browse files
authored
chore: modernize code with go fix (#103)
1 parent 0515e52 commit 6b618ae

10 files changed

Lines changed: 22 additions & 47 deletions

File tree

clientip/clientip_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ func TestAddressesAndRangesToIPNets(t *testing.T) {
311311
}
312312

313313
require.Equal(t, len(tt.want), len(got))
314-
for i := 0; i < len(got); i++ {
314+
for i := range got {
315315
if got[i].String() != tt.want[i] {
316316
assert.Equal(t, tt.want[i], got[i].String())
317317
}

context_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,6 @@ func TestWrapF(t *testing.T) {
485485
}
486486

487487
for _, tc := range cases {
488-
tc := tc
489488

490489
t.Run(tc.name, func(t *testing.T) {
491490
t.Parallel()
@@ -554,7 +553,6 @@ func TestWrapH(t *testing.T) {
554553
}
555554

556555
for _, tc := range cases {
557-
tc := tc
558556

559557
t.Run(tc.name, func(t *testing.T) {
560558
t.Parallel()

fox_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4296,7 +4296,7 @@ func TestRouter_ServeHTTP_Concurrent(t *testing.T) {
42964296
var wg sync.WaitGroup
42974297
wg.Add(300)
42984298
start, wait := atomicSync()
4299-
for i := 0; i < 100; i++ {
4299+
for range 100 {
43004300
go func() {
43014301
defer wg.Done()
43024302
wait()

internal/simplelru/lru.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,7 @@ func (c *LRU[K, V]) Cap() int {
153153

154154
// Resize changes the cache size.
155155
func (c *LRU[K, V]) Resize(size int) (evicted int) {
156-
diff := c.Len() - size
157-
if diff < 0 {
158-
diff = 0
159-
}
156+
diff := max(c.Len()-size, 0)
160157
for i := 0; i < diff; i++ {
161158
c.removeOldest()
162159
}

internal/simplelru/lru_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestLRU_Basics(t *testing.T) {
2222
t.Fatalf("err: %v", err)
2323
}
2424

25-
for i := 0; i < 256; i++ {
25+
for i := range 256 {
2626
l.Add(i, i)
2727
}
2828
if l.Len() != 128 {
@@ -46,7 +46,7 @@ func TestLRU_Basics(t *testing.T) {
4646
t.Fatalf("bad value: %v", v)
4747
}
4848
}
49-
for i := 0; i < 128; i++ {
49+
for i := range 128 {
5050
if _, ok := l.Get(i); ok {
5151
t.Fatalf("should be evicted")
5252
}
@@ -90,7 +90,7 @@ func TestLRU_GetOldest_RemoveOldest(t *testing.T) {
9090
if err != nil {
9191
t.Fatalf("err: %v", err)
9292
}
93-
for i := 0; i < 256; i++ {
93+
for i := range 256 {
9494
l.Add(i, i)
9595
}
9696
k, _, ok := l.GetOldest()

internal/slogpretty/handler.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"log/slog"
1212
"os"
13+
"strings"
1314
"sync"
1415
"time"
1516

@@ -114,15 +115,15 @@ func (h *Handler) Handle(_ context.Context, record slog.Record) error {
114115
}
115116
buf = append(buf, " | "...)
116117

117-
lastGroup := ""
118+
var lastGroup strings.Builder
118119
for _, goa := range h.Goa {
119120
switch {
120121
case goa.group != "":
121-
lastGroup += goa.group + "."
122+
lastGroup.WriteString(goa.group + ".")
122123
default:
123124
attr := goa.attr
124-
if lastGroup != "" {
125-
attr.Key = lastGroup + attr.Key
125+
if lastGroup.String() != "" {
126+
attr.Key = lastGroup.String() + attr.Key
126127
}
127128

128129
buf = appendAttr(record.Level, buf, attr)
@@ -132,8 +133,8 @@ func (h *Handler) Handle(_ context.Context, record slog.Record) error {
132133
// If there are additional attributes, append them to the log record.
133134
if record.NumAttrs() > 0 {
134135
record.Attrs(func(attr slog.Attr) bool {
135-
if lastGroup != "" {
136-
attr.Key = lastGroup + attr.Key
136+
if lastGroup.String() != "" {
137+
attr.Key = lastGroup.String() + attr.Key
137138
}
138139
buf = appendAttr(record.Level, buf, attr)
139140

matcher.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net"
77
"net/http"
88
"regexp"
9+
"slices"
910
"strings"
1011

1112
"github.com/fox-toolkit/fox/internal/netutil"
@@ -59,12 +60,7 @@ func (m QueryMatcher) Match(c RequestContext) bool {
5960
if len(values) == 0 {
6061
return false
6162
}
62-
for _, v := range values {
63-
if v == m.value {
64-
return true
65-
}
66-
}
67-
return false
63+
return slices.Contains(values, m.value)
6864
}
6965

7066
// Equal reports whether matcher is a [QueryMatcher] with the same key and value.
@@ -129,12 +125,7 @@ func (m QueryRegexpMatcher) Match(c RequestContext) bool {
129125
if len(values) == 0 {
130126
return false
131127
}
132-
for _, v := range values {
133-
if m.regex.MatchString(v) {
134-
return true
135-
}
136-
}
137-
return false
128+
return slices.ContainsFunc(values, m.regex.MatchString)
138129
}
139130

140131
// Equal reports whether matcher is a [QueryRegexpMatcher] with the same key and regular expression source.
@@ -182,12 +173,7 @@ func (m HeaderMatcher) Match(c RequestContext) bool {
182173
if len(values) == 0 {
183174
return false
184175
}
185-
for _, v := range values {
186-
if v == m.value {
187-
return true
188-
}
189-
}
190-
return false
176+
return slices.Contains(values, m.value)
191177
}
192178

193179
// Equal reports whether matcher is a [HeaderMatcher] with the same key and value.
@@ -247,12 +233,7 @@ func (m HeaderRegexpMatcher) Match(c RequestContext) bool {
247233
if len(values) == 0 {
248234
return false
249235
}
250-
for _, v := range values {
251-
if m.regex.MatchString(v) {
252-
return true
253-
}
254-
}
255-
return false
236+
return slices.ContainsFunc(values, m.regex.MatchString)
256237
}
257238

258239
// Equal reports whether matcher is a [HeaderRegexpMatcher] with the same key and regular expression source.

params_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ func TestParamsFromContext(t *testing.T) {
101101
}
102102

103103
for _, tc := range cases {
104-
tc := tc
105104

106105
t.Run(tc.name, func(t *testing.T) {
107106
t.Parallel()

path_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ func TestCleanPath_Mallocs(t *testing.T) {
8585
}
8686

8787
for _, test := range cleanTests {
88-
test := test
8988
allocs := testing.AllocsPerRun(100, func() { CleanPath(test.result) })
9089
if allocs > 0 {
9190
t.Errorf("CleanPath(%q): %v allocs, want zero", test.result, allocs)

recovery.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ func recovery(logger *slog.Logger, c *Context, handle RecoveryFunc) {
8383
sb.Write(before)
8484
for header := range iterutil.SplitBytesSeq(after, reqHeaderSep) {
8585
sb.Write(reqHeaderSep)
86-
idx := bytes.IndexByte(header, ':')
87-
if idx < 0 {
86+
before0, _, ok := bytes.Cut(header, []byte{':'})
87+
if !ok {
8888
continue
8989
}
90-
if slices.Contains(blacklistedHeader, string(header[:idx])) {
91-
sb.Write(header[:idx])
90+
if slices.Contains(blacklistedHeader, string(before0)) {
91+
sb.Write(before0)
9292
sb.WriteString(": <redacted>")
9393
continue
9494
}

0 commit comments

Comments
 (0)