-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpagination.go
170 lines (150 loc) · 3.66 KB
/
pagination.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package coze
type BasePaged[T any] interface {
Err() error
Items() []*T
Current() *T
Next() bool
HasMore() bool
}
type NumberPaged[T any] interface {
BasePaged[T]
Total() int
}
type LastIDPaged[T any] interface {
BasePaged[T]
GetLastID() string
}
type pageRequest struct {
PageToken string `json:"page_token,omitempty"`
PageNum int `json:"page_num,omitempty"`
PageSize int `json:"page_size,omitempty"`
}
type pageResponse[T any] struct {
HasMore bool `json:"has_more"`
Total int `json:"total"`
Data []*T `json:"data"`
LastID string `json:"last_id,omitempty"`
NextID string `json:"next_id,omitempty"`
LogID string `json:"log_id,omitempty"`
}
type basePager[T any] struct {
pageFetcher PageFetcher[T]
pageSize int
currentPage *pageResponse[T]
currentIndex int
currentPageNum int
cur *T
err error
}
func (p *basePager[T]) Err() error {
return p.err
}
func (p *basePager[T]) Items() []*T {
return ptrValue(p.currentPage).Data
}
func (p *basePager[T]) Current() *T {
return p.cur
}
func (p *basePager[T]) Total() int {
return ptrValue(p.currentPage).Total
}
func (p *basePager[T]) HasMore() bool {
return ptrValue(p.currentPage).HasMore
}
// PageFetcher interface
type PageFetcher[T any] func(request *pageRequest) (*pageResponse[T], error)
// NumberPaged implementation
type implNumberPaged[T any] struct {
basePager[T]
}
func NewNumberPaged[T any](fetcher PageFetcher[T], pageSize, pageNum int) (NumberPaged[T], error) {
if pageNum <= 0 {
pageNum = 1
}
paginator := &implNumberPaged[T]{basePager: basePager[T]{pageFetcher: fetcher, pageSize: pageSize, currentPageNum: pageNum}}
err := paginator.fetchNextPage()
if err != nil {
return nil, err
}
return paginator, nil
}
func (p *implNumberPaged[T]) fetchNextPage() error {
request := &pageRequest{PageNum: p.currentPageNum, PageSize: p.pageSize}
var err error
p.currentPage, err = p.pageFetcher(request)
if err != nil {
return err
}
p.currentIndex = 0
p.currentPageNum++
return nil
}
func (p *implNumberPaged[T]) Next() bool {
if p.currentIndex < len(ptrValue(p.currentPage).Data) {
p.cur = p.currentPage.Data[p.currentIndex]
p.currentIndex++
return true
}
if p.currentPage.HasMore {
err := p.fetchNextPage()
if err != nil {
p.err = err
return false
}
if len(p.currentPage.Data) == 0 {
return false
}
p.cur = p.currentPage.Data[p.currentIndex]
p.currentIndex++
return true
}
return false
}
// TokenPaged implementation
type implLastIDPaged[T any] struct {
basePager[T]
pageToken *string
}
func NewLastIDPaged[T any](fetcher PageFetcher[T], pageSize int, nextID *string) (LastIDPaged[T], error) {
paginator := &implLastIDPaged[T]{basePager: basePager[T]{pageFetcher: fetcher, pageSize: pageSize}, pageToken: nextID}
err := paginator.fetchNextPage()
if err != nil {
return nil, err
}
return paginator, nil
}
func (p *implLastIDPaged[T]) fetchNextPage() error {
request := &pageRequest{PageToken: ptrValue(p.pageToken), PageSize: p.pageSize}
var err error
p.currentPage, err = p.pageFetcher(request)
if err != nil {
return err
}
p.currentIndex = 0
p.pageToken = &p.currentPage.NextID
return nil
}
func (p *implLastIDPaged[T]) Next() bool {
if p.currentIndex < len(ptrValue(p.currentPage).Data) {
p.cur = p.currentPage.Data[p.currentIndex]
p.currentIndex++
return true
}
if p.currentPage.HasMore {
err := p.fetchNextPage()
if err != nil {
p.err = err
return false
}
if len(p.currentPage.Data) == 0 {
return false
}
p.cur = p.currentPage.Data[p.currentIndex]
p.currentIndex++
return true
}
return false
}
func (p *implLastIDPaged[T]) GetLastID() string {
return p.currentPage.LastID
}