pagination

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const DocumentationHMACKey = "openmrp-openapi-documentation-cursor-key-v1"

DocumentationHMACKey signs OpenAPI example cursors. It is not used at runtime.

Variables

This section is empty.

Functions

func EncodeCursor

func EncodeCursor(c Cursor) string

func EncodeDocumentationCursor

func EncodeDocumentationCursor(createdAt time.Time, internalID int64) string

EncodeDocumentationCursor returns a signed forward cursor for OpenAPI list examples using pagination.Cursor (int64 internal IDs).

func EncodeDocumentationStringCursor

func EncodeDocumentationStringCursor(occurredAt time.Time, id string) string

EncodeDocumentationStringCursor returns a signed forward cursor for OpenAPI list examples using pagination.StringCursor (varchar primary keys / type IDs).

func EncodeStringCursor

func EncodeStringCursor(c StringCursor) string

func Init

func Init(key []byte)

Init sets the HMAC key used to sign and verify cursors. Must be called once at service startup before any cursor operations.

Types

type Cursor

type Cursor struct {
	CreatedAt time.Time `json:"c"`
	ID        int64     `json:"i"`
	Direction Direction `json:"d"`
}

func DecodeCursor

func DecodeCursor(s string) (Cursor, error)

type Direction

type Direction string
const (
	DirectionForward  Direction = "f"
	DirectionBackward Direction = "b"
)

type PageInfo

type PageInfo struct {
	NextCursor  *string
	PrevCursor  *string
	HasNextPage bool
	HasPrevPage bool
}

func BuildPage

func BuildPage[T any](
	items []T,
	limit int32,
	cursorDir *Direction,
	getCreatedAt func(T) time.Time,
	getID func(T) int64,
) ([]T, PageInfo)

BuildPage trims the limit+1 slice, reverses if backward, and computes PageInfo. cursorDir is nil for first-page requests.

func BuildPageString

func BuildPageString[T any](
	items []T,
	limit int32,
	cursorDir *Direction,
	getOccurredAt func(T) time.Time,
	getID func(T) string,
) ([]T, PageInfo)

BuildPageString is like BuildPage but uses string IDs with StringCursor, for tables whose primary key is a varchar.

func BuildPageStringWithSearchRank

func BuildPageStringWithSearchRank[T any](
	items []T,
	limit int32,
	cursorDir *Direction,
	searchRankEnabled bool,
	getOccurredAt func(T) time.Time,
	getID func(T) string,
	getMatchTier func(T) int32,
) ([]T, PageInfo)

BuildPageStringWithSearchRank is like BuildPageString but embeds MatchTier in cursors when searchRankEnabled is true (catalog search relevance pagination).

type StringCursor

type StringCursor struct {
	OccurredAt time.Time `json:"c"`
	ID         string    `json:"s"`
	Direction  Direction `json:"d"`
	// MatchTier is set for ranked catalog search pagination (exact/prefix/substring SKU tiers).
	MatchTier *int `json:"t,omitempty"`
}

StringCursor is like Cursor but uses a string ID instead of int64, for tables whose primary key is a varchar (e.g. request_log).

func DecodeStringCursor

func DecodeStringCursor(s string) (StringCursor, error)

Jump to

Keyboard shortcuts

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