-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpage_info.go
38 lines (33 loc) · 1.32 KB
/
page_info.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
package paging
import (
"math"
)
// NewOffsetBasedPageInfo returns a new PageInfo object with data filled in, based on offset pagination
func NewOffsetBasedPageInfo(
pageSize *int,
totalCount int64,
currentOffset int,
) PageInfo {
count := int(totalCount)
endOffset := count - int(math.Mod(float64(count), float64(*pageSize)))
if endOffset == count {
endOffset = count - *pageSize
}
return PageInfo{
TotalCount: func() (*int, error) { return &count, nil },
StartCursor: func() (*string, error) { return EncodeOffsetCursor(0), nil },
EndCursor: func() (*string, error) { return EncodeOffsetCursor(endOffset), nil },
HasNextPage: func() (bool, error) { return (currentOffset+*pageSize < count), nil },
HasPreviousPage: func() (bool, error) { return (currentOffset-*pageSize > 0), nil },
}
}
// NewEmptyPageInfo returns a empty instance of PageInfo. Useful for when working on a new page to be able to fullfil PageInfo requirements
func NewEmptyPageInfo() *PageInfo {
return &PageInfo{
TotalCount: func() (*int, error) { return nil, nil },
StartCursor: func() (*string, error) { return nil, nil },
EndCursor: func() (*string, error) { return nil, nil },
HasNextPage: func() (bool, error) { return false, nil },
HasPreviousPage: func() (bool, error) { return false, nil },
}
}