Skip to content

Commit

Permalink
feat: add experimental iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
costela committed Feb 7, 2024
1 parent 9067982 commit 141d142
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions iterator.go
@@ -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) iter.Seq2[*T, error] {
return func(yield func(*T, error) bool) {
nextLink := ""
for {
page, resp, err := f(opt, 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
}
}
}

0 comments on commit 141d142

Please sign in to comment.