Skip to content

Commit e041d63

Browse files
authored
🧹 chore: Add iterator helpers for client types (#3560)
* feat: add All iterators for client cookie and path params * Remove deprecated VisitAll helpers
1 parent e9e0367 commit e041d63

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
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: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -765,10 +765,16 @@ func (c Cookie) DelCookies(key ...string) {
765765
}
766766
}
767767

768-
// VisitAll iterates through all cookies, calling f for each.
769-
func (c Cookie) VisitAll(f func(key, val string)) {
770-
for k, v := range c {
771-
f(k, v)
768+
// All returns an iterator over cookie key-value pairs.
769+
//
770+
// The returned key and value should not be retained after the iteration loop.
771+
func (c Cookie) All() iter.Seq2[string, string] {
772+
return func(yield func(string, string) bool) {
773+
for k, v := range c {
774+
if !yield(k, v) {
775+
break
776+
}
777+
}
772778
}
773779
}
774780

@@ -815,10 +821,16 @@ func (p PathParam) DelParams(key ...string) {
815821
}
816822
}
817823

818-
// VisitAll iterates through all path parameters, calling f for each.
819-
func (p PathParam) VisitAll(f func(key, val string)) {
820-
for k, v := range p {
821-
f(k, v)
824+
// All returns an iterator over path parameter key-value pairs.
825+
//
826+
// The returned key and value should not be retained after the iteration loop.
827+
func (p PathParam) All() iter.Seq2[string, string] {
828+
return func(yield func(string, string) bool) {
829+
for k, v := range p {
830+
if !yield(k, v) {
831+
break
832+
}
833+
}
822834
}
823835
}
824836

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: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,12 +1205,13 @@ func (c Cookie) SetCookiesWithStruct(v any)
12051205
func (c Cookie) DelCookies(key ...string)
12061206
```
12071207

1208-
### VisitAll
1208+
### All
12091209

1210-
**VisitAll** iterates over all cookies and executes a given function.
1210+
**All** returns an iterator over all cookies. The key and value returned
1211+
should not be retained after the loop ends.
12111212

12121213
```go title="Signature"
1213-
func (c Cookie) VisitAll(f func(key, val string))
1214+
func (c Cookie) All() iter.Seq2[string, string]
12141215
```
12151216

12161217
### Reset
@@ -1277,12 +1278,13 @@ func (p PathParam) SetParamsWithStruct(v any)
12771278
func (p PathParam) DelParams(key ...string)
12781279
```
12791280

1280-
### VisitAll
1281+
### All
12811282

1282-
**VisitAll** iterates over all path parameters and executes the provided function.
1283+
**All** returns an iterator over all path parameters. The key and value returned
1284+
should not be retained after the loop ends.
12831285

12841286
```go title="Signature"
1285-
func (p PathParam) VisitAll(f func(key, val string))
1287+
func (p PathParam) All() iter.Seq2[string, string]
12861288
```
12871289

12881290
### Reset

0 commit comments

Comments
 (0)