forked from aws/aws-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pagination.go
89 lines (78 loc) · 2 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
package api
import (
"encoding/json"
"fmt"
"os"
)
// Paginator keeps track of pagination configuration for an API operation.
type Paginator struct {
InputTokens interface{} `json:"input_token"`
OutputTokens interface{} `json:"output_token"`
LimitKey string `json:"limit_key"`
MoreResults string `json:"more_results"`
}
// InputTokensString returns output tokens formatted as a list
func (p *Paginator) InputTokensString() string {
str := p.InputTokens.([]string)
return fmt.Sprintf("%#v", str)
}
// OutputTokensString returns output tokens formatted as a list
func (p *Paginator) OutputTokensString() string {
str := p.OutputTokens.([]string)
return fmt.Sprintf("%#v", str)
}
// used for unmarshaling from the paginators JSON file
type paginationDefinitions struct {
*API
Pagination map[string]Paginator
}
// AttachPaginators attaches pagination configuration from filename to the API.
func (a *API) AttachPaginators(filename string) {
p := paginationDefinitions{API: a}
f, err := os.Open(filename)
defer f.Close()
if err != nil {
panic(err)
}
err = json.NewDecoder(f).Decode(&p)
if err != nil {
panic(err)
}
p.setup()
}
// setup runs post-processing on the paginator configuration.
func (p *paginationDefinitions) setup() {
for n, e := range p.Pagination {
if e.InputTokens == nil || e.OutputTokens == nil {
continue
}
paginator := e
switch t := paginator.InputTokens.(type) {
case string:
paginator.InputTokens = []string{t}
case []interface{}:
toks := []string{}
for _, e := range t {
s := e.(string)
toks = append(toks, s)
}
paginator.InputTokens = toks
}
switch t := paginator.OutputTokens.(type) {
case string:
paginator.OutputTokens = []string{t}
case []interface{}:
toks := []string{}
for _, e := range t {
s := e.(string)
toks = append(toks, s)
}
paginator.OutputTokens = toks
}
if o, ok := p.Operations[n]; ok {
o.Paginator = &paginator
} else {
panic("unknown operation for paginator " + n)
}
}
}