-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagination_cursor.go
More file actions
82 lines (71 loc) · 2.18 KB
/
pagination_cursor.go
File metadata and controls
82 lines (71 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Package gin 提供基于 Gin 的增强上下文与相关组件。
package gin
import "time"
// CursorPaginationParams 定义基于游标的分页请求参数。
type CursorPaginationParams struct {
Cursor string `json:"cursor,omitempty"`
Limit int `json:"limit"`
}
// CursorPageInfo 定义基于游标的分页响应元信息。
type CursorPageInfo struct {
NextCursor string `json:"next_cursor,omitempty"`
PrevCursor string `json:"prev_cursor,omitempty"`
Limit int `json:"limit,omitempty"`
HasMore bool `json:"has_more,omitempty"`
}
// CursorPaginatedResponse 定义带游标元信息的列表响应。
type CursorPaginatedResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
Cursor *CursorPageInfo `json:"cursor,omitempty"`
RequestID string `json:"request_id,omitempty"`
Timestamp int64 `json:"timestamp"`
}
// CursorPaginationOption 定义游标分页配置选项。
type CursorPaginationOption func(*cursorPaginationOptions)
type cursorPaginationOptions struct {
defaultLimit int
maxLimit int
}
// WithDefaultCursorLimit 设置游标分页的默认 limit。
func WithDefaultCursorLimit(limit int) CursorPaginationOption {
return func(o *cursorPaginationOptions) {
if limit > 0 {
o.defaultLimit = limit
}
}
}
// WithMaxCursorLimit 设置游标分页的最大 limit,<=0 表示不限制。
func WithMaxCursorLimit(limit int) CursorPaginationOption {
return func(o *cursorPaginationOptions) {
if limit > 0 {
o.maxLimit = limit
}
}
}
func defaultCursorPaginationOptions() *cursorPaginationOptions {
return &cursorPaginationOptions{
defaultLimit: 20,
maxLimit: 0,
}
}
func clampCursorLimit(limit, maxLimit int) int {
if limit < 1 {
limit = 1
}
if maxLimit > 0 && limit > maxLimit {
return maxLimit
}
return limit
}
func newCursorPaginatedResponse(code int, message string, data any, cursor *CursorPageInfo, requestID string) CursorPaginatedResponse {
return CursorPaginatedResponse{
Code: code,
Message: message,
Data: data,
Cursor: cursor,
RequestID: requestID,
Timestamp: time.Now().Unix(),
}
}