-
Notifications
You must be signed in to change notification settings - Fork 0
/
batchgetpaginator.go
118 lines (98 loc) · 3.54 KB
/
batchgetpaginator.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
package dygo
import (
"context"
"fmt"
"reflect"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
// batchGetItemPaginatorOptions is the paginator options for BatchGetItem
type batchGetItemPaginatorOptions struct {
// Set to true if pagination should stop if the service returns a pagination token
// that matches the most recent token provided to the service.
stopOnDuplicateToken bool
}
// batchGetItemPaginator is a paginator for BatchGetItem
type batchGetItemPaginator struct {
options batchGetItemPaginatorOptions
client batchGetItemAPIClient
params *dynamodb.BatchGetItemInput
firstPage bool
requestItems map[string]types.KeysAndAttributes
isTruncated bool
}
// batchGetItemAPIClient is a client that implements the BatchGetItem operation.
type batchGetItemAPIClient interface {
BatchGetItem(context.Context, *dynamodb.BatchGetItemInput, ...func(*dynamodb.Options)) (*dynamodb.BatchGetItemOutput, error)
}
// newBatchGetItemPaginator returns a new batchGetItemPaginator
func newBatchGetItemPaginator(client batchGetItemAPIClient, params *dynamodb.BatchGetItemInput, optFns ...func(*batchGetItemPaginatorOptions)) *batchGetItemPaginator {
if params == nil {
params = &dynamodb.BatchGetItemInput{}
}
options := batchGetItemPaginatorOptions{}
for _, fn := range optFns {
fn(&options)
}
return &batchGetItemPaginator{
options: options,
client: client,
params: params,
firstPage: true,
requestItems: params.RequestItems,
}
}
// hasMorePages returns a boolean indicating whether more pages are available
func (p *batchGetItemPaginator) hasMorePages() bool {
return p.firstPage || p.isTruncated
}
// nextPage retrieves the next BatchGetItem page.
func (p *batchGetItemPaginator) nextPage(ctx context.Context, optFns ...func(*dynamodb.Options)) (*dynamodb.BatchGetItemOutput, error) {
if !p.hasMorePages() {
return nil, fmt.Errorf("no more pages available")
}
params := *p.params
params.RequestItems = p.requestItems
result, err := p.client.BatchGetItem(ctx, ¶ms, optFns...)
if err != nil {
return nil, err
}
p.firstPage = false
prevToken := p.requestItems
p.isTruncated = len(result.UnprocessedKeys) != 0
p.requestItems = nil
if p.isTruncated {
p.requestItems = result.UnprocessedKeys
}
if p.options.stopOnDuplicateToken &&
prevToken != nil &&
p.requestItems != nil &&
deepEqual(prevToken, p.requestItems) {
p.isTruncated = false
}
return result, nil
}
// deepEqual returns if the two values are deeply equal like reflect.DeepEqual.
// In addition to this, this method will also dereference the input values if
// possible so the DeepEqual performed will not fail if one parameter is a
// pointer and the other is not.
//
// deepEqual will not perform indirection of nested values of the input parameters.
func deepEqual(a, b interface{}) bool {
ra := reflect.Indirect(reflect.ValueOf(a))
rb := reflect.Indirect(reflect.ValueOf(b))
if raValid, rbValid := ra.IsValid(), rb.IsValid(); !raValid && !rbValid {
// If the elements are both nil, and of the same type the are equal
// If they are of different types they are not equal
return reflect.TypeOf(a) == reflect.TypeOf(b)
} else if raValid != rbValid {
// Both values must be valid to be equal
return false
}
// Special casing for strings as typed enumerations are string aliases
// but are not deep equal.
if ra.Kind() == reflect.String && rb.Kind() == reflect.String {
return ra.String() == rb.String()
}
return reflect.DeepEqual(ra.Interface(), rb.Interface())
}