File: handwritten_paginators.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.24.1-2~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 554,032 kB
  • sloc: java: 15,941; makefile: 419; sh: 175
file content (88 lines) | stat: -rw-r--r-- 2,462 bytes parent folder | download | duplicates (4)
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
package dynamodb

import (
	"context"
	"fmt"
	"github.com/aws/aws-sdk-go-v2/internal/awsutil"
	"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       *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, *BatchGetItemInput, ...func(*Options)) (*BatchGetItemOutput, error)
}

// NewBatchGetItemPaginator returns a new BatchGetItemPaginator
func NewBatchGetItemPaginator(client BatchGetItemAPIClient, params *BatchGetItemInput, optFns ...func(*BatchGetItemPaginatorOptions)) *BatchGetItemPaginator {
	if params == nil {
		params = &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(*Options)) (*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, &params, 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 &&
		awsutil.DeepEqual(prevToken, p.requestItems) {
		p.isTruncated = false
	}

	return result, nil
}