Skip to content

Commit c9b78ea

Browse files
committed
Add support for locally determining whether a PagedResult represents the last page of results.
1 parent 42b5a31 commit c9b78ea

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

compute/paging.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package compute
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
)
46

57
// PagedResult represents the common fields for all paged results from the compute API.
68
type PagedResult struct {
@@ -30,6 +32,17 @@ func (page *PagedResult) NextPage() *Paging {
3032
}
3133
}
3234

35+
// IsLastPage determines whether the page represents the last page of results.
36+
func (page *PagedResult) IsLastPage() bool {
37+
if page.IsEmpty() {
38+
return true
39+
}
40+
41+
itemCountUpToCurrentPage := ((page.PageNumber - 1) * page.PageSize) + page.PageCount
42+
43+
return itemCountUpToCurrentPage >= page.TotalCount // i.e. Are there any more records?
44+
}
45+
3346
// Paging contains the paging configuration for a compute API operation.
3447
type Paging struct {
3548
PageNumber int

compute/paging_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package compute
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// Verify that a PagedResult correctly determines that a page is not the last page.
8+
func TestPagedResult_IsLastPage_PageSize_2_FirstPage_2of6Records(test *testing.T) {
9+
expect := expect(test)
10+
11+
result := &PagedResult{
12+
PageNumber: 1,
13+
PageSize: 2,
14+
PageCount: 2,
15+
TotalCount: 6,
16+
}
17+
18+
expect.IsFalse("PagedResult.IsLastPage",
19+
result.IsLastPage(),
20+
)
21+
}
22+
23+
// Verify that a PagedResult correctly determines that a page is not the last page.
24+
func TestPagedResult_IsLastPage_PageSize_2_SecondLastPage_3of6Records(test *testing.T) {
25+
expect := expect(test)
26+
27+
result := &PagedResult{
28+
PageNumber: 2,
29+
PageSize: 2,
30+
PageCount: 1,
31+
TotalCount: 6,
32+
}
33+
34+
expect.IsFalse("PagedResult.IsLastPage",
35+
result.IsLastPage(),
36+
)
37+
}
38+
39+
// Verify that a PagedResult correctly determines that a page is the last page.
40+
func TestPagedResult_IsLastPage_PageSize_2_LastPage_6of6Records(test *testing.T) {
41+
expect := expect(test)
42+
43+
result := &PagedResult{
44+
PageNumber: 3,
45+
PageSize: 2,
46+
PageCount: 2,
47+
TotalCount: 6,
48+
}
49+
50+
expect.IsTrue("PagedResult.IsLastPage",
51+
result.IsLastPage(),
52+
)
53+
}

0 commit comments

Comments
 (0)