pagination

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0, BSD-3-Clause, CC-BY-4.0, + 2 more Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package pagination 提供与产品、账号和输出无关的逻辑分页算法。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CollectStreams added in v1.1.0

func CollectStreams[T any, C Cursor](ctx context.Context, plan PagePlan, streams []Stream[T, C]) ([]T, StreamState[C], PageResult, error)

CollectStreams 从所有流的零 cursor 开始,按顺序收集一个统一逻辑页。

func CollectStreamsFrom added in v1.1.0

func CollectStreamsFrom[T any, C Cursor](ctx context.Context, plan PagePlan, streams []Stream[T, C], initial StreamState[C]) ([]T, StreamState[C], PageResult, error)

CollectStreamsFrom 从调用方提供的聚合位置开始收集一个统一逻辑页。

当 Limit 在源批次内部截断时,返回的 StreamState 会保留当前流的 Checkpoint cursor;当当前流结束而仍有后续流时,位置推进到下一流的零 cursor。任一流失败都会丢弃整个逻辑页的部分结果。

Types

type Cursor

type Cursor interface {
	IsZero() bool
	String() string
}

Cursor 是分页算法所需的最小 continuation 契约。实现必须让零值表示没有 continuation,并提供稳定的文本形式以检测循环;算法不会解码或重建 cursor。

type PagePlan

type PagePlan struct {
	Skip     int
	Limit    int
	OneBatch bool
}

PagePlan 描述逻辑分页请求。Limit 为 0 时遍历全部结果。

type PageResult

type PageResult struct {
	Returned int
	HasMore  bool
}

PageResult 描述逻辑分页实际返回的条数以及上游是否仍有内容。

func CollectFilteredPagesFrom

func CollectFilteredPagesFrom[T any, C Cursor](ctx context.Context, plan PagePlan, initial C, fetch func(context.Context, C) ([]T, C, error), include func(T) (bool, error), checkpoint func(C, int) (C, error)) ([]T, C, PageResult, error)

CollectFilteredPagesFrom 对上游批次逐项筛选,再应用 Skip、Limit 和 OneBatch。 不能先把 Limit 传给普通 TraversePages,否则被筛掉的候选会占用逻辑页额度。

func CollectPages

func CollectPages[T any, C Cursor](ctx context.Context, plan PagePlan, fetch func(context.Context, C) ([]T, C, error)) ([]T, PageResult, error)

CollectPages 使用共享遍历引擎收集结果;失败时丢弃已经收集的部分结果。

func CollectPagesFrom

func CollectPagesFrom[T any, C Cursor](ctx context.Context, plan PagePlan, initial C, fetch func(context.Context, C) ([]T, C, error)) ([]T, PageResult, error)

CollectPagesFrom 保留调用方初始 cursor 的 operation/query binding,并在失败 时丢弃已经收集的部分结果。

func TraversePages

func TraversePages[T any, C Cursor](ctx context.Context, plan PagePlan, fetch func(context.Context, C) ([]T, C, error), consume func([]T) error) (PageResult, error)

TraversePages 从 Cursor 的零值开始遍历。Cursor 的具体产品类型由 fetch 推导,分页包不依赖任何 SDK 或传输实现。

func TraversePagesFrom

func TraversePagesFrom[T any, C Cursor](ctx context.Context, plan PagePlan, initial C, fetch func(context.Context, C) ([]T, C, error), consume func([]T) error) (PageResult, error)

TraversePagesFrom 从调用方已有的 opaque cursor 开始遍历。

type Stream added in v1.1.0

type Stream[T any, C Cursor] struct {
	Fetch      func(context.Context, C) ([]T, C, error)
	Include    func(T) (bool, error)
	Checkpoint func(C, int) (C, error)
}

Stream 描述一个保持自身 upstream 顺序的实体流。多个 Stream 会按切片顺序 连接成一个逻辑序列;Include、Skip 和 Limit 都在连接序列上统一生效。 Checkpoint 必须把源批次的输入 cursor 和已消费位置编码为可继续的 cursor, 以便逻辑页在批内截断时不会丢失余项。

type StreamState added in v1.1.0

type StreamState[C Cursor] struct {
	Current int
	Cursors []C
}

StreamState 是聚合流的可恢复位置。Current 指向下一次读取的流,Cursors 保存每个流各自的 continuation;已完成的流使用零 cursor。它只承载共享 算法需要的结构,不负责产品 cursor 的编码或 binding。

Jump to

Keyboard shortcuts

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