paging

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package paging parses the three pagination modes (limit, offset, keyset) and the order parameter exactly like the reference, and builds the collection links (with pagination meta nested in links).

Index

Constants

View Source
const (
	OrderAsc  = "asc"
	OrderDesc = "desc"
)

Order values (the default is desc, the production default).

Variables

This section is empty.

Functions

This section is empty.

Types

type Doc

type Doc struct {
	Self  string
	Prev  *string
	Next  *string
	First *string
	Last  *string
	// Meta is nil in cursor mode.
	Meta *Meta
}

Doc is a collection's links document: the four offset links (prev/next nil at the ends) with pagination meta nested inside, or the keyset self/next pair.

func CursorDoc

func CursorDoc(path, cursor string, size int, next uuid.NullUUID) Doc

CursorDoc builds the keyset-mode links: self (retaining the cursor and the size) and next (the last record's id), nil when the page ran out.

func OffsetDoc

func OffsetDoc(path string, number, size, pages, count int) Doc

OffsetDoc builds the offset-mode links against the request's original path (the account form the caller used), with page[number] and page[size] as the only query parameters.

type Meta

type Meta struct {
	Pages int
	Count int
}

Meta is the nested pagination meta (offset mode only).

type Mode

type Mode int

Mode is the pagination mode a request selected.

const (
	// ModeLimit is the default: ?limit=N (or 10), no links.
	ModeLimit Mode = iota
	// ModeOffset is page[number]+page[size]: full links + meta.
	ModeOffset
	// ModeCursor is page[cursor]+page[size]: self/next links.
	ModeCursor
)

The modes.

type Page

type Page struct {
	Cursor *uuid.UUID
	Limit  int64
	Offset int64
	Fetch  int64
	Asc    bool
}

Page is the storage-layer list window shared by every repository: the cursor (NULL outside keyset mode), the page bounds and the direction. Fetch is the row count to read — one more than the page in cursor mode, so the row after the page answers whether a next page exists.

type Params

type Params struct {
	Mode   Mode
	Limit  int
	Number int
	Size   int
	// Cursor is the keyset cursor; Valid false in the other modes.
	Cursor uuid.NullUUID
	Order  string
}

Params is the parsed pagination of a request.

func Parse

func Parse(query url.Values) (Params, error)

Parse reads limit, page[number], page[size], page[cursor] and order from the query. Errors are the reference's 400 documents with source.parameter set.

func (Params) SizeOrDefault

func (p Params) SizeOrDefault() int

SizeOrDefault answers the page size (limit mode included).

func (Params) Window

func (p Params) Window() Page

Window projects the parsed params onto the storage window.

Jump to

Keyboard shortcuts

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