Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 bug: fix client iterators when using break statement #3357

Merged
merged 2 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 8 additions & 6 deletions client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,13 @@ func (r *Request) Cookie(key string) string {
// Use maps.Collect() to gather them into a map if needed.
func (r *Request) Cookies() iter.Seq2[string, string] {
return func(yield func(string, string) bool) {
r.cookies.VisitAll(func(key, val string) {
if !yield(key, val) {
var res bool
for k, v := range *r.cookies {
res = yield(k, v)
if !res {
return
}
})
}
}
}

Expand Down Expand Up @@ -343,11 +345,11 @@ func (r *Request) PathParam(key string) string {
// Use maps.Collect() to gather them into a map if needed.
func (r *Request) PathParams() iter.Seq2[string, string] {
return func(yield func(string, string) bool) {
r.path.VisitAll(func(key, val string) {
if !yield(key, val) {
for k, v := range *r.path {
if !yield(k, v) {
return
}
})
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions client/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,14 @@
require.Equal(t, "bar", cookies["foo"])
require.Equal(t, "foo", cookies["bar"])

require.NotPanics(t, func() {
for _, v := range req.Cookies() {
if v == "bar" {

Check failure on line 456 in client/request_test.go

View workflow job for this annotation

GitHub Actions / lint

string `bar` has 6 occurrences, make it a constant (goconst)
break
}
}
})

require.Len(t, cookies, 2)
}

Expand Down Expand Up @@ -564,6 +572,14 @@
require.Equal(t, "foo", pathParams["bar"])

require.Len(t, pathParams, 2)

require.NotPanics(t, func() {
for _, v := range req.PathParams() {
if v == "bar" {
break
}
}
})
}

func Benchmark_Request_PathParams(b *testing.B) {
Expand Down Expand Up @@ -1579,7 +1595,7 @@

require.True(t, func() bool {
for _, v := range p.PeekMulti("TSlice") {
if string(v) == "bar" { //nolint:goconst // test

Check failure on line 1598 in client/request_test.go

View workflow job for this annotation

GitHub Actions / lint

directive `//nolint:goconst // test` is unused for linter "goconst" (nolintlint)
return true
}
}
Expand Down
Loading