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

Commit 9067982

Browse files
committed
feat: add AllPages pagination helper
1 parent 997404b commit 9067982

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

pagination.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package gitlab
2+
3+
// AllPages can be used to fetch all pages of a paginated resource, e.g.: (assuming gl is a gitlab client instance)
4+
//
5+
// allUsers, err := gitlab.AllPages(gl.Users.List, nil)
6+
//
7+
// It is also possible to specify additional pagination parameters:
8+
//
9+
// mrs, err := gitlab.AllPages(gl.MergeRequests.ListMergeRequests, &gitlab.ListMergeRequestsOptions{
10+
// ListOptions: gitlab.ListOptions{
11+
// PerPage: 100,
12+
// Pagination: "keyset",
13+
// OrderBy: "created_at",
14+
// },
15+
// })
16+
func AllPages[O, T any](f Paginatable[O, T], opt *O) ([]*T, error) {
17+
all := make([]*T, 0)
18+
nextLink := ""
19+
for {
20+
page, resp, err := f(opt, WithKeysetPaginationParameters(nextLink))
21+
if err != nil {
22+
return nil, err
23+
}
24+
all = append(all, page...)
25+
if resp.NextLink == "" {
26+
break
27+
}
28+
nextLink = resp.NextLink
29+
}
30+
return all, nil
31+
}
32+
33+
// Paginatable is the type that is implemented by all functions used to paginated content.
34+
type Paginatable[O, T any] func(*O, ...RequestOptionFunc) ([]*T, *Response, error)

0 commit comments

Comments
 (0)