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

Commit fb83965

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

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
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, optFunc ...RequestOptionFunc) iter.Seq2[*T, error] {
22+
return func(yield func(*T, error) bool) {
23+
nextLink := ""
24+
for {
25+
page, resp, err := f(opt, append(optFunc, 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+
}

pagination.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@ package gitlab
66
//
77
// It is also possible to specify additional pagination parameters:
88
//
9-
// mrs, err := gitlab.AllPages(gl.MergeRequests.ListMergeRequests, &gitlab.ListMergeRequestsOptions{
10-
// ListOptions: gitlab.ListOptions{
11-
// PerPage: 100,
12-
// Pagination: "keyset",
13-
// OrderBy: "created_at",
9+
// mrs, err := gitlab.AllPages(
10+
// gl.MergeRequests.ListMergeRequests,
11+
// &gitlab.ListMergeRequestsOptions{
12+
// ListOptions: gitlab.ListOptions{
13+
// PerPage: 100,
14+
// Pagination: "keyset",
15+
// OrderBy: "created_at",
16+
// },
1417
// },
15-
// })
16-
func AllPages[O, T any](f Paginatable[O, T], opt *O) ([]*T, error) {
18+
// gitlab.WithContext(ctx),
19+
// )
20+
func AllPages[O, T any](f Paginatable[O, T], opt *O, optFunc ...RequestOptionFunc) ([]*T, error) {
1721
all := make([]*T, 0)
1822
nextLink := ""
1923
for {
20-
page, resp, err := f(opt, WithKeysetPaginationParameters(nextLink))
24+
page, resp, err := f(opt, append(optFunc, WithKeysetPaginationParameters(nextLink))...)
2125
if err != nil {
2226
return nil, err
2327
}

0 commit comments

Comments
 (0)