Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit bad551f

Browse files
committed
Add support for multi-token pagination rules
Also refactor and add tests for truncation logic
1 parent 0eea626 commit bad551f

29 files changed

Lines changed: 511 additions & 583 deletions

File tree

‎aws/request.go‎

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ type Operation struct {
4545
}
4646

4747
type Paginator struct {
48-
InputToken string
49-
OutputToken string
48+
InputTokens []string
49+
OutputTokens []string
5050
LimitToken string
5151
TruncationToken string
5252
}
@@ -229,43 +229,55 @@ func (r *Request) Send() error {
229229
}
230230

231231
func (r *Request) HasNextPage() bool {
232-
return r.nextPageToken() != nil
232+
return r.nextPageTokens() != nil
233233
}
234234

235-
func (r *Request) nextPageToken() interface{} {
235+
func (r *Request) nextPageTokens() []interface{} {
236236
if r.Operation.Paginator == nil {
237237
return nil
238238
}
239-
v := awsutil.ValuesAtAnyPath(r.Data, r.Operation.OutputToken)
240-
if v != nil && len(v) > 0 {
241-
return v[0]
242-
}
243-
return nil
244-
}
245-
246-
func (r *Request) NextPage() *Request {
247-
token := r.nextPageToken()
248-
if token == nil {
249-
return nil
250-
}
251239

252240
if r.Operation.TruncationToken != "" {
253241
tr := awsutil.ValuesAtAnyPath(r.Data, r.Operation.TruncationToken)
254-
if tr == nil {
242+
if tr == nil || len(tr) == 0 {
255243
return nil
256-
} else if len(tr) > 0 {
257-
switch v := tr[0].(type) {
258-
case bool:
259-
if v == false {
260-
return nil
261-
}
244+
}
245+
switch v := tr[0].(type) {
246+
case bool:
247+
if v == false {
248+
return nil
262249
}
263250
}
264251
}
265252

253+
found := false
254+
tokens := make([]interface{}, len(r.Operation.OutputTokens))
255+
256+
for i, outtok := range r.Operation.OutputTokens {
257+
v := awsutil.ValuesAtAnyPath(r.Data, outtok)
258+
if v != nil && len(v) > 0 {
259+
found = true
260+
tokens[i] = v[0]
261+
}
262+
}
263+
264+
if found {
265+
return tokens
266+
}
267+
return nil
268+
}
269+
270+
func (r *Request) NextPage() *Request {
271+
tokens := r.nextPageTokens()
272+
if tokens == nil {
273+
return nil
274+
}
275+
266276
data := reflect.New(reflect.TypeOf(r.Data).Elem()).Interface()
267277
nr := NewRequest(r.Service, r.Operation, awsutil.CopyOf(r.Params), data)
268-
awsutil.SetValueAtAnyPath(nr.Params, nr.Operation.InputToken, token)
278+
for i, intok := range nr.Operation.InputTokens {
279+
awsutil.SetValueAtAnyPath(nr.Params, intok, tokens[i])
280+
}
269281
return nr
270282
}
271283

@@ -279,6 +291,9 @@ func (r *Request) EachPage(fn interface{}) error {
279291
if valfn.Kind() != reflect.Func {
280292
panic("expected function for EachPage()")
281293
}
294+
if valfn.Type().NumOut() > 0 && valfn.Type().Out(0).Kind() != reflect.Bool {
295+
panic("EachPage(fn) function must return bool if it returns a value")
296+
}
282297

283298
for page := r; page != nil; page = page.NextPage() {
284299
page.Send()
@@ -298,9 +313,6 @@ func (r *Request) EachPage(fn interface{}) error {
298313
out := valfn.Call(args)
299314

300315
if len(out) > 0 {
301-
if out[0].Kind() != reflect.Bool {
302-
panic("EachPage(fn) function must return bool if it returns a value")
303-
}
304316
shouldContinue = out[0].Bool()
305317
}
306318

‎aws/request_pagination_test.go‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/awslabs/aws-sdk-go/aws"
77
"github.com/awslabs/aws-sdk-go/internal/test/unit"
88
"github.com/awslabs/aws-sdk-go/service/dynamodb"
9+
"github.com/awslabs/aws-sdk-go/service/s3"
910
"github.com/stretchr/testify/assert"
1011
)
1112

@@ -208,6 +209,90 @@ func TestPaginationEarlyExit(t *testing.T) {
208209
assert.Nil(t, err)
209210
}
210211

212+
func TestEachPagePanics(t *testing.T) {
213+
db := dynamodb.New(nil)
214+
db.Handlers.Send.Clear() // mock sending
215+
req, _ := db.ListTablesRequest(nil)
216+
217+
assert.Panics(t, func() { req.EachPage(0) }) // must be a function
218+
assert.Panics(t, func() { // must return bool
219+
req.EachPage(func(p *dynamodb.ListTablesOutput, last bool) string {
220+
return ""
221+
})
222+
})
223+
}
224+
225+
func TestSkipPagination(t *testing.T) {
226+
client := s3.New(nil)
227+
client.Handlers.Send.Clear() // mock sending
228+
client.Handlers.Unmarshal.Clear()
229+
client.Handlers.UnmarshalMeta.Clear()
230+
client.Handlers.ValidateResponse.Clear()
231+
client.Handlers.Unmarshal.PushBack(func(r *aws.Request) {
232+
r.Data = &s3.HeadBucketOutput{}
233+
})
234+
235+
req, _ := client.HeadBucketRequest(&s3.HeadBucketInput{Bucket: aws.String("bucket")})
236+
237+
numPages, gotToEnd := 0, false
238+
req.EachPage(func(p *s3.HeadBucketOutput, last bool) {
239+
numPages++
240+
if last {
241+
gotToEnd = true
242+
}
243+
})
244+
assert.Equal(t, 1, numPages)
245+
assert.True(t, gotToEnd)
246+
}
247+
248+
// Use S3 for simplicity
249+
func TestPaginationTruncation(t *testing.T) {
250+
count := 0
251+
client := s3.New(nil)
252+
253+
reqNum := &count
254+
resps := []*s3.ListObjectsOutput{
255+
&s3.ListObjectsOutput{IsTruncated: aws.Boolean(true), Contents: []*s3.Object{&s3.Object{Key: aws.String("Key1")}}},
256+
&s3.ListObjectsOutput{IsTruncated: aws.Boolean(true), Contents: []*s3.Object{&s3.Object{Key: aws.String("Key2")}}},
257+
&s3.ListObjectsOutput{IsTruncated: aws.Boolean(false), Contents: []*s3.Object{&s3.Object{Key: aws.String("Key3")}}},
258+
&s3.ListObjectsOutput{IsTruncated: aws.Boolean(true), Contents: []*s3.Object{&s3.Object{Key: aws.String("Key4")}}},
259+
}
260+
261+
client.Handlers.Send.Clear() // mock sending
262+
client.Handlers.Unmarshal.Clear()
263+
client.Handlers.UnmarshalMeta.Clear()
264+
client.Handlers.ValidateResponse.Clear()
265+
client.Handlers.Unmarshal.PushBack(func(r *aws.Request) {
266+
r.Data = resps[*reqNum]
267+
*reqNum++
268+
})
269+
270+
params := &s3.ListObjectsInput{Bucket: aws.String("bucket")}
271+
272+
results := []string{}
273+
err := client.ListObjectsPages(params, func(p *s3.ListObjectsOutput, last bool) bool {
274+
results = append(results, *p.Contents[0].Key)
275+
return true
276+
})
277+
278+
assert.Equal(t, []string{"Key1", "Key2", "Key3"}, results)
279+
assert.Nil(t, err)
280+
281+
// Try again without truncation token at all
282+
count = 0
283+
resps[1].IsTruncated = nil
284+
resps[2].IsTruncated = aws.Boolean(true)
285+
results = []string{}
286+
err = client.ListObjectsPages(params, func(p *s3.ListObjectsOutput, last bool) bool {
287+
results = append(results, *p.Contents[0].Key)
288+
return true
289+
})
290+
291+
assert.Equal(t, []string{"Key1", "Key2"}, results)
292+
assert.Nil(t, err)
293+
294+
}
295+
211296
// Benchmarks
212297
var benchResps = []*dynamodb.ListTablesOutput{
213298
&dynamodb.ListTablesOutput{TableNames: []*string{aws.String("TABLE"), aws.String("NXT")}, LastEvaluatedTableName: aws.String("NXT")},

‎internal/model/api/operation.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ func (c *{{ .API.StructName }}) {{ .ExportedName }}Request(` +
6262
{{ if ne .HTTP.Method "" }}HTTPMethod: "{{ .HTTP.Method }}",
6363
{{ end }}{{ if ne .HTTP.RequestURI "" }}HTTPPath: "{{ .HTTP.RequestURI }}",
6464
{{ end }}{{ if .Paginator }}Paginator: &aws.Paginator{
65-
InputToken: "{{ .Paginator.InputToken }}",
66-
OutputToken: "{{ .Paginator.OutputToken }}",
65+
InputTokens: {{ .Paginator.InputTokensString }},
66+
OutputTokens: {{ .Paginator.OutputTokensString }},
6767
LimitToken: "{{ .Paginator.LimitKey }}",
6868
TruncationToken: "{{ .Paginator.MoreResults }}",
6969
},

‎internal/model/api/pagination.go‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,27 @@ package api
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"os"
67
)
78

89
type Paginator struct {
9-
InputToken string `json:"input_token"`
10-
OutputToken string `json:"output_token"`
11-
LimitKey string `json:"limit_key"`
12-
MoreResults string `json:"more_results"`
10+
InputTokens interface{} `json:"input_token"`
11+
OutputTokens interface{} `json:"output_token"`
12+
LimitKey string `json:"limit_key"`
13+
MoreResults string `json:"more_results"`
14+
}
15+
16+
// InputTokensString returns output tokens formatted as a list
17+
func (p *Paginator) InputTokensString() string {
18+
str := p.InputTokens.([]string)
19+
return fmt.Sprintf("%#v", str)
20+
}
21+
22+
// OutputTokensString returns output tokens formatted as a list
23+
func (p *Paginator) OutputTokensString() string {
24+
str := p.OutputTokens.([]string)
25+
return fmt.Sprintf("%#v", str)
1326
}
1427

1528
// used for unmarshaling from the paginators JSON file
@@ -36,7 +49,34 @@ func (a *API) AttachPaginators(filename string) {
3649

3750
func (p *paginationDefinitions) setup() {
3851
for n, e := range p.Pagination {
52+
if e.InputTokens == nil || e.OutputTokens == nil {
53+
continue
54+
}
3955
paginator := e
56+
57+
switch t := paginator.InputTokens.(type) {
58+
case string:
59+
paginator.InputTokens = []string{t}
60+
case []interface{}:
61+
toks := []string{}
62+
for _, e := range t {
63+
s := e.(string)
64+
toks = append(toks, s)
65+
}
66+
paginator.InputTokens = toks
67+
}
68+
switch t := paginator.OutputTokens.(type) {
69+
case string:
70+
paginator.OutputTokens = []string{t}
71+
case []interface{}:
72+
toks := []string{}
73+
for _, e := range t {
74+
s := e.(string)
75+
toks = append(toks, s)
76+
}
77+
paginator.OutputTokens = toks
78+
}
79+
4080
n = p.ExportableName(n)
4181
if o, ok := p.Operations[n]; ok {
4282
o.Paginator = &paginator

‎service/autoscaling/api.go‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,8 @@ func (c *AutoScaling) DescribeAutoScalingGroupsRequest(input *DescribeAutoScalin
519519
HTTPMethod: "POST",
520520
HTTPPath: "/",
521521
Paginator: &aws.Paginator{
522-
InputToken: "NextToken",
523-
OutputToken: "NextToken",
522+
InputTokens: []string{"NextToken"},
523+
OutputTokens: []string{"NextToken"},
524524
LimitToken: "MaxRecords",
525525
TruncationToken: "",
526526
},
@@ -567,8 +567,8 @@ func (c *AutoScaling) DescribeAutoScalingInstancesRequest(input *DescribeAutoSca
567567
HTTPMethod: "POST",
568568
HTTPPath: "/",
569569
Paginator: &aws.Paginator{
570-
InputToken: "NextToken",
571-
OutputToken: "NextToken",
570+
InputTokens: []string{"NextToken"},
571+
OutputTokens: []string{"NextToken"},
572572
LimitToken: "MaxRecords",
573573
TruncationToken: "",
574574
},
@@ -648,8 +648,8 @@ func (c *AutoScaling) DescribeLaunchConfigurationsRequest(input *DescribeLaunchC
648648
HTTPMethod: "POST",
649649
HTTPPath: "/",
650650
Paginator: &aws.Paginator{
651-
InputToken: "NextToken",
652-
OutputToken: "NextToken",
651+
InputTokens: []string{"NextToken"},
652+
OutputTokens: []string{"NextToken"},
653653
LimitToken: "MaxRecords",
654654
TruncationToken: "",
655655
},
@@ -796,8 +796,8 @@ func (c *AutoScaling) DescribeNotificationConfigurationsRequest(input *DescribeN
796796
HTTPMethod: "POST",
797797
HTTPPath: "/",
798798
Paginator: &aws.Paginator{
799-
InputToken: "NextToken",
800-
OutputToken: "NextToken",
799+
InputTokens: []string{"NextToken"},
800+
OutputTokens: []string{"NextToken"},
801801
LimitToken: "MaxRecords",
802802
TruncationToken: "",
803803
},
@@ -840,8 +840,8 @@ func (c *AutoScaling) DescribePoliciesRequest(input *DescribePoliciesInput) (req
840840
HTTPMethod: "POST",
841841
HTTPPath: "/",
842842
Paginator: &aws.Paginator{
843-
InputToken: "NextToken",
844-
OutputToken: "NextToken",
843+
InputTokens: []string{"NextToken"},
844+
OutputTokens: []string{"NextToken"},
845845
LimitToken: "MaxRecords",
846846
TruncationToken: "",
847847
},
@@ -887,8 +887,8 @@ func (c *AutoScaling) DescribeScalingActivitiesRequest(input *DescribeScalingAct
887887
HTTPMethod: "POST",
888888
HTTPPath: "/",
889889
Paginator: &aws.Paginator{
890-
InputToken: "NextToken",
891-
OutputToken: "NextToken",
890+
InputTokens: []string{"NextToken"},
891+
OutputTokens: []string{"NextToken"},
892892
LimitToken: "MaxRecords",
893893
TruncationToken: "",
894894
},
@@ -970,8 +970,8 @@ func (c *AutoScaling) DescribeScheduledActionsRequest(input *DescribeScheduledAc
970970
HTTPMethod: "POST",
971971
HTTPPath: "/",
972972
Paginator: &aws.Paginator{
973-
InputToken: "NextToken",
974-
OutputToken: "NextToken",
973+
InputTokens: []string{"NextToken"},
974+
OutputTokens: []string{"NextToken"},
975975
LimitToken: "MaxRecords",
976976
TruncationToken: "",
977977
},
@@ -1014,8 +1014,8 @@ func (c *AutoScaling) DescribeTagsRequest(input *DescribeTagsInput) (req *aws.Re
10141014
HTTPMethod: "POST",
10151015
HTTPPath: "/",
10161016
Paginator: &aws.Paginator{
1017-
InputToken: "NextToken",
1018-
OutputToken: "NextToken",
1017+
InputTokens: []string{"NextToken"},
1018+
OutputTokens: []string{"NextToken"},
10191019
LimitToken: "MaxRecords",
10201020
TruncationToken: "",
10211021
},

0 commit comments

Comments
 (0)