forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
paging.go
209 lines (174 loc) · 4.11 KB
/
paging.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package influxdb
import (
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/kit/platform/errors"
)
const (
DefaultPageSize = 20
MaxPageSize = 100
)
// PagingFilter represents a filter containing url query params.
type PagingFilter interface {
// QueryParams returns a map containing url query params.
QueryParams() map[string][]string
}
// PagingLinks represents paging links.
type PagingLinks struct {
Prev string `json:"prev,omitempty"`
Self string `json:"self"`
Next string `json:"next,omitempty"`
}
// FindOptions represents options passed to all find methods with multiple results.
type FindOptions struct {
Limit int
Offset int
After *platform.ID
SortBy string
Descending bool
}
// GetLimit returns the resolved limit between then limit boundaries.
// Given a limit <= 0 it returns the default limit.
func (f *FindOptions) GetLimit() int {
if f == nil || f.Limit <= 0 {
return DefaultPageSize
}
if f.Limit > MaxPageSize {
return MaxPageSize
}
return f.Limit
}
// DecodeFindOptions returns a FindOptions decoded from http request.
func DecodeFindOptions(r *http.Request) (*FindOptions, error) {
opts := &FindOptions{}
qp := r.URL.Query()
if offset := qp.Get("offset"); offset != "" {
o, err := strconv.Atoi(offset)
if err != nil {
return nil, &errors.Error{
Code: errors.EInvalid,
Msg: "offset is invalid",
}
}
opts.Offset = o
}
if after := qp.Get("after"); after != "" {
id, err := platform.IDFromString(after)
if err != nil {
return nil, &errors.Error{
Code: errors.EInvalid,
Err: fmt.Errorf("decoding after: %w", err),
}
}
opts.After = id
}
if limit := qp.Get("limit"); limit != "" {
l, err := strconv.Atoi(limit)
if err != nil {
return nil, &errors.Error{
Code: errors.EInvalid,
Msg: "limit is invalid",
}
}
if l < 1 || l > MaxPageSize {
return nil, &errors.Error{
Code: errors.EInvalid,
Msg: fmt.Sprintf("limit must be between 1 and %d", MaxPageSize),
}
}
opts.Limit = l
} else {
opts.Limit = DefaultPageSize
}
if sortBy := qp.Get("sortBy"); sortBy != "" {
opts.SortBy = sortBy
}
if descending := qp.Get("descending"); descending != "" {
desc, err := strconv.ParseBool(descending)
if err != nil {
return nil, &errors.Error{
Code: errors.EInvalid,
Msg: "descending is invalid",
}
}
opts.Descending = desc
}
return opts, nil
}
func FindOptionParams(opts ...FindOptions) [][2]string {
var out [][2]string
for _, o := range opts {
for k, vals := range o.QueryParams() {
for _, v := range vals {
out = append(out, [2]string{k, v})
}
}
}
return out
}
// QueryParams returns a map containing url query params.
func (f FindOptions) QueryParams() map[string][]string {
qp := map[string][]string{
"descending": {strconv.FormatBool(f.Descending)},
"offset": {strconv.Itoa(f.Offset)},
}
if f.After != nil {
qp["after"] = []string{f.After.String()}
}
if f.Limit > 0 {
qp["limit"] = []string{strconv.Itoa(f.Limit)}
}
if f.SortBy != "" {
qp["sortBy"] = []string{f.SortBy}
}
return qp
}
// NewPagingLinks returns a PagingLinks.
// num is the number of returned results.
func NewPagingLinks(basePath string, opts FindOptions, f PagingFilter, num int) *PagingLinks {
u := url.URL{
Path: basePath,
}
values := url.Values{}
for k, vs := range f.QueryParams() {
for _, v := range vs {
if v != "" {
values.Add(k, v)
}
}
}
var self, next, prev string
for k, vs := range opts.QueryParams() {
for _, v := range vs {
if v != "" {
values.Add(k, v)
}
}
}
u.RawQuery = values.Encode()
self = u.String()
if num >= opts.Limit {
nextOffset := opts.Offset + opts.Limit
values.Set("offset", strconv.Itoa(nextOffset))
u.RawQuery = values.Encode()
next = u.String()
}
if opts.Offset > 0 {
prevOffset := opts.Offset - opts.Limit
if prevOffset < 0 {
prevOffset = 0
}
values.Set("offset", strconv.Itoa(prevOffset))
u.RawQuery = values.Encode()
prev = u.String()
}
links := &PagingLinks{
Prev: prev,
Self: self,
Next: next,
}
return links
}