-
Notifications
You must be signed in to change notification settings - Fork 2
/
iter.go
executable file
·148 lines (123 loc) · 3.4 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
package 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 Values) (*[]interface{}, string, error)
type IterI interface {
Next() bool
Current() interface{}
Err() error
}
type IterPagingI interface {
IterI
EOFPage() bool
}
type Iter struct {
Query
ListParams ListParamsContainer
Params []interface{}
CurrentIndex int
Values *[]interface{}
Cursor string
Error error
OnPageError
}
// 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() (ExportValues, error) {
p := Params{Params: i.GetParams()}
paramValues, err := p.ToValues()
if err != nil {
return ExportValues{}, err
}
listParamValues, _ := Params{Params: i.ListParams}.ToValues()
for key, value := range paramValues {
listParamValues.Set(key, value[0])
}
if i.GetCursor() != "" {
listParamValues.Del("page")
}
return 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.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 != ""
}