Skip to content

Commit 5e8e36f

Browse files
authored
private/mode/api: refactoring paginators (#119)
* Refactoring paginator * Fixing docs and tests * regenerating services * Introducing Copy method on API Requests * regenerating services * Updating tests to use new paginator and add mocking of paginators
1 parent 39ee151 commit 5e8e36f

File tree

226 files changed

+24706
-23925
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

226 files changed

+24706
-23925
lines changed

aws/request_pagination.go

Lines changed: 15 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
package aws
22

33
import (
4-
"reflect"
54
"sync/atomic"
65

76
"github.com/aws/aws-sdk-go-v2/internal/awsutil"
87
)
98

10-
// A Pagination provides paginating of SDK API operations which are paginatable.
9+
// A Pager provides paginating of SDK API operations which are paginatable.
1110
// Generally you should not use this type directly, but use the "Pages" API
1211
// operations method to automatically perform pagination for you. Such as,
1312
// "S3.ListObjectsPages", and "S3.ListObjectsPagesWithContext" methods.
1413
//
15-
// Pagination differs from a Paginator type in that pagination is the type that
14+
// Pagier differs from a Paginator type in that pagination is the type that
1615
// does the pagination between API operations, and Paginator defines the
1716
// configuration that will be used per page request.
1817
//
19-
// cont := true
20-
// for p.Next() && cont {
21-
// data := p.Page().(*s3.ListObjectsOutput)
18+
// for p.Next() {
19+
// data := p.CurrentPage().(*s3.ListObjectsOutput)
2220
// // process the page's data
2321
// }
2422
// return p.Err()
2523
//
2624
// See service client API operation Pages methods for examples how the SDK will
27-
// use the Pagination type.
28-
type Pagination struct {
25+
// use the Pager type.
26+
type Pager struct {
2927
// Function to return a Request value for each pagination request.
3028
// Any configuration or handlers that need to be applied to the request
3129
// prior to getting the next page should be done here before the request
@@ -42,24 +40,24 @@ type Pagination struct {
4240
curPage interface{}
4341
}
4442

45-
// HasNextPage will return true if Pagination is able to determine that the API
43+
// hasNextPage will return true if Pager is able to determine that the API
4644
// operation has additional pages. False will be returned if there are no more
4745
// pages remaining.
4846
//
4947
// Will always return true if Next has not been called yet.
50-
func (p *Pagination) HasNextPage() bool {
48+
func (p *Pager) hasNextPage() bool {
5149
return !(p.started && len(p.nextTokens) == 0)
5250
}
5351

54-
// Err returns the error Pagination encountered when retrieving the next page.
55-
func (p *Pagination) Err() error {
52+
// Err returns the error Pager encountered when retrieving the next page.
53+
func (p *Pager) Err() error {
5654
return p.err
5755
}
5856

59-
// Page returns the current page. Page should only be called after a successful
57+
// CurrentPage returns the current page. Page should only be called after a successful
6058
// call to Next. It is undefined what Page will return if Page is called after
6159
// Next returns false.
62-
func (p *Pagination) Page() interface{} {
60+
func (p *Pager) CurrentPage() interface{} {
6361
return p.curPage
6462
}
6563

@@ -71,8 +69,8 @@ func (p *Pagination) Page() interface{} {
7169
// to be cast to the API operation's output type.
7270
//
7371
// Use the Err method to determine if an error occurred if Page returns false.
74-
func (p *Pagination) Next() bool {
75-
if !p.HasNextPage() {
72+
func (p *Pager) Next() bool {
73+
if !p.hasNextPage() {
7674
return false
7775
}
7876

@@ -105,7 +103,7 @@ func (p *Pagination) Next() bool {
105103
// should be paginated. This type is used by the API service models to define
106104
// the generated pagination config for service APIs.
107105
//
108-
// The Pagination type is what provides iterating between pages of an API. It
106+
// The Pager type is what provides iterating between pages of an API. It
109107
// is only used to store the token metadata the SDK should use for performing
110108
// pagination.
111109
type Paginator struct {
@@ -187,65 +185,3 @@ var (
187185
logDeprecatedNextPage int32
188186
logDeprecatedEachPage int32
189187
)
190-
191-
// HasNextPage returns true if this request has more pages of data available.
192-
//
193-
// Deprecated Use Pagination type for configurable pagination of API operations
194-
func (r *Request) HasNextPage() bool {
195-
logDeprecatedf(r.Config.Logger, &logDeprecatedHasNextPage,
196-
"Request.HasNextPage deprecated. Use Pagination type for configurable pagination of API operations")
197-
198-
return len(r.nextPageTokens()) > 0
199-
}
200-
201-
// NextPage returns a new Request that can be executed to return the next
202-
// page of result data. Call .Send() on this request to execute it.
203-
//
204-
// Deprecated Use Pagination type for configurable pagination of API operations
205-
func (r *Request) NextPage() *Request {
206-
logDeprecatedf(r.Config.Logger, &logDeprecatedNextPage,
207-
"Request.NextPage deprecated. Use Pagination type for configurable pagination of API operations")
208-
209-
tokens := r.nextPageTokens()
210-
if len(tokens) == 0 {
211-
return nil
212-
}
213-
214-
data := reflect.New(reflect.TypeOf(r.Data).Elem()).Interface()
215-
nr := New(r.Config, r.Metadata, r.Handlers, r.Retryer, r.Operation, awsutil.CopyOf(r.Params), data)
216-
for i, intok := range nr.Operation.InputTokens {
217-
awsutil.SetValueAtPath(nr.Params, intok, tokens[i])
218-
}
219-
return nr
220-
}
221-
222-
// EachPage iterates over each page of a paginated request object. The fn
223-
// parameter should be a function with the following sample signature:
224-
//
225-
// func(page *T, lastPage bool) bool {
226-
// return true // return false to stop iterating
227-
// }
228-
//
229-
// Where "T" is the structure type matching the output structure of the given
230-
// operation. For example, a request object generated by
231-
// DynamoDB.ListTablesRequest() would expect to see dynamodb.ListTablesOutput
232-
// as the structure "T". The lastPage value represents whether the page is
233-
// the last page of data or not. The return value of this function should
234-
// return true to keep iterating or false to stop.
235-
//
236-
// Deprecated Use Pagination type for configurable pagination of API operations
237-
func (r *Request) EachPage(fn func(data interface{}, isLastPage bool) (shouldContinue bool)) error {
238-
logDeprecatedf(r.Config.Logger, &logDeprecatedEachPage,
239-
"Request.EachPage deprecated. Use Pagination type for configurable pagination of API operations")
240-
241-
for page := r; page != nil; page = page.NextPage() {
242-
if err := page.Send(); err != nil {
243-
return err
244-
}
245-
if getNextPage := fn(page.Data, !page.HasNextPage()); !getNextPage {
246-
return page.Error
247-
}
248-
}
249-
250-
return nil
251-
}

0 commit comments

Comments
 (0)