Skip to content

Commit

Permalink
Place pagination Bag in a good state when calling NextToken
Browse files Browse the repository at this point in the history
  • Loading branch information
shackra committed Oct 7, 2024
1 parent d5da368 commit 28e18a1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/pagination/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ func (pb *Bag) Next(pageToken string) error {

// Next pops the current token, and pushes a copy of it with an updated page token.
func (pb *Bag) NextToken(pageToken string) (string, error) {
// assume that `pb` was passed an empty token
if pb.currentState == nil {
pb.currentState = &PageState{
Token: pageToken,
}
return pb.Marshal()
}

st := pb.pop()
if st == nil {
return "", fmt.Errorf("no active page state")
Expand Down
14 changes: 14 additions & 0 deletions pkg/pagination/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,17 @@ func TestPaginationBagNextPage(t *testing.T) {
require.Len(t, bag.states, 0)
comparePageState(t, ps2, *bag.Current())
}

func TestPaginationBagNextPageWithoutActivePageState(t *testing.T) {
bag := &Bag{}

// try to unmarshal an empty string
err := bag.Unmarshal("")
// passing an empty string SHOULD NOT return error
require.NoError(t, err)

updatedToken, err := bag.NextToken("<valid token from third-party service>")
require.NoError(t, err)
require.NotEmpty(t, updatedToken)
require.NotNil(t, bag.currentState)
}

0 comments on commit 28e18a1

Please sign in to comment.