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
2 changes: 1 addition & 1 deletion clientip/clientip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
2 changes: 0 additions & 2 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion fox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 1 addition & 4 deletions internal/simplelru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
6 changes: 3 additions & 3 deletions internal/simplelru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 7 additions & 6 deletions internal/slogpretty/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"log/slog"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down
29 changes: 5 additions & 24 deletions matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"net/http"
"regexp"
"slices"
"strings"

"github.com/fox-toolkit/fox/internal/netutil"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(": <redacted>")
continue
}
Expand Down
Loading