Skip to content

Commit 2411bc4

Browse files
committed
feat: add All iterators for client cookie and path params
1 parent dd73968 commit 2411bc4

File tree

4 files changed

+67
-15
lines changed

4 files changed

+67
-15
lines changed

client/hooks.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ func parserRequestURL(c *Client, req *Request) error {
7171
}
7272

7373
// Set path parameters from the request and client.
74-
req.path.VisitAll(func(key, val string) {
74+
for key, val := range req.path.All() {
7575
uri = strings.ReplaceAll(uri, ":"+key, val)
76-
})
77-
c.path.VisitAll(func(key, val string) {
76+
}
77+
for key, val := range c.path.All() {
7878
uri = strings.ReplaceAll(uri, ":"+key, val)
79-
})
79+
}
8080

8181
// Set the URI in the raw request.
8282
req.RawRequest.SetRequestURI(uri)
@@ -161,14 +161,14 @@ func parserRequestHeader(c *Client, req *Request) error {
161161
}
162162

163163
// Set cookies from the client.
164-
c.cookies.VisitAll(func(key, val string) {
164+
for key, val := range c.cookies.All() {
165165
req.RawRequest.Header.SetCookie(key, val)
166-
})
166+
}
167167

168168
// Set cookies from the request.
169-
req.cookies.VisitAll(func(key, val string) {
169+
for key, val := range req.cookies.All() {
170170
req.RawRequest.Header.SetCookie(key, val)
171-
})
171+
}
172172

173173
return nil
174174
}

client/request.go

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -766,10 +766,27 @@ func (c Cookie) DelCookies(key ...string) {
766766
}
767767

768768
// VisitAll iterates through all cookies, calling f for each.
769+
// All returns an iterator over cookie key-value pairs.
770+
//
771+
// The returned key and value should not be retained after the iteration loop.
772+
func (c Cookie) All() iter.Seq2[string, string] {
773+
return func(yield func(string, string) bool) {
774+
for k, v := range c {
775+
if !yield(k, v) {
776+
break
777+
}
778+
}
779+
}
780+
}
781+
782+
// VisitAll iterates through all cookies, calling f for each.
783+
//
784+
// Deprecated: Use All instead.
769785
func (c Cookie) VisitAll(f func(key, val string)) {
770-
for k, v := range c {
786+
c.All()(func(k, v string) bool {
771787
f(k, v)
772-
}
788+
return true
789+
})
773790
}
774791

775792
// Reset clears the Cookie map.
@@ -816,10 +833,27 @@ func (p PathParam) DelParams(key ...string) {
816833
}
817834

818835
// VisitAll iterates through all path parameters, calling f for each.
836+
// All returns an iterator over path parameter key-value pairs.
837+
//
838+
// The returned key and value should not be retained after the iteration loop.
839+
func (p PathParam) All() iter.Seq2[string, string] {
840+
return func(yield func(string, string) bool) {
841+
for k, v := range p {
842+
if !yield(k, v) {
843+
break
844+
}
845+
}
846+
}
847+
}
848+
849+
// VisitAll iterates through all path parameters, calling f for each.
850+
//
851+
// Deprecated: Use All instead.
819852
func (p PathParam) VisitAll(f func(key, val string)) {
820-
for k, v := range p {
853+
p.All()(func(k, v string) bool {
821854
f(k, v)
822-
}
855+
return true
856+
})
823857
}
824858

825859
// Reset clears the PathParam map.

docs/client/hooks.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ func main() {
159159
fmt.Printf("HTTP protocol: %s\n\n", resp.Protocol())
160160

161161
fmt.Println("Response Headers:")
162-
resp.RawResponse.Header.VisitAll(func(key, value []byte) {
163-
fmt.Printf("%s: %s\n", key, value)
164-
})
162+
for key, value := range resp.RawResponse.Header.All() {
163+
fmt.Printf("%s: %s\n", key, value)
164+
}
165165

166166
return nil
167167
})

docs/client/request.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,15 @@ func (c Cookie) SetCookiesWithStruct(v any)
12051205
func (c Cookie) DelCookies(key ...string)
12061206
```
12071207

1208+
### All
1209+
1210+
**All** returns an iterator over all cookies. The key and value returned
1211+
should not be retained after the loop ends.
1212+
1213+
```go title="Signature"
1214+
func (c Cookie) All() iter.Seq2[string, string]
1215+
```
1216+
12081217
### VisitAll
12091218

12101219
**VisitAll** iterates over all cookies and executes a given function.
@@ -1277,6 +1286,15 @@ func (p PathParam) SetParamsWithStruct(v any)
12771286
func (p PathParam) DelParams(key ...string)
12781287
```
12791288

1289+
### All
1290+
1291+
**All** returns an iterator over all path parameters. The key and value returned
1292+
should not be retained after the loop ends.
1293+
1294+
```go title="Signature"
1295+
func (p PathParam) All() iter.Seq2[string, string]
1296+
```
1297+
12801298
### VisitAll
12811299

12821300
**VisitAll** iterates over all path parameters and executes the provided function.

0 commit comments

Comments
 (0)