-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiter.go
349 lines (288 loc) · 7.72 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package ghiter
import (
"context"
"errors"
"fmt"
"iter"
"net/url"
"reflect"
"strconv"
"strings"
"time"
"github.com/google/go-github/v69/github"
)
type Iterator[T, O any] struct {
opt O
ctx context.Context
args []string
fn func(ctx context.Context, opt O) ([]T, *github.Response, error)
fn1 func(ctx context.Context, arg1 string, opt O) ([]T, *github.Response, error)
fn2 func(ctx context.Context, arg1, arg2 string, opt O) ([]T, *github.Response, error)
raw *github.Response
err error
}
func NewFromFn[T, O any](
fn func(ctx context.Context, opt O) ([]T, *github.Response, error),
) *Iterator[T, O] {
return &Iterator[T, O]{
fn: fn,
}
}
func NewFromFn1[T, O any](
fn1 func(ctx context.Context, arg1 string, opt O) ([]T, *github.Response, error),
arg1 string,
) *Iterator[T, O] {
return &Iterator[T, O]{
fn1: fn1,
args: []string{arg1},
}
}
func NewFromFn2[T, O any](
fn2 func(ctx context.Context, arg1, arg2 string, opt O) ([]T, *github.Response, error),
arg1 string,
arg2 string,
) *Iterator[T, O] {
return &Iterator[T, O]{
fn2: fn2,
args: []string{arg1, arg2},
}
}
func (it *Iterator[T, O]) Ctx(ctx context.Context) *Iterator[T, O] {
it.ctx = ctx
return it
}
func (it *Iterator[T, O]) Args(args ...string) *Iterator[T, O] {
it.args = args
return it
}
func (it *Iterator[T, O]) Opts(opt O) *Iterator[T, O] {
it.opt = opt
return it
}
func (it *Iterator[T, O]) Raw() *github.Response {
return it.raw
}
func (it *Iterator[T, O]) Err() error {
return it.err
}
func (it *Iterator[T, O]) All() iter.Seq[T] {
initialize(it)
return func(yield func(T) bool) {
if err := validate(it); err != nil {
it.err = err
return
}
for {
parts, resp, err := it.do()
it.raw = resp
if err != nil {
it.err = err
return
}
// push the result until yield, or an error occurs
for _, p := range parts {
if !yield(p) || contextErr(it) {
return
}
}
// no more results, break
if resp.NextPage == 0 {
return
}
// get the next page from the link header
links := ParseLinkHeader(resp.Header.Get("link"))
if next, found := links.FindByRel("next"); found {
nextURL, err := url.Parse(next.URL)
if err != nil {
it.err = err
return
}
vals := make(map[string]string)
for k, v := range nextURL.Query() {
vals[k] = v[0]
}
if err := updateOptions(it.opt, vals); err != nil {
it.err = err
return
}
}
}
}
}
func initialize[T, O any](it *Iterator[T, O]) {
// initialize context if nil
if it.ctx == nil {
it.ctx = context.Background()
}
// initialize options if nil
if reflect.ValueOf(it.opt).IsNil() {
optionPointerType := reflect.TypeOf(it.opt)
optionValue := reflect.New(optionPointerType.Elem())
if opt, ok := optionValue.Interface().(O); ok {
it.opt = opt
}
}
}
func validate[T, O any](it *Iterator[T, O]) error {
if it.fn == nil && it.fn1 == nil && it.fn2 == nil {
return errors.New("no func provided")
}
numOfArgs := len(it.args)
if it.fn1 != nil {
if numOfArgs != 1 {
args := strings.Join(it.args, ",")
return fmt.Errorf("wrong number of arguments: expected 1, got %d [%s]", numOfArgs, args)
}
if it.args[0] == "" {
return errors.New("empty argument[0]")
}
}
if it.fn2 != nil {
if numOfArgs != 2 {
args := strings.Join(it.args, ",")
return fmt.Errorf("wrong number of arguments: expected 2, got %d [%s]", numOfArgs, args)
}
if it.args[0] == "" {
return errors.New("empty argument[0]")
}
if it.args[1] == "" {
return errors.New("empty argument[1]")
}
}
return nil
}
func contextErr[T, O any](it *Iterator[T, O]) bool {
if err := it.ctx.Err(); err != nil {
it.err = err
return true
}
return false
}
func (it *Iterator[T, O]) do() ([]T, *github.Response, error) {
if it.fn != nil {
return it.fn(it.ctx, it.opt)
} else if it.fn1 != nil {
return it.fn1(it.ctx, it.args[0], it.opt)
} else if it.fn2 != nil {
return it.fn2(it.ctx, it.args[0], it.args[1], it.opt)
}
return nil, nil, errors.New("no func provided")
}
var (
stringTypePtr *string
intTypePtr *int
int64TypePtr *int64
boolTypePtr *bool
)
// updateOptions will update the github options based on the provided map and the `url` tag.
// If the field in the struct has a `url` tag it tries to set the value of the field from the one
// found in the map, if any.
func updateOptions(v any, m map[string]string) error {
valueOf := reflect.ValueOf(v)
typeOf := reflect.TypeOf(v)
if valueOf.Kind() == reflect.Pointer {
valueOf = valueOf.Elem()
typeOf = typeOf.Elem()
}
for i := 0; i < valueOf.NumField(); i++ {
structField := typeOf.Field(i)
fieldValue := valueOf.Field(i)
// if field is of type struct then iterate over the pointer
if structField.Type.Kind() == reflect.Struct {
if fieldValue.CanAddr() {
if err := updateOptions(fieldValue.Addr().Interface(), m); err != nil {
return err
}
}
}
// otherwise check if it has a 'url' tag
urlTag := structField.Tag.Get("url")
if urlTag == "" {
continue
}
if !fieldValue.IsValid() || !fieldValue.CanSet() {
continue
}
urlParam := strings.Split(urlTag, ",")[0]
v, found := m[urlParam]
if !found {
continue
}
switch fieldValue.Kind() {
// handle string
case reflect.String:
fieldValue.Set(reflect.ValueOf(v))
// handle numeric types (int, int8, int16, int32, int64)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if i, err := strconv.Atoi(v); err == nil {
fieldValue.SetInt(int64(i))
}
// handle bool
case reflect.Bool:
parsedBool, err := strconv.ParseBool(v)
if err != nil {
return fmt.Errorf("error while parsing string '%s' as bool: %s", v, err)
}
fieldValue.Set(reflect.ValueOf(parsedBool))
// handle pointers (*string, *int, *int64, *bool, *time.Time)
case reflect.Pointer:
switch fieldValue.Type() {
// handle *string
case reflect.TypeOf(stringTypePtr):
fieldValue.Set(reflect.ValueOf(&v))
// handle *int
case reflect.TypeOf(intTypePtr):
parsedInt, err := strconv.Atoi(v)
if err != nil {
return fmt.Errorf("error while parsing string '%s' as int: %s", v, err)
}
fieldValue.Set(reflect.ValueOf(&parsedInt))
// handle *int64
case reflect.TypeOf(int64TypePtr):
parsedInt64, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return fmt.Errorf("error while parsing string '%s' as int64: %s", v, err)
}
fieldValue.Set(reflect.ValueOf(&parsedInt64))
// handle *bool
case reflect.TypeOf(boolTypePtr):
parsedBool, err := strconv.ParseBool(v)
if err != nil {
return fmt.Errorf("error while parsing string '%s' as bool: %s", v, err)
}
fieldValue.Set(reflect.ValueOf(&parsedBool))
// handle *time.Time
case reflect.TypeOf(&time.Time{}):
layout := time.RFC3339
if len(v) == len(time.DateOnly) {
layout = time.DateOnly
}
result, err := time.Parse(layout, v)
if err != nil {
return fmt.Errorf("error while parsing string '%s' as time.Time: %s", v, err)
}
fieldValue.Set(reflect.ValueOf(&result))
default:
return fmt.Errorf("cannot set '%s' value to unknown pointer of '%s'", v, fieldValue.Type())
}
case reflect.Struct:
// handle time.Time
if fieldValue.Type() == reflect.TypeOf(time.Time{}) {
layout := time.RFC3339
if len(v) == len(time.DateOnly) {
layout = time.DateOnly
}
result, err := time.Parse(layout, v)
if err != nil {
return fmt.Errorf("error while parsing string '%s' as time.Time: %s", v, err)
}
fieldValue.Set(reflect.ValueOf(result))
} else {
return fmt.Errorf("cannot set '%s' value to unknown struct '%s'", v, fieldValue.Type())
}
default:
return fmt.Errorf("cannot set '%s' value to unknown type '%s'", v, fieldValue.Type())
}
}
return nil
}