-
Notifications
You must be signed in to change notification settings - Fork 2
/
iter.go
executable file
·198 lines (161 loc) · 4.45 KB
/
iter.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
package files_sdk
import (
"github.com/Files-com/files-sdk-go/v2/lib"
)
type ListParams struct {
Page int64 `json:"page,omitempty" url:"page,omitempty" required:"false"`
PerPage int64 `json:"per_page,omitempty" url:"per_page,omitempty" required:"false"`
Cursor string `json:"cursor,omitempty" url:"cursor,omitempty" required:"false"`
MaxPages int64 `json:"-" url:"-"`
}
// ListParamsContainer is a general interface for which all list parameter
// structs should comply. They achieve this by embedding a ListParams struct
// and inheriting its implementation of this interface.
type ListParamsContainer interface {
GetListParams() *ListParams
}
// GetListParams returns a ListParams struct (itself). It exists because any
// structs that embed ListParams will inherit it, and thus implement the
// ListParamsContainer interface.
func (p *ListParams) GetListParams() *ListParams {
return p
}
type OnPageError func(error) (*[]interface{}, error)
type Query func(params lib.Values, opts ...RequestResponseOption) (*[]interface{}, string, error)
type IterI interface {
Next() bool
Current() interface{}
Err() error
}
var _ IterI = (*Iter)(nil)
type TypedIterI[T any] interface {
Next() bool
Current() interface{}
Resource() T
Err() error
}
type IterPagingI interface {
IterI
EOFPage() bool
}
var _ IterPagingI = (*Iter)(nil)
type ResourceIterator interface {
Iterate(interface{}, ...RequestResponseOption) (IterI, error)
}
type ReloadIterator interface {
Reload(opts ...RequestResponseOption) IterI
}
var _ ReloadIterator = (*Iter)(nil)
type ResourceLoader interface {
LoadResource(interface{}, ...RequestResponseOption) (interface{}, error)
}
type Identifier interface {
Identifier() interface{}
}
type Iterable interface {
Iterable() bool
}
type Iter struct {
Query
ListParams ListParamsContainer
Params []interface{}
CurrentIndex int
Values *[]interface{}
Cursor string
Error error
OnPageError
requestResponseOptions []RequestResponseOption
}
// Err returns the error, if any,
// that caused the Iter to stop.
// It must be inspected
// after Next returns false.
func (i *Iter) Err() error {
return i.Error
}
func (i *Iter) Current() interface{} {
return (*i.Values)[i.CurrentIndex]
}
func (i *Iter) GetParams() *ListParams {
return i.ListParams.GetListParams()
}
func (i *Iter) ExportParams() (lib.ExportValues, error) {
p := lib.Params{Params: i.GetParams()}
paramValues, err := p.ToValues()
if err != nil {
return lib.ExportValues{}, err
}
listParamValues, err := lib.Params{Params: i.ListParams}.ToValues()
if err != nil {
return lib.ExportValues{}, err
}
for key, value := range paramValues {
listParamValues.Set(key, value[0])
}
if i.GetCursor() != "" {
listParamValues.Del("page")
}
return lib.ExportValues{Values: listParamValues}, nil
}
func (i *Iter) GetPage() bool {
if i.GetParams().MaxPages != 0 && i.GetParams().Page == i.GetParams().MaxPages {
return false
}
i.CurrentIndex = 0
i.GetParams().Page += 1
if i.GetParams().Page == 2 && i.Cursor == "" {
return false
}
params, _ := i.ExportParams()
i.Values, i.Cursor, i.Error = i.Query(params, i.requestResponseOptions...)
i.SetCursor(i.Cursor)
if i.Error != nil && i.OnPageError != nil {
i.Values, i.Error = i.OnPageError(i.Error)
}
return i.Error == nil && len(*i.Values) != 0
}
func (i *Iter) EOFPage() bool {
return len(*i.Values) == i.CurrentIndex+1
}
func (i *Iter) Paging() bool {
return true
}
func (i *Iter) GetCursor() string {
return i.GetParams().Cursor
}
func (i *Iter) SetCursor(cursor string) {
i.GetParams().Cursor = cursor
i.Cursor = cursor
}
// Next iterates the results in i.Current() or i.`ResourceName`().
// It returns true until there are no results remaining.
// To adjust the number of results set ListParams.PerPage.
// To have it auto-paginate set ListParams.MaxPages, default is 1.
//
// To iterate over all results use the following pattern.
//
// for i.Next() {
// i.Current()
// }
func (i *Iter) Next() bool {
if i.Values == nil {
return i.GetPage() && len(*i.Values) > 0
} else if len(*i.Values) > i.CurrentIndex+1 {
i.CurrentIndex += 1
return true
}
if i.EOFPage() {
return i.GetPage()
}
return false
}
func (i *Iter) NextPage() bool {
return i.Cursor != ""
}
// Reload ignores any id passed in and creates a new reset Iter
func (i *Iter) Reload(opts ...RequestResponseOption) IterI {
newIter := *i
newIter.ListParams = &ListParams{}
newIter.requestResponseOptions = opts
return &newIter
}