ListOptions.setDefaults(): remove artificial and buggy pagination limits (#573)

fixes #571

Co-authored-by: Norwin <git@nroo.de>
Co-authored-by: Andrew Thornton <art27@cantab.net>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/573
Reviewed-by: Gusted <williamzijl7@hotmail.com>
Reviewed-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: Norwin <noerw@noreply.gitea.io>
Co-committed-by: Norwin <noerw@noreply.gitea.io>
This commit is contained in:
Norwin 2022-04-27 03:07:37 +08:00 committed by John Olheiser
parent 2e8bb53b30
commit 223f0a75e0

View file

@ -9,12 +9,13 @@ import (
"net/url" "net/url"
) )
const defaultPageSize = 10
const maxPageSize = 50
// ListOptions options for using Gitea's API pagination // ListOptions options for using Gitea's API pagination
type ListOptions struct { type ListOptions struct {
Page int // Setting Page to -1 disables pagination on endpoints that support it.
// Page numbering starts at 1.
Page int
// The default value depends on the server config DEFAULT_PAGING_NUM
// The highest valid value depends on the server config MAX_RESPONSE_ITEMS
PageSize int PageSize int
} }
@ -26,8 +27,9 @@ func (o ListOptions) getURLQuery() url.Values {
return query return query
} }
// setDefaults set default pagination options if none or wrong are set // setDefaults applies default pagination options.
// if you set -1 as page it will set all to 0 // If .Page is set to -1, it will disable pagination.
// WARNING: This function is not idempotent, make sure to never call this method twice!
func (o *ListOptions) setDefaults() { func (o *ListOptions) setDefaults() {
if o.Page < 0 { if o.Page < 0 {
o.Page, o.PageSize = 0, 0 o.Page, o.PageSize = 0, 0
@ -35,8 +37,4 @@ func (o *ListOptions) setDefaults() {
} else if o.Page == 0 { } else if o.Page == 0 {
o.Page = 1 o.Page = 1
} }
if o.PageSize < 0 || o.PageSize > maxPageSize {
o.PageSize = defaultPageSize
}
} }