-
Notifications
You must be signed in to change notification settings - Fork 12
/
query_params.go
65 lines (57 loc) · 1.1 KB
/
query_params.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
package woocommerce
import (
"github.com/google/go-querystring/query"
"net/url"
"strings"
)
const (
SortAsc = "asc"
SortDesc = "desc"
)
const (
ViewContext = "view"
EditContext = "edit"
)
type queryParams struct {
Page int `url:"page,omitempty"`
PerPage int `url:"per_page,omitempty"`
Offset int `url:"offset,omitempty"`
Order string `url:"order,omitempty"`
OrderBy string `url:"order_by,omitempty"`
Context string `url:"context,omitempty"`
}
func (q *queryParams) TidyVars() *queryParams {
if q.Page <= 0 {
q.Page = 1
}
if q.PerPage <= 0 {
q.PerPage = 10
} else if q.PerPage > 100 {
q.PerPage = 100
}
if q.Offset < 0 {
q.Offset = 0
}
if q.Order == "" {
q.Order = SortAsc
} else {
q.Order = strings.ToLower(q.Order)
if q.Order != SortDesc {
q.OrderBy = SortAsc
}
}
if q.Context == "" {
q.Context = ViewContext
} else {
q.Context = strings.ToLower(q.Context)
if q.Context != EditContext {
q.Context = ViewContext
}
}
return q
}
// change to url.values
func toValues(i interface{}) (values url.Values) {
values, _ = query.Values(i)
return
}