-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
213 lines (168 loc) · 4.1 KB
/
parse.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
210
211
212
213
package kendo
import (
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/globalsign/mgo/bson"
)
const (
TimeLayout = "2006-01-02T15-04-05"
TokensPerFilter = 4
)
// NewDataStateFromRequest creates a *DataState from the query parameters of the request
func NewDataStateFromRequest(request *http.Request) (dataState *DataState, err error) {
values, err := url.ParseQuery(request.URL.RawQuery)
if err != nil {
return
}
dataState = new(DataState)
dataState.values = values
return
}
// WithReplacements adds field replacements to the DataState.
// For example map[string]string{ "_id": "id" }
func (d *DataState) WithReplacements(replacements map[string]string) {
d.replacements = replacements
}
// WithLookups adds LookupDescriptors to the DataState
func (d *DataState) WithLookups(lookups []LookupDescriptor) {
d.Lookup = lookups
}
// WithPreprocessing adds pipeline steps to the DataState that are executed before any other steps
func (d *DataState) WithPreprocessing(preprocessing []bson.M) {
d.preprocessing = preprocessing
}
func (d *DataState) parse() (err error) {
if err = d.parsePage(); err != nil {
return
}
if err = d.parsePageSize(); err != nil {
return
}
if err = d.parseFilterDescriptors(); err != nil {
return
}
d.parseSortDescriptors()
d.parseGroupDescriptors()
d.parseAggregateDescriptors()
return
}
func (d *DataState) parseAggregateDescriptors() {
aggregrate := d.values.Get("aggregate")
if aggregrate == "" {
return
}
tokens := tokenize(aggregrate, "~")
aggregates := make([]AggregateDescriptor, len(tokens))
for i, t := range tokens {
values := tokenize(t, "-")
aggregates[i].Field = d.replaceField(values[0])
aggregates[i].Aggregate = values[1]
}
d.Aggregates = aggregates
}
func (d *DataState) parseFilterDescriptors() (err error) {
filter := d.values.Get("filter")
if filter == "" {
return
}
tokens := tokenize(filter, "~")
nbTokens := len(tokens)
nbFilters := (nbTokens / TokensPerFilter) + 1
filters := make([]FilterDescriptor, nbFilters)
for i := 0; i < nbTokens; i += TokensPerFilter {
idx := i / TokensPerFilter
var value interface{}
value, err = toValue(tokens[i+2])
if err != nil {
return
}
filters[idx] = FilterDescriptor{
Field: d.replaceField(tokens[i]),
Operator: tokens[i+1],
Value: value,
}
}
d.Filter.Filters = filters
if nbFilters > 1 {
d.Filter.Logic = "and" //TODO support "or" logic
}
return
}
func (d *DataState) parseGroupDescriptors() {
group := d.values.Get("group")
if group == "" {
return
}
tokens := tokenize(group, "~")
groups := make([]GroupDescriptor, len(tokens))
for i, t := range tokens {
group := tokenize(t, "-")
field := d.replaceField(group[0])
direction := group[1]
groups[i] = GroupDescriptor{
Field: field,
Dir: direction,
}
}
d.Group = groups
}
func (d *DataState) parsePage() (err error) {
page := d.values.Get("page")
if page == "" {
return
}
d.Page, err = strconv.Atoi(page)
return
}
func (d *DataState) parsePageSize() (err error) {
size := d.values.Get("pageSize")
if size == "" {
return
}
d.PageSize, err = strconv.Atoi(size)
return
}
func (d *DataState) parseSortDescriptors() {
sort := d.values.Get("sort")
if sort == "" {
return
}
s := tokenize(sort, "-")
if len(s) >= 2 { //id-asc
field := d.replaceField(s[0])
direction := s[1]
d.Sort = append(d.Sort, SortDescriptor{
Field: field,
Dir: direction,
})
}
}
func (d *DataState) replaceField(field string) (replaced string) {
replaced = field
if replacement, ok := d.replacements[field]; ok {
replaced = replacement
}
return
}
func tokenize(s string, sep string) (tokens []string) {
i := strings.Index(s, "(")
j := strings.Index(s, ")")
if i != -1 && j != 1 {
s = s[i+1 : j]
}
return strings.Split(s, sep)
}
func toValue(s string) (value interface{}, err error) {
s = strings.TrimSuffix(s, "'")
if d := strings.TrimPrefix(s, "datetime'"); d != s {
value, err = time.Parse(TimeLayout, d)
} else if strings.HasPrefix(s, "'") == false {
value, err = strconv.ParseFloat(s, 64)
} else {
value = strings.TrimPrefix(s, "'")
}
return
}