Repository navigation
Expand file tree
/
Copy pathpaginator.go
More file actions
686 lines (618 loc) · 21.4 KB
/
Copy pathpaginator.go
File metadata and controls
686 lines (618 loc) · 21.4 KB
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
package api
import (
"fmt"
"github.com/andygello555/gotils/v2/slices"
mapset "github.com/deckarep/golang-set/v2"
"github.com/pkg/errors"
"reflect"
"strings"
"time"
)
// Afterable denotes whether a response type can be used in a Paginator for a Binding that takes an "after" parameter.
type Afterable interface {
// After returns the value of the "after" parameter that should be used for the next page of pagination. If this
// returns nil, then it is assumed that pagination has finished.
After() any
}
// Mergeable denotes whether a return type can be merged in a Paginator for a Binding. Instances of Mergeable can be
// used instead of reflect.Slice or reflect.Array types as a return type for a Paginator.
type Mergeable interface {
// Merge merges the given value into the Mergeable instance.
Merge(similar any) error
// HasMore returns true if there are more pages to fetch.
HasMore() bool
}
// Lenable is an interface that provides the Len interface for calculating the length of things.
type Lenable interface {
Len() int
}
type paginatorParamSet int
const (
unknownParamSet paginatorParamSet = iota
pageParamSet
afterParamSet
)
func (pps paginatorParamSet) String() string {
return strings.TrimPrefix(pps.Set().String(), "Set")
}
func (pps paginatorParamSet) GetPaginatorParamValue(params []BindingParam, resource any, page int) (map[string]any, error) {
switch pps {
case pageParamSet:
return map[string]any{"page": page}, nil
case afterParamSet:
if resource == nil {
for _, param := range params {
if param.name == "after" {
return map[string]any{"after": reflect.Zero(param.Type()).Interface()}, nil
}
}
return nil, fmt.Errorf("cannot find \"after\" parameter in parameters to use zero value for nil resource")
}
if afterable, ok := resource.(Afterable); ok {
return map[string]any{"after": afterable.After()}, nil
} else {
return nil, fmt.Errorf("cannot find next \"after\" parameter as return type %T is not Afterable", resource)
}
default:
return nil, fmt.Errorf("%v is not a valid paginatorParamSet", pps)
}
}
func (pps paginatorParamSet) InsertPaginatorParamValues(params []BindingParam, args []any, paginatorValues map[string]any) ([]any, error) {
ppsSet := pps.Set()
ppsSetUsed := mapset.NewSet[string]()
// Here we find out what indices that the paginator args should be passed in relative to the ordering of the params.
for paramNo, param := range params {
if pagVal, ok := paginatorValues[param.name]; ok {
args = slices.AddElems(args, []any{pagVal}, paramNo)
ppsSetUsed.Add(param.name)
} else if paramNo >= len(args) {
if param.required {
return args, fmt.Errorf(
"required parameter %q (no. %d) cannot be defaulted and not all paginator args have been inserted yet (%s remaining)",
param.name, paramNo, ppsSet.Difference(ppsSetUsed),
)
} else {
args = slices.AddElems(args, []any{param.defaultValue}, paramNo)
}
}
// If we have marked all paginator arguments to be inserted then we can break out of the loop
if ppsSet.Equal(ppsSetUsed) {
break
}
}
return args, nil
}
func (pps paginatorParamSet) Set() mapset.Set[string] {
switch pps {
case pageParamSet:
return mapset.NewSet("page")
case afterParamSet:
return mapset.NewSet("after")
default:
return mapset.NewSet[string]()
}
}
func (pps paginatorParamSet) Sets() []paginatorParamSet {
return []paginatorParamSet{pageParamSet, afterParamSet}
}
func checkPaginatorParams(params []BindingParam) paginatorParamSet {
paramNameSet := mapset.NewSet(slices.Comprehension(params, func(idx int, value BindingParam, arr []BindingParam) string {
return value.name
})...)
for _, pps := range unknownParamSet.Sets() {
if pps.Set().Difference(paramNameSet).Cardinality() == 0 {
return pps
}
}
return unknownParamSet
}
var limitParamNames = mapset.NewSet[string]("limit", "count")
// Paginator can fetch resources from a Binding that is paginated. Use NewPaginator or NewTypedPaginator to create a new
// one for a given Binding.
type Paginator[ResT any, RetT any] interface {
// Continue returns whether the Paginator can continue fetching more pages for the Binding. This will also return true
// when the Paginator is on the first page.
Continue() bool
// Page fetches the current page of results.
Page() RetT
// Next fetches the next page from the Binding. The result can be fetched using the Page method.
Next() error
// All returns all the return values for the Binding at once.
All() (RetT, error)
// Pages fetches the given number of pages from the Binding whilst appending each response slice together.
Pages(pages int) (RetT, error)
// Until keeps fetching pages until there are no more pages, or the given predicate function returns false.
Until(predicate func(paginator Paginator[ResT, RetT], pages RetT) bool) (RetT, error)
}
type typedPaginator[ResT any, RetT any] struct {
client Client
rateLimitedClient RateLimitedClient
usingRateLimitedClient bool
binding Binding[ResT, RetT]
params []BindingParam
paramSet paginatorParamSet
limitArg *float64
waitTime time.Duration
args []any
returnType reflect.Type
page int
currentPage RetT
}
func (p *typedPaginator[ResT, RetT]) mergeable() bool {
return p.returnType.Implements(reflect.TypeOf((*Mergeable)(nil)).Elem())
}
func (p *typedPaginator[ResT, RetT]) Continue() bool {
hasMore := false
if p.returnType.Implements(reflect.TypeOf((*Mergeable)(nil)).Elem()) {
if mergeable, ok := any(p.currentPage).(Mergeable); ok {
hasMore = mergeable.HasMore()
}
} else {
hasMore = reflect.ValueOf(p.currentPage).Len() > 0
}
return p.page == 1 || hasMore
}
func (p *typedPaginator[ResT, RetT]) Page() RetT { return p.currentPage }
func paginatorCheckRateLimit(
client Client,
waitTime time.Duration,
bindingName string,
limitArg **float64,
page int,
currentPage any,
params []BindingParam,
args []any,
) (ignoreFirstRequest bool, ok bool, err error) {
var rateLimitedClient RateLimitedClient
if rateLimitedClient, ok = client.(RateLimitedClient); ok {
rl := rateLimitedClient.LatestRateLimit(bindingName)
tries := 3
for rl == nil && tries > 0 {
rateLimitedClient.Log(fmt.Sprintf(
"Could not get latest rate limit for %q%v on page no. %d. Trying again in %s (%d tries left)...",
bindingName, args, page, waitTime.String(), tries,
))
time.Sleep(waitTime)
rl = rateLimitedClient.LatestRateLimit(bindingName)
tries--
}
if rl != nil && rl.Reset().After(time.Now().UTC()) {
sleepTime := rl.Reset().Sub(time.Now().UTC())
switch rl.Type() {
case RequestRateLimit:
if rl.Remaining() == 0 {
rateLimitedClient.Log(fmt.Sprintf(
"Latest request rate limit for %q%v has expired on page no. %d. Sleeping for %s until %s...",
bindingName, args, page, sleepTime.String(), rl.Reset(),
))
time.Sleep(sleepTime)
}
case ResourceRateLimit:
cont := func() bool {
return page == 1 || reflect.ValueOf(currentPage).Len() > 0
}
if reflect.ValueOf(currentPage).Len() > rl.Remaining() {
rateLimitedClient.Log(fmt.Sprintf(
"Latest resource rate limit for %q%v has expired on page no. %d. Sleeping for %s until %s...",
bindingName, args, page, sleepTime.String(), rl.Reset(),
))
time.Sleep(sleepTime)
} else if cont() {
if limitArg == nil {
for i, param := range params {
if !limitParamNames.Contains(param.name) {
continue
}
var argVal reflect.Value
if i < len(args) {
argVal = reflect.ValueOf(args[i])
} else if !param.required && !param.variadic {
argVal = reflect.ValueOf(param.defaultValue)
}
var val float64
switch {
case argVal.CanInt():
val = float64(argVal.Int())
case argVal.CanUint():
val = float64(argVal.Uint())
case argVal.CanFloat():
val = argVal.Float()
default:
continue
}
**limitArg = val
// Break out of the loop if we have found a limit argument
break
}
}
if **limitArg > float64(rl.Remaining()) {
rateLimitedClient.Log(fmt.Sprintf(
"Latest resource rate limit for %q%v has expired on page no. %d. Sleeping for %s until %s...",
bindingName, args, page, sleepTime.String(), rl.Reset(),
))
time.Sleep(sleepTime)
}
}
}
} else if page == 1 {
ignoreFirstRequest = true
} else if rl == nil {
rateLimitedClient.Log(fmt.Sprintf(
"Could not get the latest rate limit for %q%v on page no. %d",
bindingName, args, page,
))
err = fmt.Errorf(
"could not get the latest RateLimit/RateLimit has expired but we are on page %d, check Client.Run",
page,
)
return
} else {
rateLimitedClient.Log(fmt.Sprintf(
"Latest rate limit for %q is before the current time: %s - %s = %s, so we are going to execute the binding anyway",
bindingName, time.Now().UTC().Format("15:04:05"), rl.Reset().Format("15:04:05"), time.Now().UTC().Sub(rl.Reset()),
))
}
}
return
}
func (p *typedPaginator[ResT, RetT]) Next() (err error) {
var paginatorValues map[string]any
if paginatorValues, err = p.paramSet.GetPaginatorParamValue(p.params, p.currentPage, p.page); err != nil {
err = errors.Wrapf(
err, "cannot get paginator param values from %T value on page %d",
p.currentPage, p.page,
)
return
}
var args []any
if args, err = p.paramSet.InsertPaginatorParamValues(p.params, p.args, paginatorValues); err != nil {
err = errors.Wrapf(
err, "cannot insert paginator values (%v) into arguments for page %d",
paginatorValues, p.page,
)
}
var ignoreFirstRequest bool
execute := func() (ret RetT, err error) {
if ignoreFirstRequest, p.usingRateLimitedClient, err = paginatorCheckRateLimit(
p.client, p.waitTime, p.binding.Name(), &p.limitArg, p.page, p.currentPage, p.params, p.args,
); err != nil {
return
}
return p.binding.Execute(p.client, args...)
}
if p.currentPage, err = execute(); err != nil {
if !ignoreFirstRequest {
err = errors.Wrapf(err, "error occurred on page no. %d", p.page)
return
}
if p.currentPage, err = execute(); err != nil {
err = errors.Wrapf(
err, "error occurred on page no. %d, after ignoring the first request due to no rate limit",
p.page,
)
return
}
}
p.page++
if p.waitTime != 0 {
time.Sleep(p.waitTime)
}
return
}
func (p *typedPaginator[ResT, RetT]) merge(pages reflect.Value) (reflect.Value, error) {
mergeable := p.mergeable()
if mergeable {
if p.page == 2 {
pages = reflect.ValueOf(p.currentPage)
} else {
if err := pages.Interface().(Mergeable).Merge(p.Page()); err != nil {
return pages, err
}
}
} else {
pages = reflect.AppendSlice(pages, reflect.ValueOf(p.Page()))
}
return pages, nil
}
func (p *typedPaginator[ResT, RetT]) All() (RetT, error) {
pages := reflect.New(p.returnType).Elem()
for p.Continue() {
var err error
// Fetch the next page...
if err = p.Next(); err != nil {
return pages.Interface().(RetT), err
}
// ...merge the current page into the aggregation of all pages
if pages, err = p.merge(pages); err != nil {
return pages.Interface().(RetT), err
}
}
return pages.Interface().(RetT), nil
}
func (p *typedPaginator[ResT, RetT]) Pages(pageNo int) (RetT, error) {
pages := reflect.New(p.returnType).Elem()
for p.Continue() && p.page <= pageNo {
var err error
// Fetch the next page...
if err = p.Next(); err != nil {
return pages.Interface().(RetT), err
}
// ...merge the current page into the aggregation of all pages
if pages, err = p.merge(pages); err != nil {
return pages.Interface().(RetT), err
}
}
return pages.Interface().(RetT), nil
}
func (p *typedPaginator[ResT, RetT]) Until(predicate func(paginator Paginator[ResT, RetT], pages RetT) bool) (RetT, error) {
pages := reflect.New(p.returnType).Elem()
for p.Continue() && predicate(p, pages.Interface().(RetT)) {
var err error
// Fetch the next page...
if err = p.Next(); err != nil {
return pages.Interface().(RetT), err
}
// ...merge the current page into the aggregation of all pages
if pages, err = p.merge(pages); err != nil {
return pages.Interface().(RetT), err
}
}
return pages.Interface().(RetT), nil
}
// NewTypedPaginator creates a new type aware Paginator using the given Client, wait time.Duration, and arguments for
// the given Binding. The given Binding's Binding.Paginated method must return true, and the return type (RetT) of the
// Binding must be a slice-type, otherwise an appropriate error will be returned.
//
// The Paginator requires one of the following sets of BindingParam(s) taken by the given Binding:
// 1. ("page",): a singular page argument where each time Paginator.Next is called the page will be incremented
// 2. ("after",): a singular after argument where each time Paginator.Next is called the Afterable.After method will be
// called on the returned response and the returned value will be set as the "after" parameter for the next
// Binding.Execute. This requires the RetT to implement the Afterable interface.
//
// The sets of BindingParam(s) shown above are given in priority order. This means that a Binding that defines multiple
// BindingParam(s) that exist within these sets, only the first complete set will be taken.
//
// The args given to NewTypedPaginator should not include the set of BindingParam(s) (listed above), that are going to
// be used to paginate the binding.
//
// If the given Client also implements RateLimitedClient then the given waitTime argument will be ignored in favour of
// waiting (or not) until the RateLimit for the given Binding resets. If the RateLimit that is returned by
// RateLimitedClient.LatestRateLimit is of type ResourceRateLimit, and the Paginator is on the first page. The following
// parameter arguments will be checked for a limit/count value to see whether there is enough RateLimit.Remaining (in
// priority order):
// 1. "limit"
// 2. "count"
func NewTypedPaginator[ResT any, RetT any](client Client, waitTime time.Duration, binding Binding[ResT, RetT], args ...any) (paginator Paginator[ResT, RetT], err error) {
if !binding.Paginated() {
err = fmt.Errorf("cannot create typed Paginator as Binding is not pagenatable")
return
}
p := &typedPaginator[ResT, RetT]{
client: client,
binding: binding,
params: binding.Params(),
waitTime: waitTime,
args: args,
page: 1,
}
p.rateLimitedClient, p.usingRateLimitedClient = client.(RateLimitedClient)
if p.paramSet = checkPaginatorParams(p.params); p.paramSet == unknownParamSet {
err = fmt.Errorf(
"cannot create typed Paginator as we couldn't find any paginateable params, need one of the following sets of params %v",
unknownParamSet.Sets(),
)
return
}
returnType := reflect.ValueOf(new(RetT)).Elem().Type()
if returnType.Implements(reflect.TypeOf((*Mergeable)(nil)).Elem()) {
p.returnType = returnType
} else {
switch returnType.Kind() {
case reflect.Slice, reflect.Array:
p.returnType = returnType
default:
err = fmt.Errorf(
"cannot create typed Paginator for Binding[%v, %v] that has a non-slice/array return type",
reflect.ValueOf(new(ResT)).Elem().Type(), returnType,
)
return
}
}
paginator = p
return
}
// MustTypePaginate calls NewTypedPaginator with the given arguments and panics if an error occurs.
func MustTypePaginate[ResT any, RetT any](client Client, waitTime time.Duration, binding Binding[ResT, RetT], args ...any) (paginator Paginator[ResT, RetT]) {
var err error
if paginator, err = NewTypedPaginator(client, waitTime, binding, args...); err != nil {
panic(err)
}
return
}
type paginator struct {
client Client
rateLimitedClient RateLimitedClient
usingRateLimitedClient bool
binding *BindingWrapper
params []BindingParam
paramSet paginatorParamSet
limitArg *float64
waitTime time.Duration
args []any
returnType reflect.Type
page int
currentPage any
}
func (p *paginator) mergeable() bool {
return p.returnType.Implements(reflect.TypeOf((*Mergeable)(nil)).Elem())
}
func (p *paginator) Continue() bool {
hasMore := false
if p.returnType.Implements(reflect.TypeOf((*Mergeable)(nil)).Elem()) {
if mergeable, ok := p.currentPage.(Mergeable); ok {
hasMore = mergeable.HasMore()
}
} else {
hasMore = reflect.ValueOf(p.currentPage).Len() > 0
}
return p.page == 1 || hasMore
}
func (p *paginator) Page() any { return p.currentPage }
func (p *paginator) Next() (err error) {
var paginatorValues map[string]any
if paginatorValues, err = p.paramSet.GetPaginatorParamValue(p.params, p.currentPage, p.page); err != nil {
err = errors.Wrapf(
err, "cannot get paginator param values from %T value on page %d",
p.currentPage, p.page,
)
return
}
var args []any
if args, err = p.paramSet.InsertPaginatorParamValues(p.params, p.args, paginatorValues); err != nil {
err = errors.Wrapf(
err, "cannot insert paginator values (%v) into arguments for page %d",
paginatorValues, p.page,
)
}
//fmt.Println("paginatorValues", paginatorValues, len(paginatorValues))
//fmt.Println("args", args, len(args))
var ignoreFirstRequest bool
execute := func() (err error) {
if ignoreFirstRequest, p.usingRateLimitedClient, err = paginatorCheckRateLimit(
p.client, p.waitTime, p.binding.Name(), &p.limitArg, p.page, p.currentPage, p.params, p.args,
); err != nil {
return
}
if p.currentPage, err = p.binding.Execute(p.client, args...); err != nil {
err = errors.Wrapf(err, "error occurred on page no. %d", p.page)
}
return
}
if err = execute(); err != nil {
if !ignoreFirstRequest {
return
}
if err = execute(); err != nil {
err = errors.Wrapf(
err, "error occurred on page no. %d, after ignoring the first request due to no rate limit",
p.page,
)
return
}
}
p.page++
if p.waitTime != 0 {
time.Sleep(p.waitTime)
}
return
}
func (p *paginator) merge(pages reflect.Value) (reflect.Value, error) {
mergeable := p.mergeable()
if mergeable {
// If we have just fetched the first page then we will set pages to be the value of the first page
if p.page == 2 {
pages = reflect.ValueOf(p.currentPage)
} else {
if err := pages.Interface().(Mergeable).Merge(p.Page()); err != nil {
return reflect.ValueOf(nil), err
}
}
} else {
pages = reflect.AppendSlice(pages, reflect.ValueOf(p.Page()))
}
return pages, nil
}
func (p *paginator) All() (any, error) {
pages := reflect.New(p.returnType).Elem()
for p.Continue() {
var err error
// Fetch the next page...
if err = p.Next(); err != nil {
return pages.Interface(), err
}
// ...merge the current page into the aggregation of all pages
if pages, err = p.merge(pages); err != nil {
return pages.Interface(), err
}
}
return pages.Interface(), nil
}
func (p *paginator) Pages(pageNo int) (any, error) {
pages := reflect.New(p.returnType).Elem()
for p.Continue() && p.page <= pageNo {
var err error
// Fetch the next page...
if err = p.Next(); err != nil {
return pages.Interface(), err
}
// ...merge the current page into the aggregation of all pages
if pages, err = p.merge(pages); err != nil {
return pages.Interface(), err
}
}
return pages.Interface(), nil
}
func (p *paginator) Until(predicate func(paginator Paginator[any, any], pages any) bool) (any, error) {
pages := reflect.New(p.returnType).Elem()
for p.Continue() && predicate(p, pages.Interface()) {
var err error
// Fetch the next page...
if err = p.Next(); err != nil {
return pages.Interface(), err
}
// ...merge the current page into the aggregation of all pages
if pages, err = p.merge(pages); err != nil {
return pages.Interface(), err
}
}
return pages.Interface(), nil
}
// NewPaginator creates an un-typed Paginator for the given BindingWrapper. It creates a Paginator in a similar way as
// NewTypedPaginator, except the return type of the Paginator is []any. See NewTypedPaginator for more information on
// Paginator construction.
func NewPaginator(client Client, waitTime time.Duration, binding BindingWrapper, args ...any) (pag Paginator[any, any], err error) {
if !binding.Paginated() {
err = fmt.Errorf("cannot create a Paginator as Binding is not pagenatable")
return
}
p := &paginator{
client: client,
binding: &binding,
params: binding.Params(),
waitTime: waitTime,
args: args,
page: 1,
}
p.rateLimitedClient, p.usingRateLimitedClient = client.(RateLimitedClient)
if p.paramSet = checkPaginatorParams(p.params); p.paramSet == unknownParamSet {
err = fmt.Errorf(
"cannot create a Paginator as we couldn't find any paginateable params, need one of the following sets of params %v",
unknownParamSet.Sets(),
)
return
}
if binding.returnType.Implements(reflect.TypeOf((*Mergeable)(nil)).Elem()) {
p.returnType = binding.returnType
} else {
switch binding.returnType.Kind() {
case reflect.Slice, reflect.Array:
p.returnType = binding.returnType
default:
err = fmt.Errorf(
"cannot create a Paginator for Binding[%v, %v] that has a non-slice/array return type",
binding.responseType, binding.returnType,
)
return
}
}
pag = p
return
}
// MustPaginate calls NewPaginator for the given arguments and panics if an error occurs.
func MustPaginate(client Client, waitTime time.Duration, binding BindingWrapper, args ...any) (paginator Paginator[any, any]) {
var err error
if paginator, err = NewPaginator(client, waitTime, binding, args...); err != nil {
panic(err)
}
return
}