-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaginator_details.go
84 lines (73 loc) · 1.58 KB
/
paginator_details.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
77
78
79
80
81
82
83
84
package pagefilter
import (
"errors"
"fmt"
"net/url"
"strconv"
"github.com/jacobbrewer1/pagefilter/common"
)
var (
ErrInvalidPaginatorDetails = errors.New("invalid paginator details")
)
// PaginatorDetails contains pagination details
type PaginatorDetails struct {
Limit int
LastVal string
LastID string
SortBy string
SortDir string
sortComparator string
sortOperator string
}
func getLimit(q url.Values) (limit int, err error) {
limit = defaultPageLimit
if limitStr := q.Get(QueryLimit); limitStr != "" {
if limit, err = strconv.Atoi(limitStr); err != nil {
return -1, fmt.Errorf("invalid limit: %w", err)
}
}
if limit > maxLimit {
limit = maxLimit
}
if limit == 0 {
limit = defaultPageLimit
}
return limit, nil
}
// GetPaginatorDetails loads paginator details from a request. Requests have each pagination detail determined
// separately by codegen.
func GetPaginatorDetails(
limit *common.LimitParam,
lastVal *common.LastValue,
lastID *common.LastId,
sortBy *common.SortBy,
sortDir *common.SortDirection,
) *PaginatorDetails {
d := new(PaginatorDetails)
if lastID != nil {
d.LastID = *lastID
}
if lastVal != nil {
d.LastVal = *lastVal
}
if limit != nil {
d.Limit, _ = strconv.Atoi(*limit)
}
if sortBy != nil {
d.SortBy = *sortBy
}
if sortDir != nil {
d.SortDir = *sortDir
}
if d.Limit <= 0 {
d.Limit = defaultPageLimit
}
if d.Limit > maxLimit {
d.Limit = maxLimit
}
return d
}
// RemoveLimit removes the limit from the paginator details.
func (p *PaginatorDetails) RemoveLimit() {
p.Limit = -1
}