🐛 bug: fix client iterators when using break statement

pull/3357/head
Muhammed Efe Cetin 2025-03-18 17:46:42 +03:00
parent 395c8fafa9
commit d06d0ca794
No known key found for this signature in database
GPG Key ID: 0AA4D45CBAA86F73
2 changed files with 24 additions and 6 deletions

View File

@ -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
}
})
}
}
}
@ -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
}
})
}
}
}

View File

@ -451,6 +451,14 @@ func Test_Request_Cookies(t *testing.T) {
require.Equal(t, "bar", cookies["foo"])
require.Equal(t, "foo", cookies["bar"])
require.NotPanics(t, func() {
for _, v := range req.Cookies() {
if v == "bar" {
break
}
}
})
require.Len(t, cookies, 2)
}
@ -564,6 +572,14 @@ func Test_Request_PathParams(t *testing.T) {
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) {