Discover Packages
github.com/forgeplex/appkit
page
package
Version:
v0.9.0
Opens a new window with list of versions in this module.
Published: Sep 5, 2026
License: MIT
Opens a new window with license information.
Imports: 6
Opens a new window with list of imports.
Imported by: ? 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.
Opens a new window with list of known importers.
Documentation
Documentation
¶
Package page 是列表端点的分页机制件:参数解析与校验、游标编解码、
统一响应信封、「多取一行判下一页」的标准技巧。
机制归框架、语义归域(同权限/租户的切分):
机制(本包):?limit 解析与值域校验(畸形即 422,不静默裁剪——
?limit=10⁶ 被悄悄压成 50 条时客户端以为拿到了全部)、游标的不透明
编解码(JSON → base64url)、items + next_cursor 信封、Trim 的
limit+1 技巧。
语义(域):cursor 结构体定义(排序键是什么每表不同)、sqlc 里的
keyset WHERE 子句(框架不碰 SQL 拼接)、要不要 total。排序键必须
唯一——时间戳做键必加 id 兜住同刻并列,否则翻页在并列处漏行。
游标对客户端是不透明契约:只回传、不解析、不构造。编码不签名:伪造
游标最多翻到别的页(keyset WHERE 照样跑全量过滤),不是安全边界,
不值得密钥管理成本。
Decode 解开 Encode 的产物。坏 base64、坏 JSON 都返回 422 INVALID_ARGUMENT
(客户端构造了不该构造的东西),调用方原样透传即可。JSON 合法但字段值
非法(如坏 uuid)不在此层——值域是域的知识,域 Decode 后自校验并自包 422。
Encode 把游标值编码为不透明字符串:JSON 序列化后 base64url(无 padding,
可直接进 query string)。域通常把排序键直接放进返回行,编码行本身即可。
func Trim[T any ](rows []T, limit int ) (items []T, next *T)
Trim 落实「limit+1 多取一行」技巧:查询用 limit+1 取回,len(rows) 超过
limit 即有下一页。返回截断后的 items 与下一页游标锚点 next——锚点是
返回的末行(不是被截掉的那行:keyset (排序键) < 游标 从末行之后继续,
用被截行会跳过整个本页末尾)。next 非 nil 时由调用方 Encode 进信封。
type List[T any ] struct {
Items []T `json:"items"`
NextCursor *string `json:"next_cursor,omitempty"`
}
List 是列表端点的统一响应信封:items + next_cursor。NextCursor 为 nil
(字段缺席)即最后一页;有下一页时是末行游标的 Encode 产物,客户端
原样回传 ?cursor=。
type Option func(*config)
Option 调整 Parse 的值域。宽窄行的合理页大小不同(审计流水 20、窄日志
200),用默认值域覆盖不了的列表再定制。
WithDefault 设置 ?limit 缺省时的页大小。
WithMax 设置 ?limit 的上限。n 大于等于默认值才有意义,但这里不拦——
配置错是启动期一眼能看见的事,不值得 API 面多一个错误分支。
type Params struct {
Limit int
Cursor string
}
Params 是 Parse 的产物。Limit 已校验归一(缺省用默认值,值域内原样);
Cursor 是原始字符串,形状校验留给 Decode[T]——类型在域手里,Parse 没有
类型参数。
Parse 从查询串解析分页参数(参数名固定 limit 与 cursor——参数名漂移
正是本包要消灭的)。畸形值一律 422 INVALID_ARGUMENT 而非静默纠正:
超上限被裁剪时客户端以为拿到了要的页,响亮失败让写错的一眼看见
(detail 带 field/value 与上限)。游标非空时原样透传,由域经 Decode
验形状后使用。
Source Files
¶
Click to show internal directories.
Click to hide internal directories.