pagination

package
v9.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	InvalidValue int32 = -1
)

Variables

This section is empty.

Functions

func GetListAndCursor

func GetListAndCursor[T any](raw []T, sg PageSizeGetter) ([]T, T)

GetListAndCursor takes a slice of elements and returns a page of results in the form of a slice and a page cursor.

The page of results is the result of the user query and is to be returned to the user as is.

The page cursor is used to determine whether there are additional result pages available. The cursor is an element that is part of the collection, but not part of the current page.

If the cursor:

* Is a non-zero value, then there are additional pages available. * Is a zero value, then the returned page is the last page.

The returned cursor is typically processed by `GetNextPageToken[...]()` type functions to generate a page token.

See firestore.setMaxResults to understand why this function is being used.

func GetNextPageTokenFromTime

func GetNextPageTokenFromTime(t time.Time) string

GetNextPageTokenFromTime generates a page token. This function should be used when generating a next page token based on a `time.Time` type field.

func NewPageToken

func NewPageToken(input encoding.TextMarshaler) string

NewPageToken generates a page token in base64. It returns an empty string if the input is nil or the conversion to string fails.

func PageSize

func PageSize(req PageSizeGetter, opts ...PageSizeOptions) int32

PageSize returns a valid page size following the AIP-158 proposal for Pagination. Reference: https://google.aip.dev/158

Default value: 50
Default max value: 100

If no value is passed, it returns the default value.
If a value greater than the max page size is specified, it caps the result value to the max page size.
If a negative value is specified, it returns -1.

func ParsePageTokenToTime

func ParsePageTokenToTime(token string) (time.Time, error)

ParsePageTokenToTime converts the given token and returns a valid time.Time. The token provided is usually the value used in cursor-based pagination.

func WriteResponse

func WriteResponse(w http.ResponseWriter, res PagingResponse) error

WriteResponse writes to http.ResponseWriter the pagination values determined by PagingResponse.

Types

type PageSizeGetter

type PageSizeGetter interface {
	// GetPageSize returns the desired page size. It's using int32 in order to match the method signature from the
	// generated Go stubs that also return an int32 value.
	GetPageSize() int32
}

PageSizeGetter provides the amount of results to return when paginating results.

type PageSizeOptions

type PageSizeOptions struct {
	// MaxSize is the maximum number of elements that can be returned by the API.
	MaxSize int32
	// DefaultSize is the number of elements that the page list will default to.
	DefaultSize int32
}

PageSizeOptions allows developers to pass new MaxSize and DefaultSize values to the PageSize function. The options described in this struct override the default PageSize values provided in this library.

type PageTokenGetter

type PageTokenGetter interface {
	// GetPageToken returns the requested page token.
	// If the user requests a specific page, this value must not be empty.
	// For cursor-based pagination, this value must be in base64.
	GetPageToken() string
}

PageTokenGetter provides a page token used to paginate results.

type Pagination

type Pagination interface {
	PageSizeGetter
	PageTokenGetter
}

Pagination performs result pagination. It is intended to be implemented by client request types.

type PagingLinks struct {
	// First contains the URL where clients should query the first page of elements.
	First *url.URL
	// Previous contains the URL where clients should query the first page of elements.
	Previous *url.URL
	// Next contains the URL where clients should query the first page of elements.
	Next *url.URL
	// Last contains the URL where clients should query the first page of elements.
	Last *url.URL
}

PagingLinks contains a set of URLs to help with pagination.

func (PagingLinks) ToLinkHeaders

func (p PagingLinks) ToLinkHeaders() []string

ToLinkHeaders converts the links in PagingLinks to an array of links to use in the "Link" HTTP header.

type PagingRequest

type PagingRequest struct {
	// Page is the page that should be shown. When using ReadRequest, this value defaults to 1.
	Page uint64
	// PageSize contains the amount of elements that a Page will include. When using ReadRequest, this value defaults to 30.
	PageSize uint64
}

PagingRequest is used to describe pagination values from incoming requests.

func ReadRequest

func ReadRequest(r *http.Request) PagingRequest

ReadRequest reads the PagingRequest from a http.Request.

type PagingResponse

type PagingResponse struct {
	// BaseURL contains the base url that should be used to interact with the desired API list method that supports pagination.
	// If BaseURL contains query params for page and page_size, those values will be overwritten.
	BaseURL *url.URL
	// Page contains the page that was requested.
	Page uint64
	// PageSize contains the amount of elements that were requested for this Page.
	PageSize uint64
	// Count contains the amount of elements that were retrieved for this Page.
	Count uint64
	// TotalCount contains the elements available, usually used to calculate the amount of pages available.
	TotalCount uint64
}

PagingResponse contains the values that should be added to an HTTP response.

func (r PagingResponse) ToPagingLinks() PagingLinks

ToPagingLinks converts the current PagingResponse to a group of links described by PagingLinks.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL