-
Notifications
You must be signed in to change notification settings - Fork 962
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
53 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//go:build go1.22 && goexperiment.rangefunc | ||
// +build go1.22,goexperiment.rangefunc | ||
|
||
package gitlab | ||
|
||
import ( | ||
"iter" | ||
) | ||
|
||
// PageIterator is an EXPERIMENTAL iterator as defined in the "rangefunc" experiment for go 1.22. | ||
// See https://go.dev/wiki/RangefuncExperiment for more details. | ||
// | ||
// It can be used as: | ||
// | ||
// for user, err := range gitlab.PageIterator(gl.Users.List, nil) { | ||
// if err != nil { | ||
// // handle error | ||
// } | ||
// // process individual user | ||
// } | ||
func PageIterator[O, T any](f Paginatable[O, T], opt *O, optFunc ...RequestOptionFunc) iter.Seq2[*T, error] { | ||
return func(yield func(*T, error) bool) { | ||
nextLink := "" | ||
for { | ||
page, resp, err := f(opt, append(optFunc, WithKeysetPaginationParameters(nextLink))...) | ||
if err != nil { | ||
yield(nil, err) | ||
return | ||
} | ||
for _, p := range page { | ||
if !yield(p, nil) { | ||
return | ||
} | ||
} | ||
if resp.NextLink == "" { | ||
break | ||
} | ||
nextLink = resp.NextLink | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters