/
page.go
36 lines (32 loc) · 1.08 KB
/
page.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
package types
const (
DefaultPageRequestLimit = uint32(10)
)
// IsEmptyOrDefault returns true if the page request is nil, or if it only contains default values.
func IsEmptyOrDefault(pr *PageRequest) bool {
if pr == nil || (len(pr.Key) == 0 && pr.Limit == 0) {
return true
}
return false
}
// DefaultPageRequest returns a default page request with a nil key, and the limit set to DefaultPageRequestLimit.
func DefaultPageRequest() *PageRequest {
return &PageRequest{
Key: nil,
Limit: DefaultPageRequestLimit,
}
}
// ExtractPageRequest first checks if given a valid page request. If so, it returns the key and limit.
// Else, it returns the default key (nil) and limit.
func ExtractPageRequest(pr *PageRequest) ([]byte, uint32) {
if !IsEmptyOrDefault(pr) {
// handle the case where we were given a key, but no limit. in this case, we need to set the limit
// to default, otherwise, it will be 0 and the query will not return anything.
if pr.Limit == 0 {
pr.Limit = DefaultPageRequestLimit
}
return pr.Key, pr.Limit
}
df := DefaultPageRequest()
return df.Key, df.Limit
}