pagination

package
v1.0.0-beta.228 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const MAX_SAFE_ITER = 10_000

Variables

This section is empty.

Functions

func CollectAll

func CollectAll[T any](ctx context.Context, paginator Paginator[T], cursor *Cursor) ([]T, error)

CollectAll collects all items from the paginator with cursoring.

Types

type Cursor

type Cursor struct {
	Time time.Time
	ID   string
}

func DecodeCursor

func DecodeCursor(s string) (*Cursor, error)

DecodeCursor decodes a base64-encoded cursor string into a Cursor object.

func NewCursor

func NewCursor(t time.Time, id string) Cursor

NewCursor creates a new Cursor object with the given time and ID. The time is converted to UTC before being stored.

func (Cursor) Encode

func (c Cursor) Encode() string

Encode converts the cursor to a base64-encoded string representation. The encoded string is formatted as <RFC3339 time>,<ID>.

func (*Cursor) EncodePtr

func (c *Cursor) EncodePtr() *string

func (Cursor) MarshalText

func (c Cursor) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. It encodes the cursor into a text form.

func (*Cursor) UnmarshalText

func (c *Cursor) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. It decodes the cursor from its text form.

func (Cursor) Validate

func (c Cursor) Validate() error

type Item

type Item interface {
	// Cursor returns the cursor used for cursor-based ordering
	Cursor() Cursor
}

Item is the interface that must be implemented by items used in cursor pagination. It provides access to the time and ID fields needed for cursor generation.

type Paginator

type Paginator[T any] interface {
	Paginate(ctx context.Context, cursor *Cursor) (Result[T], error)
}

func NewPaginator

func NewPaginator[T any](fn func(ctx context.Context, cursor *Cursor) (Result[T], error)) Paginator[T]

type Result

type Result[T any] struct {
	// The items returned
	Items []T `json:"items"`

	// Cursor for the next page
	NextCursor *Cursor `json:"nextCursor"`
}

Result represents the response structure for cursor-based pagination

func NewResult

func NewResult[T Item](items []T) Result[T]

NewResult creates a new pagination result from an ordered list of items. T must implement the Item interface for cursor generation.

Jump to

Keyboard shortcuts

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