Skip to content

Commit fe2a14f

Browse files
committed
client: add support for go1.23 iterators
1 parent f010539 commit fe2a14f

File tree

4 files changed

+440
-0
lines changed

4 files changed

+440
-0
lines changed

client/request.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"context"
66
"errors"
77
"io"
8+
"iter"
89
"path/filepath"
910
"reflect"
11+
"slices"
1012
"strconv"
1113
"sync"
1214
"time"
@@ -129,6 +131,29 @@ func (r *Request) Header(key string) []string {
129131
return r.header.PeekMultiple(key)
130132
}
131133

134+
// Headers returns all headers in the request using an iterator.
135+
// You can use maps.Collect() to collect all headers into a map.
136+
//
137+
// The returned value is valid until the request object is released.
138+
// Any future calls to Headers method will return the modified value. Do not store references to returned value. Make copies instead.
139+
func (r *Request) Headers() iter.Seq2[string, []string] {
140+
return func(yield func(string, []string) bool) {
141+
keys := r.header.PeekKeys()
142+
143+
for _, key := range keys {
144+
vals := r.header.PeekAll(utils.UnsafeString(key))
145+
valsStr := make([]string, len(vals))
146+
for i, v := range vals {
147+
valsStr[i] = utils.UnsafeString(v)
148+
}
149+
150+
if !yield(utils.UnsafeString(key), valsStr) {
151+
return
152+
}
153+
}
154+
}
155+
}
156+
132157
// AddHeader method adds a single header field and its value in the request instance.
133158
func (r *Request) AddHeader(key, val string) *Request {
134159
r.header.Add(key, val)
@@ -168,6 +193,33 @@ func (r *Request) Param(key string) []string {
168193
return res
169194
}
170195

196+
// Params returns all params in the request using an iterator.
197+
// You can use maps.Collect() to collect all params into a map.
198+
//
199+
// The returned value is valid until the request object is released.
200+
// Any future calls to Params method will return the modified value. Do not store references to returned value. Make copies instead.
201+
func (r *Request) Params() iter.Seq2[string, []string] {
202+
return func(yield func(string, []string) bool) {
203+
keys := r.params.Keys()
204+
205+
for _, key := range keys {
206+
if key == "" {
207+
continue
208+
}
209+
210+
vals := r.params.PeekMulti(key)
211+
valsStr := make([]string, len(vals))
212+
for i, v := range vals {
213+
valsStr[i] = utils.UnsafeString(v)
214+
}
215+
216+
if !yield(key, valsStr) {
217+
return
218+
}
219+
}
220+
}
221+
}
222+
171223
// AddParam method adds a single param field and its value in the request instance.
172224
func (r *Request) AddParam(key, val string) *Request {
173225
r.params.Add(key, val)
@@ -254,6 +306,18 @@ func (r *Request) Cookie(key string) string {
254306
return ""
255307
}
256308

309+
// Cookies returns all cookies in the cookies using an iterator.
310+
// You can use maps.Collect() to collect all cookies into a map.
311+
func (r *Request) Cookies() iter.Seq2[string, string] {
312+
return func(yield func(string, string) bool) {
313+
r.cookies.VisitAll(func(key, val string) {
314+
if !yield(key, val) {
315+
return
316+
}
317+
})
318+
}
319+
}
320+
257321
// SetCookie method sets a single cookie field and its value in the request instance.
258322
// It will override cookie which set in client instance.
259323
func (r *Request) SetCookie(key, val string) *Request {
@@ -291,6 +355,18 @@ func (r *Request) PathParam(key string) string {
291355
return ""
292356
}
293357

358+
// PathParams returns all path params in request instance.
359+
// You can use maps.Collect() to collect all cookies into a map.
360+
func (r *Request) PathParams() iter.Seq2[string, string] {
361+
return func(yield func(string, string) bool) {
362+
r.path.VisitAll(func(key, val string) {
363+
if !yield(key, val) {
364+
return
365+
}
366+
})
367+
}
368+
}
369+
294370
// SetPathParam method sets a single path param field and its value in the request instance.
295371
// It will override path param which set in client instance.
296372
func (r *Request) SetPathParam(key, val string) *Request {
@@ -376,6 +452,33 @@ func (r *Request) FormData(key string) []string {
376452
return res
377453
}
378454

455+
// FormDatas method returns all form datas in request instance.
456+
// You can use maps.Collect() to collect all cookies into a map.
457+
//
458+
// The returned value is valid until the request object is released.
459+
// Any future calls to FormDatas method will return the modified value. Do not store references to returned value. Make copies instead.
460+
func (r *Request) FormDatas() iter.Seq2[string, []string] {
461+
return func(yield func(string, []string) bool) {
462+
keys := r.formData.Keys()
463+
464+
for _, key := range keys {
465+
if key == "" {
466+
continue
467+
}
468+
469+
vals := r.formData.PeekMulti(key)
470+
valsStr := make([]string, len(vals))
471+
for i, v := range vals {
472+
valsStr[i] = utils.UnsafeString(v)
473+
}
474+
475+
if !yield(key, valsStr) {
476+
return
477+
}
478+
}
479+
}
480+
}
481+
379482
// AddFormData method adds a single form data field and its value in the request instance.
380483
func (r *Request) AddFormData(key, val string) *Request {
381484
r.formData.AddData(key, val)
@@ -435,6 +538,14 @@ func (r *Request) File(name string) *File {
435538
return nil
436539
}
437540

541+
// Files method returns all files in request instance.
542+
//
543+
// The returned value is valid until the request object is released.
544+
// Any future calls to Files method will return the modified value. Do not store references to returned value. Make copies instead.
545+
func (r *Request) Files() []*File {
546+
return r.files
547+
}
548+
438549
// FileByPath returns file ptr store in request obj by path.
439550
func (r *Request) FileByPath(path string) *File {
440551
for _, v := range r.files {
@@ -617,6 +728,16 @@ type QueryParam struct {
617728
*fasthttp.Args
618729
}
619730

731+
// Keys method returns all keys in the query params.
732+
func (f *QueryParam) Keys() []string {
733+
keys := make([]string, f.Len())
734+
f.VisitAll(func(key, value []byte) {
735+
keys = append(keys, utils.UnsafeString(key))
736+
})
737+
738+
return slices.Compact(keys)
739+
}
740+
620741
// AddParams receive a map and add each value to param.
621742
func (p *QueryParam) AddParams(r map[string][]string) {
622743
for k, v := range r {
@@ -747,6 +868,16 @@ type FormData struct {
747868
*fasthttp.Args
748869
}
749870

871+
// Keys method returns all keys in the form data.
872+
func (f *FormData) Keys() []string {
873+
keys := make([]string, f.Len())
874+
f.VisitAll(func(key, value []byte) {
875+
keys = append(keys, utils.UnsafeString(key))
876+
})
877+
878+
return slices.Compact(keys)
879+
}
880+
750881
// AddData method is a wrapper of Args's Add method.
751882
func (f *FormData) AddData(key, val string) {
752883
f.Add(key, val)

0 commit comments

Comments
 (0)