From 23e9ba1e06ea8f0e0bf1b0de589d3f7044c5c5ad Mon Sep 17 00:00:00 2001 From: tigerwill90 <26261762+tigerwill90@users.noreply.github.com> Date: Tue, 5 May 2026 10:24:24 +0200 Subject: [PATCH] chore: modernize code with go fix --- clientip/clientip_test.go | 2 +- context_test.go | 2 -- fox_test.go | 2 +- internal/simplelru/lru.go | 5 +---- internal/simplelru/lru_test.go | 6 +++--- internal/slogpretty/handler.go | 13 +++++++------ matcher.go | 29 +++++------------------------ params_test.go | 1 - path_test.go | 1 - recovery.go | 8 ++++---- 10 files changed, 22 insertions(+), 47 deletions(-) diff --git a/clientip/clientip_test.go b/clientip/clientip_test.go index 2168f893..7bb893be 100644 --- a/clientip/clientip_test.go +++ b/clientip/clientip_test.go @@ -311,7 +311,7 @@ func TestAddressesAndRangesToIPNets(t *testing.T) { } require.Equal(t, len(tt.want), len(got)) - for i := 0; i < len(got); i++ { + for i := range got { if got[i].String() != tt.want[i] { assert.Equal(t, tt.want[i], got[i].String()) } diff --git a/context_test.go b/context_test.go index 53ea54e9..166e73f4 100644 --- a/context_test.go +++ b/context_test.go @@ -485,7 +485,6 @@ func TestWrapF(t *testing.T) { } for _, tc := range cases { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() @@ -554,7 +553,6 @@ func TestWrapH(t *testing.T) { } for _, tc := range cases { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() diff --git a/fox_test.go b/fox_test.go index 9783cdf9..2d38a3c9 100644 --- a/fox_test.go +++ b/fox_test.go @@ -4296,7 +4296,7 @@ func TestRouter_ServeHTTP_Concurrent(t *testing.T) { var wg sync.WaitGroup wg.Add(300) start, wait := atomicSync() - for i := 0; i < 100; i++ { + for range 100 { go func() { defer wg.Done() wait() diff --git a/internal/simplelru/lru.go b/internal/simplelru/lru.go index d2bbb228..55f49582 100644 --- a/internal/simplelru/lru.go +++ b/internal/simplelru/lru.go @@ -153,10 +153,7 @@ func (c *LRU[K, V]) Cap() int { // Resize changes the cache size. func (c *LRU[K, V]) Resize(size int) (evicted int) { - diff := c.Len() - size - if diff < 0 { - diff = 0 - } + diff := max(c.Len()-size, 0) for i := 0; i < diff; i++ { c.removeOldest() } diff --git a/internal/simplelru/lru_test.go b/internal/simplelru/lru_test.go index 65e34548..e473e4b0 100644 --- a/internal/simplelru/lru_test.go +++ b/internal/simplelru/lru_test.go @@ -22,7 +22,7 @@ func TestLRU_Basics(t *testing.T) { t.Fatalf("err: %v", err) } - for i := 0; i < 256; i++ { + for i := range 256 { l.Add(i, i) } if l.Len() != 128 { @@ -46,7 +46,7 @@ func TestLRU_Basics(t *testing.T) { t.Fatalf("bad value: %v", v) } } - for i := 0; i < 128; i++ { + for i := range 128 { if _, ok := l.Get(i); ok { t.Fatalf("should be evicted") } @@ -90,7 +90,7 @@ func TestLRU_GetOldest_RemoveOldest(t *testing.T) { if err != nil { t.Fatalf("err: %v", err) } - for i := 0; i < 256; i++ { + for i := range 256 { l.Add(i, i) } k, _, ok := l.GetOldest() diff --git a/internal/slogpretty/handler.go b/internal/slogpretty/handler.go index 82a02876..af5a0840 100644 --- a/internal/slogpretty/handler.go +++ b/internal/slogpretty/handler.go @@ -10,6 +10,7 @@ import ( "io" "log/slog" "os" + "strings" "sync" "time" @@ -114,15 +115,15 @@ func (h *Handler) Handle(_ context.Context, record slog.Record) error { } buf = append(buf, " | "...) - lastGroup := "" + var lastGroup strings.Builder for _, goa := range h.Goa { switch { case goa.group != "": - lastGroup += goa.group + "." + lastGroup.WriteString(goa.group + ".") default: attr := goa.attr - if lastGroup != "" { - attr.Key = lastGroup + attr.Key + if lastGroup.String() != "" { + attr.Key = lastGroup.String() + attr.Key } buf = appendAttr(record.Level, buf, attr) @@ -132,8 +133,8 @@ func (h *Handler) Handle(_ context.Context, record slog.Record) error { // If there are additional attributes, append them to the log record. if record.NumAttrs() > 0 { record.Attrs(func(attr slog.Attr) bool { - if lastGroup != "" { - attr.Key = lastGroup + attr.Key + if lastGroup.String() != "" { + attr.Key = lastGroup.String() + attr.Key } buf = appendAttr(record.Level, buf, attr) diff --git a/matcher.go b/matcher.go index 8dc91ce0..1bf517d1 100644 --- a/matcher.go +++ b/matcher.go @@ -6,6 +6,7 @@ import ( "net" "net/http" "regexp" + "slices" "strings" "github.com/fox-toolkit/fox/internal/netutil" @@ -59,12 +60,7 @@ func (m QueryMatcher) Match(c RequestContext) bool { if len(values) == 0 { return false } - for _, v := range values { - if v == m.value { - return true - } - } - return false + return slices.Contains(values, m.value) } // Equal reports whether matcher is a [QueryMatcher] with the same key and value. @@ -129,12 +125,7 @@ func (m QueryRegexpMatcher) Match(c RequestContext) bool { if len(values) == 0 { return false } - for _, v := range values { - if m.regex.MatchString(v) { - return true - } - } - return false + return slices.ContainsFunc(values, m.regex.MatchString) } // 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 { if len(values) == 0 { return false } - for _, v := range values { - if v == m.value { - return true - } - } - return false + return slices.Contains(values, m.value) } // Equal reports whether matcher is a [HeaderMatcher] with the same key and value. @@ -247,12 +233,7 @@ func (m HeaderRegexpMatcher) Match(c RequestContext) bool { if len(values) == 0 { return false } - for _, v := range values { - if m.regex.MatchString(v) { - return true - } - } - return false + return slices.ContainsFunc(values, m.regex.MatchString) } // Equal reports whether matcher is a [HeaderRegexpMatcher] with the same key and regular expression source. diff --git a/params_test.go b/params_test.go index 38fe4066..ce59f7b0 100644 --- a/params_test.go +++ b/params_test.go @@ -101,7 +101,6 @@ func TestParamsFromContext(t *testing.T) { } for _, tc := range cases { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() diff --git a/path_test.go b/path_test.go index 796b3dd3..00fa9ca1 100644 --- a/path_test.go +++ b/path_test.go @@ -85,7 +85,6 @@ func TestCleanPath_Mallocs(t *testing.T) { } for _, test := range cleanTests { - test := test allocs := testing.AllocsPerRun(100, func() { CleanPath(test.result) }) if allocs > 0 { t.Errorf("CleanPath(%q): %v allocs, want zero", test.result, allocs) diff --git a/recovery.go b/recovery.go index 54ebfb0a..54237aea 100644 --- a/recovery.go +++ b/recovery.go @@ -83,12 +83,12 @@ func recovery(logger *slog.Logger, c *Context, handle RecoveryFunc) { sb.Write(before) for header := range iterutil.SplitBytesSeq(after, reqHeaderSep) { sb.Write(reqHeaderSep) - idx := bytes.IndexByte(header, ':') - if idx < 0 { + before0, _, ok := bytes.Cut(header, []byte{':'}) + if !ok { continue } - if slices.Contains(blacklistedHeader, string(header[:idx])) { - sb.Write(header[:idx]) + if slices.Contains(blacklistedHeader, string(before0)) { + sb.Write(before0) sb.WriteString(": ") continue }