-
Notifications
You must be signed in to change notification settings - Fork 1
/
params_parser.go
148 lines (125 loc) · 3.65 KB
/
params_parser.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
package common
import (
"encoding/json"
"net/url"
"strconv"
"strings"
)
const (
defaultLimitParam = "limit"
defaultOffsetParam = "offset"
defaultOrderByParam = "order_by"
defaultPrevPageValuesParam = "prev_page_values"
)
type QueryParams = map[string][]string
type OrderByParseFunc = func(params QueryParams, orderByParam string) (orderByParams OrderByParams, err error)
type PrevPageValuesParseFunc = func(params QueryParams, prevPageValueParam string) (prevPageValues PrevPageValues, err error)
type PaginatorParamsParser interface {
ParseQuery(query string) (params PaginatorParams, err error)
}
type defaultPaginatorParamsParser struct {
LimitParam string
OffsetParam string
OrderByParam string
PrevPageValuesParam string
OrderByParse OrderByParseFunc
PrevPageValuesParse PrevPageValuesParseFunc
}
func NewPaginatorParamsParser(limitParam, offsetParam, orderByParam, prevPageValuesParam string,
orderByParse OrderByParseFunc, prevPageValuesParse PrevPageValuesParseFunc) *defaultPaginatorParamsParser {
orderByParseFunc := DefaultOrderByParse
if orderByParse != nil {
orderByParseFunc = orderByParse
}
prevPageValuesParseFunc := DefaultPrevPageValuesParse
if prevPageValuesParse != nil {
prevPageValuesParseFunc = prevPageValuesParse
}
return &defaultPaginatorParamsParser{
LimitParam: limitParam,
OffsetParam: offsetParam,
OrderByParam: orderByParam,
PrevPageValuesParam: prevPageValuesParam,
OrderByParse: orderByParseFunc,
PrevPageValuesParse: prevPageValuesParseFunc,
}
}
func NewDefaultPaginatorParamsParser() *defaultPaginatorParamsParser {
return &defaultPaginatorParamsParser{
LimitParam: defaultLimitParam,
OffsetParam: defaultOffsetParam,
OrderByParam: defaultOrderByParam,
PrevPageValuesParam: defaultPrevPageValuesParam,
OrderByParse: DefaultOrderByParse,
PrevPageValuesParse: DefaultPrevPageValuesParse,
}
}
func (p *defaultPaginatorParamsParser) ParseQuery(query string) (params PaginatorParams, err error) {
paramsQuery := query
if strings.Contains(paramsQuery, "?") {
paramsQuery = strings.SplitN(paramsQuery, "?", 2)[1]
}
values, err := url.ParseQuery(paramsQuery)
if err != nil {
return
}
// parsing limit
limit := -1
if len(values[p.LimitParam]) > 0 {
limit, err = strconv.Atoi(values[p.LimitParam][0])
if err != nil {
return
}
}
// parsing offset
offset := -1
if len(values[p.OffsetParam]) > 0 {
offset, err = strconv.Atoi(values[p.OffsetParam][0])
if err != nil {
return
}
}
// parsing order by params
orderBy, err := p.OrderByParse(values, p.OrderByParam)
if err != nil {
return
}
prevPage, err := p.PrevPageValuesParse(values, p.PrevPageValuesParam)
return PaginatorParams{
Limit: limit,
Offset: offset,
OrderBy: orderBy,
PrevPage: prevPage,
}, nil
}
func DefaultOrderByParse(params QueryParams, orderByParam string) (orderByParams OrderByParams, err error) {
orderBy := []string{}
if len(params[orderByParam]) > 0 {
orderBy = strings.Split(params[orderByParam][0], ",")
} else {
return
}
for _, p := range orderBy {
x := strings.Split(p, "-")
field := x[0]
order := "asc"
if len(x) > 1 {
order = x[1]
}
orderByParams = append(orderByParams, &OrderByParam{
Field: field,
Order: order,
})
}
return
}
func DefaultPrevPageValuesParse(params QueryParams, prevPageValueParam string) (prevPageValues PrevPageValues, err error) {
prevPageValues = PrevPageValues{}
if len(params[prevPageValueParam]) > 0 {
err = json.Unmarshal([]byte(params[prevPageValueParam][0]), &prevPageValues)
if err != nil {
return
}
}
return
}