This repository has been archived by the owner on Jun 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
query.go
409 lines (349 loc) · 8.34 KB
/
query.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package contentful
import (
"net/url"
"reflect"
"strconv"
"strings"
"time"
)
//Query model
type Query struct {
include uint16
contentType string
fields []string
e map[string]interface{}
ne map[string]interface{}
all map[string][]string
in map[string][]string
nin map[string][]string
exists []string
notExists []string
lt map[string]interface{}
lte map[string]interface{}
gt map[string]interface{}
gte map[string]interface{}
q string
match map[string]string
near map[string]string
within map[string]string
order []string
limit uint16
skip uint16
mime string
locale string
}
//NewQuery initilazies a new query
func NewQuery() *Query {
return &Query{
include: 0,
contentType: "",
fields: []string{},
e: make(map[string]interface{}),
ne: make(map[string]interface{}),
all: make(map[string][]string),
in: make(map[string][]string),
nin: make(map[string][]string),
exists: []string{},
notExists: []string{},
lt: make(map[string]interface{}),
lte: make(map[string]interface{}),
gt: make(map[string]interface{}),
gte: make(map[string]interface{}),
q: "",
match: make(map[string]string),
near: make(map[string]string),
within: make(map[string]string),
order: []string{},
limit: 0,
skip: 0,
mime: "",
locale: "",
}
}
//Include query
func (q *Query) Include(include uint16) *Query {
q.include = include
return q
}
//ContentType query
func (q *Query) ContentType(ct string) *Query {
q.contentType = ct
return q
}
//Select query
func (q *Query) Select(fields []string) *Query {
q.fields = fields
return q
}
//Equal equality query
func (q *Query) Equal(field string, value interface{}) *Query {
q.e[field] = value
return q
}
//NotEqual [ne] query
func (q *Query) NotEqual(field string, value interface{}) *Query {
q.ne[field] = value
return q
}
//All [all] query
func (q *Query) All(field string, value []string) *Query {
q.all[field] = value
return q
}
//In [in] query
func (q *Query) In(field string, value []string) *Query {
q.in[field] = value
return q
}
//NotIn [nin] query
func (q *Query) NotIn(field string, value []string) *Query {
q.nin[field] = value
return q
}
//Exists [exists] query
func (q *Query) Exists(field string) *Query {
q.exists = append(q.exists, field)
return q
}
//NotExists [exists] query
func (q *Query) NotExists(field string) *Query {
q.notExists = append(q.notExists, field)
return q
}
//LessThan [lt] query
func (q *Query) LessThan(field string, value interface{}) *Query {
q.lt[field] = value
return q
}
//LessThanOrEqual [lte] query
func (q *Query) LessThanOrEqual(field string, value interface{}) *Query {
q.lte[field] = value
return q
}
//GreaterThan [gt] query
func (q *Query) GreaterThan(field string, value interface{}) *Query {
q.gt[field] = value
return q
}
//GreaterThanOrEqual [lte] query
func (q *Query) GreaterThanOrEqual(field string, value interface{}) *Query {
q.gte[field] = value
return q
}
//Query param
func (q *Query) Query(qStr string) *Query {
q.q = qStr
return q
}
//Match param
func (q *Query) Match(field, match string) *Query {
q.match[field] = match
return q
}
//Near param
func (q *Query) Near(field string, lat, lon int16) *Query {
q.near[field] = strconv.Itoa(int(lat)) + "," + strconv.Itoa(int(lon))
return q
}
//Within param
func (q *Query) Within(field string, lat1, lon1, lat2, lon2 int16) *Query {
q.within[field] = strconv.Itoa(int(lat1)) + "," + strconv.Itoa(int(lon1)) + "," + strconv.Itoa(int(lat2)) + "," + strconv.Itoa(int(lon2))
return q
}
//WithinRadius param
func (q *Query) WithinRadius(field string, lat1, lon1, radius int16) *Query {
q.within[field] = strconv.Itoa(int(lat1)) + "," + strconv.Itoa(int(lon1)) + "," + strconv.Itoa(int(radius))
return q
}
//Order param
func (q *Query) Order(field string, reverse bool) *Query {
if reverse {
q.order = append(q.order, "-"+field)
} else {
q.order = append(q.order, field)
}
return q
}
//Limit query
func (q *Query) Limit(limit uint16) *Query {
q.limit = limit
return q
}
//Skip query
func (q *Query) Skip(skip uint16) *Query {
q.skip = skip
return q
}
//MimeType query
func (q *Query) MimeType(mime string) *Query {
q.mime = mime
return q
}
//Locale query
func (q *Query) Locale(locale string) *Query {
q.locale = locale
return q
}
// Values constructs url.Values
func (q *Query) Values() url.Values {
params := url.Values{}
if q.include != 0 {
if q.include > 10 {
panic("include value should be between 0 and 10")
}
params.Set("include", strconv.Itoa(int(q.include)))
}
if q.contentType != "" {
params.Set("content_type", q.contentType)
}
if len(q.fields) > 0 {
if len(q.fields) > 100 {
panic("You can select up to 100 properties for `select`")
}
for _, sel := range q.fields {
if len(strings.Split(sel, ".")) > 2 {
panic("you should provide at most 2 depth for `select`")
}
}
if q.contentType == "" {
panic("you should provide content_type parameter")
}
params.Set("select", strings.Join(q.fields, ","))
}
for k, v := range q.e {
switch reflect.TypeOf(v).Kind() {
case reflect.Int:
{
intV := reflect.ValueOf(v).Interface().(int)
params.Set(k, strconv.Itoa(intV))
}
case reflect.String:
{
strV := reflect.ValueOf(v).Interface().(string)
params.Set(k, strV)
}
}
}
for k, v := range q.ne {
switch reflect.TypeOf(v).Kind() {
case reflect.Int:
{
intV := reflect.ValueOf(v).Interface().(int)
params.Set(k+"[ne]", strconv.Itoa(intV))
}
case reflect.String:
{
strV := reflect.ValueOf(v).Interface().(string)
params.Set(k+"[ne]", strV)
}
}
}
for k, v := range q.all {
params.Set(k+"[all]", strings.Join(v, ","))
}
for k, v := range q.in {
params.Set(k+"[in]", strings.Join(v, ","))
}
for k, v := range q.nin {
params.Set(k+"[nin]", strings.Join(v, ","))
}
for _, v := range q.exists {
params.Set(v+"[exists]", "true")
}
for _, v := range q.notExists {
params.Set(v+"[exists]", "false")
}
for k, v := range q.lt {
switch reflect.TypeOf(v).Kind() {
case reflect.Int:
{
intV := reflect.ValueOf(v).Interface().(int)
params.Set(k+"[lt]", strconv.Itoa(intV))
}
case reflect.Struct:
{
timeV := reflect.ValueOf(v).Interface().(time.Time)
params.Set(k+"[lt]", timeV.Format("2006-01-02 15:04:05"))
}
}
}
for k, v := range q.lte {
switch reflect.TypeOf(v).Kind() {
case reflect.Int:
{
intV := reflect.ValueOf(v).Interface().(int)
params.Set(k+"[lte]", strconv.Itoa(intV))
}
case reflect.Struct:
{
timeV := reflect.ValueOf(v).Interface().(time.Time)
params.Set(k+"[lte]", timeV.Format("2006-01-02 15:04:05"))
}
}
}
for k, v := range q.gt {
switch reflect.TypeOf(v).Kind() {
case reflect.Int:
{
intV := reflect.ValueOf(v).Interface().(int)
params.Set(k+"[gt]", strconv.Itoa(intV))
}
case reflect.Struct:
{
timeV := reflect.ValueOf(v).Interface().(time.Time)
params.Set(k+"[gt]", timeV.Format("2006-01-02 15:04:05"))
}
}
}
for k, v := range q.gte {
switch reflect.TypeOf(v).Kind() {
case reflect.Int:
{
intV := reflect.ValueOf(v).Interface().(int)
params.Set(k+"[gte]", strconv.Itoa(intV))
}
case reflect.Struct:
{
timeV := reflect.ValueOf(v).Interface().(time.Time)
params.Set(k+"[gte]", timeV.Format("2006-01-02 15:04:05"))
}
}
}
if q.q != "" {
params.Set("query", q.q)
}
for k, v := range q.match {
params.Set(k+"[match]", v)
}
for k, v := range q.near {
params.Set(k+"[near]", v)
}
for k, v := range q.within {
params.Set(k+"[within]", v)
}
if len(q.order) > 0 {
// if q.contentType == "" {
// panic("you should provide a content type for order queries")
// }
params.Set("order", strings.Join(q.order, ","))
}
if q.limit != 0 {
if q.limit > 1000 {
panic("limit value should be between 0 and 1000")
}
params.Set("limit", strconv.Itoa(int(q.limit)))
}
if q.skip != 0 {
params.Set("skip", strconv.Itoa(int(q.skip)))
}
if q.mime != "" {
params.Set("mimetype_group", q.mime)
}
if q.locale != "" {
params.Set("locale", q.locale)
}
return params
}
func (q *Query) String() string {
return q.Values().Encode()
}