-
Notifications
You must be signed in to change notification settings - Fork 8
/
pagination.go
228 lines (202 loc) · 6.08 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package pagination
import (
"context"
"fmt"
"github.com/ARM-software/golang-utils/utils/commonerrors"
"github.com/ARM-software/golang-utils/utils/parallelisation"
)
type AbstractPaginator struct {
currentPage IStaticPage
currentPageIterator IIterator
cancellationStore *parallelisation.CancelFunctionStore
fetchNextFunc func(context.Context, IStaticPage) (IStaticPage, error)
ctx context.Context
}
func (a *AbstractPaginator) Close() error {
a.Stop()()
return nil
}
func (a *AbstractPaginator) HasNext() bool {
if parallelisation.DetermineContextError(a.ctx) != nil {
return false
}
currentIt, err := a.FetchCurrentPageIterator()
if err != nil {
return false
}
if currentIt.HasNext() {
return true
}
currentPage, err := a.FetchCurrentPage()
if err != nil {
return false
}
if !currentPage.HasNext() {
return false
}
err = a.fetchNextPage()
if err != nil {
return false
}
return a.HasNext()
}
func (a *AbstractPaginator) FetchNextPage(ctx context.Context, currentPage IStaticPage) (IStaticPage, error) {
return a.fetchNextFunc(ctx, currentPage)
}
func (a *AbstractPaginator) fetchNextPage() (err error) {
currentPage, err := a.FetchCurrentPage()
if err != nil {
return
}
if !currentPage.HasNext() {
return
}
newPage, err := a.FetchNextPage(a.ctx, currentPage)
if err != nil {
return
}
err = a.setCurrentPage(newPage)
return
}
func (a *AbstractPaginator) GetNext() (item interface{}, err error) {
err = parallelisation.DetermineContextError(a.ctx)
if err != nil {
return
}
if !a.HasNext() {
err = fmt.Errorf("%w: there is not any next item", commonerrors.ErrNotFound)
return
}
currentIt, err := a.FetchCurrentPageIterator()
if err != nil {
return
}
item, err = currentIt.GetNext()
return
}
func (a *AbstractPaginator) Stop() context.CancelFunc {
return a.cancellationStore.Cancel
}
func (a *AbstractPaginator) setCurrentPage(page IStaticPage) (err error) {
a.currentPage = page
a.currentPageIterator = nil
if page != nil {
a.currentPageIterator, err = page.GetItemIterator()
}
return
}
func (a *AbstractPaginator) SetCurrentPage(page IStaticPage) (err error) {
if page == nil {
err = fmt.Errorf("%w: missing page", commonerrors.ErrUndefined)
return
}
err = a.setCurrentPage(page)
return
}
func (a *AbstractPaginator) FetchCurrentPage() (page IStaticPage, err error) {
page = a.currentPage
if page == nil {
err = fmt.Errorf("%w: missing page", commonerrors.ErrUndefined)
}
return
}
func (a *AbstractPaginator) FetchCurrentPageIterator() (it IIterator, err error) {
it = a.currentPageIterator
if it == nil {
err = fmt.Errorf("%w: missing page iterator", commonerrors.ErrUndefined)
}
return
}
func (a *AbstractPaginator) GetContext() context.Context {
return a.ctx
}
func NewAbstractPaginator(ctx context.Context, firstPage IStaticPage, fetchNextFunc func(context.Context, IStaticPage) (IStaticPage, error)) (p *AbstractPaginator, err error) {
store := parallelisation.NewCancelFunctionsStore()
cancelCtx, cancel := context.WithCancel(ctx)
store.RegisterCancelFunction(cancel)
p = &AbstractPaginator{
cancellationStore: store,
fetchNextFunc: fetchNextFunc,
ctx: cancelCtx,
}
err = p.setCurrentPage(firstPage)
return
}
// DynamicPagePaginator defines a paginator over dynamic pages i.e. pages from which it is possible to access the next one.
type DynamicPagePaginator struct {
AbstractPaginator
}
func (d *DynamicPagePaginator) GetCurrentPage() (dynamicPage IPage, err error) {
p, err := d.FetchCurrentPage()
if err != nil {
return
}
dynamicPage, err = toDynamicPage(p)
return
}
func toDynamicPage(page IStaticPage) (dynamicPage IPage, err error) {
if page == nil {
return
}
dynamicPage, ok := page.(IPage)
if !ok {
err = fmt.Errorf("%w: current page is not dynamic i.e. it is not possible to fetch next pages from it", commonerrors.ErrInvalid)
}
return
}
func newDynamicPagePaginator(ctx context.Context, firstPage IPage) (p IPaginator, err error) {
a, err := NewAbstractPaginator(ctx, firstPage, func(fCtx context.Context, current IStaticPage) (nextPage IStaticPage, err error) {
p, err := toDynamicPage(current)
if err != nil {
return
}
nextPage, err = p.GetNext(fCtx)
return
})
if err != nil {
return
}
p = &DynamicPagePaginator{
AbstractPaginator: *a,
}
return
}
// StaticPagePaginator defines a paginator over static pages i.e. pages from which it is not possible to access the next one and the paginator must define how to fetch it.
type StaticPagePaginator struct {
AbstractPaginator
}
func (d *StaticPagePaginator) GetCurrentPage() (IStaticPage, error) {
return d.FetchCurrentPage()
}
func newStaticPagePaginator(ctx context.Context, firstPage IStaticPage, fetchNextPageFunc func(context.Context, IStaticPage) (IStaticPage, error)) (p IPaginatorAndPageFetcher, er error) {
a, err := NewAbstractPaginator(ctx, firstPage, fetchNextPageFunc)
if err != nil {
return
}
p = &StaticPagePaginator{
AbstractPaginator: *a,
}
return
}
// NewCollectionPaginator creates a paginator over a collection of dynamic pages.
func NewCollectionPaginator(ctx context.Context, fetchFirstPageFunc func(context.Context) (IPage, error)) (paginator IPaginator, err error) {
firstPage, err := fetchFirstPageFunc(ctx)
if err != nil {
return
}
paginator, err = newDynamicPagePaginator(ctx, firstPage)
return
}
// NewStaticPagePaginator creates a paginator over a collection but only dealing with static pages i.e. pages from which it is not possible to access the next one and the paginator must define how to fetch them using the fetchNextPageFunc function.
func NewStaticPagePaginator(ctx context.Context, fetchFirstPageFunc func(context.Context) (IStaticPage, error), fetchNextPageFunc func(context.Context, IStaticPage) (IStaticPage, error)) (paginator IPaginatorAndPageFetcher, err error) {
firstPage, err := fetchFirstPageFunc(ctx)
if err != nil {
return
}
paginator, err = newStaticPagePaginator(ctx, firstPage, fetchNextPageFunc)
return
}