Skip to content
This repository was archived by the owner on Dec 10, 2024. It is now read-only.

Commit 141d142

Browse files
committed
feat: add experimental iterator
1 parent 9067982 commit 141d142

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

iterator.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//go:build go1.22 && goexperiment.rangefunc
2+
// +build go1.22,goexperiment.rangefunc
3+
4+
package gitlab
5+
6+
import (
7+
"iter"
8+
)
9+
10+
// PageIterator is an EXPERIMENTAL iterator as defined in the "rangefunc" experiment for go 1.22.
11+
// See https://go.dev/wiki/RangefuncExperiment for more details.
12+
//
13+
// It can be used as:
14+
//
15+
// for user, err := range gitlab.PageIterator(gl.Users.List, nil) {
16+
// if err != nil {
17+
// // handle error
18+
// }
19+
// // process individual user
20+
// }
21+
func PageIterator[O, T any](f Paginatable[O, T], opt *O) iter.Seq2[*T, error] {
22+
return func(yield func(*T, error) bool) {
23+
nextLink := ""
24+
for {
25+
page, resp, err := f(opt, WithKeysetPaginationParameters(nextLink))
26+
if err != nil {
27+
yield(nil, err)
28+
return
29+
}
30+
for _, p := range page {
31+
if !yield(p, nil) {
32+
return
33+
}
34+
}
35+
if resp.NextLink == "" {
36+
break
37+
}
38+
nextLink = resp.NextLink
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)