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 (98 lines) | stat: -rw-r--r-- 2,651 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
89
90
91
92
93
94
95
96
97
98
package amplifybackend

import (
	"context"
	"fmt"

	"github.com/aws/aws-sdk-go-v2/aws"
)

// ListBackendJobsPaginatorOptions is the paginator options for ListBackendJobs
type ListBackendJobsPaginatorOptions struct {
	// (Optional) The maximum number of shards to return in a single call
	Limit int32

	// 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
}

// ListBackendJobsPaginator is a paginator for ListBackendJobs
type ListBackendJobsPaginator struct {
	options     ListBackendJobsPaginatorOptions
	client      ListBackendJobsAPIClient
	params      *ListBackendJobsInput
	firstPage   bool
	nextToken   *string
	isTruncated bool
}

// ListBackendJobsAPIClient is a client that implements the ListBackendJobs operation.
type ListBackendJobsAPIClient interface {
	ListBackendJobs(context.Context, *ListBackendJobsInput, ...func(*Options)) (*ListBackendJobsOutput, error)
}

// NewListBackendJobsPaginator returns a new ListBackendJobsPaginator
func NewListBackendJobsPaginator(client ListBackendJobsAPIClient, params *ListBackendJobsInput, optFns ...func(options *ListBackendJobsPaginatorOptions)) *ListBackendJobsPaginator {
	if params == nil {
		params = &ListBackendJobsInput{}
	}

	options := ListBackendJobsPaginatorOptions{}
	options.Limit = aws.ToInt32(params.MaxResults)

	for _, fn := range optFns {
		fn(&options)
	}

	return &ListBackendJobsPaginator{
		options:   options,
		client:    client,
		params:    params,
		firstPage: true,
		nextToken: params.NextToken,
	}
}

// HasMorePages returns a boolean indicating whether more pages are available
func (p *ListBackendJobsPaginator) HasMorePages() bool {
	return p.firstPage || p.isTruncated
}

// NextPage retrieves the next ListBackendJobs page.
func (p *ListBackendJobsPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*ListBackendJobsOutput, error) {
	if !p.HasMorePages() {
		return nil, fmt.Errorf("no more pages available")
	}

	params := *p.params
	params.NextToken = p.nextToken

	var limit int32
	if p.options.Limit > 0 {
		limit = p.options.Limit
	}
	params.MaxResults = aws.Int32(limit)

	result, err := p.client.ListBackendJobs(ctx, &params, optFns...)
	if err != nil {
		return nil, err
	}
	p.firstPage = false

	prevToken := p.nextToken
	p.isTruncated = result.NextToken != nil
	p.nextToken = nil
	if result.NextToken != nil {
		p.nextToken = result.NextToken
	}

	if p.options.StopOnDuplicateToken &&
		prevToken != nil &&
		p.nextToken != nil &&
		*prevToken == *p.nextToken {
		p.isTruncated = false
	}

	return result, nil
}