-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpagination.go
76 lines (71 loc) · 1.78 KB
/
pagination.go
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
package activity
import (
"context"
"errors"
)
// Pagination specifies how to paginate through resources
type Pagination struct {
// Total number of resources to query
Total int
// Start querying at this page
Start int
// Count of the number of resources to query per page
Count int
}
// Paginator paginates through results
type Paginator interface {
// PageSize returns the number of resources to query per request
PageSize() int
// Count of the aggregate total of resources queried
Count() int
// Do executes the query using the pagination specification returning
// the number of resources returned in this request or an error
Do(ctx context.Context, spec Pagination) (int, error)
}
func Paginate(ctx context.Context, paginator Paginator, spec Pagination) error {
var (
start = spec.Start
count = spec.Count
total = spec.Total
)
if total < 0 {
return errors.New("total less than zero")
}
if start <= 0 {
start = 1
}
if count <= 0 {
count = paginator.PageSize()
}
if total > 0 {
if total <= count {
count = total
}
// if requesting only one page of data then optimize
if start <= 1 && total < paginator.PageSize() {
count = total
}
}
return do(ctx, paginator, Pagination{Total: total, Start: start, Count: count})
}
func do(ctx context.Context, paginator Paginator, spec Pagination) error {
for {
n, err := paginator.Do(ctx, spec)
if err != nil {
return err
}
if n == 0 {
// fewer than requested results is a possible scenario so break only if
// 0 results were returned or we have enough to fulfill the request
break
}
// @warning(bzimmer)
// the `spec.Count` value must be consistent throughout the entire pagination
all := paginator.Count()
if spec.Total > 0 && all >= spec.Total {
break
}
spec.Start++
}
return nil
}