Skip to content

Commit

Permalink
feat: add "ForID" variants of pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
costela committed Sep 26, 2024
1 parent 4eb63f7 commit 16265ec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 8 additions & 0 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ func PageIterator[O, T any](f Paginatable[O, T], opt *O, optFunc ...RequestOptio
}
}
}

// PageIteratorForID is similar to [PageIterator] but for paginated resources that require a parent ID (e.g. tags of a project).
func PageIteratorForID[O, T any](id any, f PaginatableForID[O, T], opt *O, optFunc ...RequestOptionFunc) iter.Seq2[*T, error] {
idFunc := func(opt *O, optFunc ...RequestOptionFunc) ([]*T, *Response, error) {
return f(id, opt, optFunc...)
}
return PageIterator(idFunc, opt, optFunc...)
}
17 changes: 16 additions & 1 deletion pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,20 @@ func AllPages[O, T any](f Paginatable[O, T], opt *O, optFunc ...RequestOptionFun
return all, nil
}

// Paginatable is the type that is implemented by all functions used to paginated content.
// Paginatable is the type implemented by list functions that return paginated content (e.g. [UsersService.ListUsers]).
// It works for top-level entities (e.g. users). See [PaginatableForID] for entities that require a parent ID (e.g.
// tags).
type Paginatable[O, T any] func(*O, ...RequestOptionFunc) ([]*T, *Response, error)

// AllPagesForID is similar to [AllPages] but for paginated resources that require a parent ID (e.g. tags of a project).
func AllPagesForID[O, T any](id any, f PaginatableForID[O, T], opt *O, optFunc ...RequestOptionFunc) ([]*T, error) {
idFunc := func(opt *O, optFunc ...RequestOptionFunc) ([]*T, *Response, error) {
return f(id, opt, optFunc...)
}
return AllPages(idFunc, opt, optFunc...)
}

// PaginatableForID is the type implemented by list functions that return paginated content for sub-entities (e.g.
// [TagsService.ListTags]).
// See also [Paginatable] for top-level entities (e.g. users).
type PaginatableForID[O, T any] func(any, *O, ...RequestOptionFunc) ([]*T, *Response, error)

0 comments on commit 16265ec

Please sign in to comment.