pagination

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package pagination defines pagination helpers.

Index

Constants

View Source
const (
	// QueryKeyPage is the URL querystring for the current page.
	QueryKeyPage = "page"

	// QueryKeyLimit is the URL querystring for the current per-page limit.
	QueryKeyLimit = "limit"

	// DefaultLimit is the default pagination limit, if one is not provided.
	DefaultLimit = uint64(14)

	// MaxLimit is the maximum size for a single pagination. If a limit higher
	// than this is provided, it's capped.
	MaxLimit = uint64(100)
)

Variables

View Source
var UnlimitedResults = &PageParams{
	Page:  1,
	Limit: math.MaxInt64,
}

UnlimitedResults is a paginator that doesn't do pagination and returns all results (up to (1<<63)-1).

Functions

This section is empty.

Types

type Page

type Page struct {
	// Number is the numerical page number.
	Number uint64
}

Page represents a single page.

func (p *Page) Link(s string) (string, error)

Link is the link to this page. It accepts the current URL, adds/modifies the page querystring parameter, and returns a new URL.

type PageParams

type PageParams struct {
	// Page is the current page.
	Page uint64

	// Limit is the per-page limit.
	Limit uint64
}

PageParams are the page parameters including the current page and per page limit.

func FromRequest

func FromRequest(r *http.Request) (*PageParams, error)

FromRequest builds the PageParams from an http.Request. If there are no pagination parameters, the struct is returned with the default values.

type Paginator

type Paginator struct {
	PrevPage  *Page
	PrevPages []*Page
	CurrPage  *Page
	NextPages []*Page
	NextPage  *Page
}

Paginator is a meta-struct that holds pagination information. It's largely a collection of pointers to Page structs. You may notice some repetition across fields - this is to make the struct easier to use inside Go template's where simple features like "get the last element of a slice" are actually very hard.

Jump to

Keyboard shortcuts

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