Skip to content

Commit

Permalink
fix(pagination): always enforce max size is set (#820)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored Nov 6, 2024
1 parent 52dce5e commit eadc5d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pagination/keysetpagination/paginator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ var ErrUnknownOrder = errors.New("unknown order")
const (
OrderDescending Order = "DESC"
OrderAscending Order = "ASC"

DefaultSize = 100
DefaultMaxSize = 500
)

func (o Order) extract() (string, string, error) {
Expand Down Expand Up @@ -62,7 +65,7 @@ func (p *Paginator) Size() int {
size = 100
}
}
if p.maxSize > 0 && size > p.maxSize {
if size > p.maxSize {
size = p.maxSize
}
return size
Expand Down Expand Up @@ -229,7 +232,11 @@ func withIsLast(isLast bool) Option {
}

func GetPaginator(modifiers ...Option) *Paginator {
opts := &Paginator{}
opts := &Paginator{
// these can still be overridden by the modifiers, but they should never be unset
maxSize: DefaultMaxSize,
defaultSize: DefaultSize,
}
for _, f := range modifiers {
opts = f(opts)
}
Expand Down
5 changes: 5 additions & 0 deletions pagination/keysetpagination/paginator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func TestPaginator(t *testing.T) {
opts: nil,
expectedSize: 100,
},
{
name: "default max size",
opts: []Option{WithSize(1000)},
expectedSize: DefaultMaxSize,
},
{
name: "with size and token",
opts: []Option{WithSize(10), WithToken(StringPageToken("token"))},
Expand Down

0 comments on commit eadc5d5

Please sign in to comment.