-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #4658: Generalize Querier REST Pagination
- Loading branch information
1 parent
b3a8195
commit aba1f64
Showing
6 changed files
with
102 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#4601 Implement generic pangination helper function to be used in | ||
REST handlers and queriers. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package client | ||
|
||
// Paginate returns the correct starting and ending index for a paginated query, | ||
// given that client provides a desired page and limit of objects and the handler | ||
// provides the total number of objects. If the start page is invalid, non-positive | ||
// values are returned signaling the request is invalid. | ||
// | ||
// NOTE: The start page is assumed to be 1-indexed. | ||
func Paginate(numObjs, page, limit, defLimit int) (start, end int) { | ||
if page == 0 { | ||
// invalid start page | ||
return -1, -1 | ||
} else if limit == 0 { | ||
limit = defLimit | ||
} | ||
|
||
start = (page - 1) * limit | ||
end = limit + start | ||
|
||
if end >= numObjs { | ||
end = numObjs | ||
} | ||
|
||
if start >= numObjs { | ||
// page is out of bounds | ||
return -1, -1 | ||
} | ||
|
||
return start, end | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package client_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
) | ||
|
||
func TestPaginate(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
numObjs, page, limit, defLimit int | ||
expectedStart, expectedEnd int | ||
}{ | ||
{ | ||
"all objects in a single page", | ||
100, 1, 100, 100, | ||
0, 100, | ||
}, | ||
{ | ||
"page one of three", | ||
75, 1, 25, 100, | ||
0, 25, | ||
}, | ||
{ | ||
"page two of three", | ||
75, 2, 25, 100, | ||
25, 50, | ||
}, | ||
{ | ||
"page three of three", | ||
75, 3, 25, 100, | ||
50, 75, | ||
}, | ||
{ | ||
"end is greater than total number of objects", | ||
75, 2, 50, 100, | ||
50, 75, | ||
}, | ||
{ | ||
"invalid start page", | ||
75, 4, 25, 100, | ||
-1, -1, | ||
}, | ||
{ | ||
"invalid zero start page", | ||
75, 0, 25, 100, | ||
-1, -1, | ||
}, | ||
} | ||
|
||
for i, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
start, end := client.Paginate(tc.numObjs, tc.page, tc.limit, tc.defLimit) | ||
require.Equal(t, tc.expectedStart, start, "invalid result; test case #%d", i) | ||
require.Equal(t, tc.expectedEnd, end, "invalid result; test case #%d", i) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters