-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.go
354 lines (301 loc) · 7.64 KB
/
filter.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
package gom
import (
"fmt"
"strings"
"time"
"go.mongodb.org/mongo-driver/bson"
)
// FilterOp is string represent enumeration of supported filter command
type FilterOp string
const (
// OpAnd is AND
OpAnd FilterOp = "$and"
// OpOr is OR
OpOr = "$or"
// OpNot is Not
OpNot = "$not"
// OpEq is Equal
OpEq = "$eq"
// OpNe is Not Equal
OpNe = "$ne"
// OpGte is Greater than or Equal
OpGte = "$gte"
// OpGt is Greater than
OpGt = "$gt"
// OpLt is Less than
OpLt = "$lt"
// OpLte is Less than or equal
OpLte = "$lte"
// OpRange is range from until
OpRange = "$range"
// OpContains is Contains
OpContains = "$contains"
// OpStartWith is Start with
OpStartWith = "$startwith"
// OpEndWith is End with
OpEndWith = "$endwith"
// OpIn is In
OpIn = "$in"
// OpNin is Not in
OpNin = "$nin"
// OpSort is Sort
OpSort = "$sort"
// OpBetween is Between (Custom)
OpBetween = "between"
// OpExists is Exists
OpExists = "$exists"
// OpBetweenEq is Between Equal
OpBetweenEq = "betweenEq"
// OpRangeEq is Range Equal
OpRangeEq = "rangeEq"
// ElemMatch is Elem Match operator
OpElemMatch = "$elemMatch"
)
// Filter holding Items, Field, Operation, and Value
type Filter struct {
Items []*Filter
Field string
Op FilterOp
Value interface{}
}
// newFilter create new filter with given parameter
func newFilter(field string, op FilterOp, v interface{}, items []*Filter) *Filter {
f := new(Filter)
f.Field = field
f.Op = op
f.Value = v
if items != nil {
f.Items = items
}
return f
}
// And create new filter with And operation
func And(items ...*Filter) *Filter {
return newFilter("", OpAnd, nil, items)
}
// Or create new filter with Or operation
func Or(items ...*Filter) *Filter {
return newFilter("", OpOr, nil, items)
}
// Sort create new filter with Sort operation
func Sort(field string, sortType string) *Filter {
sort := -1
if strings.ToLower(sortType) == "asc" {
sort = 1
}
return newFilter(field, OpSort, sort, nil)
}
// Eq create new filter with Eq operation
func Eq(field string, v interface{}) *Filter {
return newFilter(field, OpEq, v, nil)
}
// Not create new filter with Not operation
func Not(item *Filter) *Filter {
return newFilter("", OpNot, nil, []*Filter{item})
}
// Ne create new filter with Ne operation
func Ne(field string, v interface{}) *Filter {
return newFilter(field, OpNe, v, nil)
}
// Gte create new filter with Gte operation
func Gte(field string, v interface{}) *Filter {
return newFilter(field, OpGte, v, nil)
}
// Gt create new filter with Gt operation
func Gt(field string, v interface{}) *Filter {
return newFilter(field, OpGt, v, nil)
}
// Lt create new filter with Lt operation
func Lt(field string, v interface{}) *Filter {
return newFilter(field, OpLt, v, nil)
}
// Lte create new filter with Lte operation
func Lte(field string, v interface{}) *Filter {
return newFilter(field, OpLte, v, nil)
}
// Range create new filter with Range operation
func Range(field string, from, to interface{}) *Filter {
f := newFilter(field, OpRange, nil, nil)
f.Value = []interface{}{from, to}
return f
}
// Between create new filter with Between operation (Custom)
func Between(field string, gt, lt interface{}) *Filter {
f := newFilter(field, OpBetween, nil, nil)
f.Value = []interface{}{gt, lt}
return f
}
// RangeEq create new filter with Range Equal operation
func RangeEq(field string, from, to interface{}) *Filter {
f := newFilter(field, OpRangeEq, nil, nil)
f.Value = []interface{}{from, to}
return f
}
// BetweenEq create new filter with Between Equal operation (Custom)
func BetweenEq(field string, gte, lte interface{}) *Filter {
f := newFilter(field, OpBetweenEq, nil, nil)
f.Value = []interface{}{gte, lte}
return f
}
// In create new filter with In operation
func In(field string, inValues ...interface{}) *Filter {
f := new(Filter)
f.Field = field
f.Op = OpIn
f.Value = inValues
return f
}
// Nin create new filter with Nin operation
func Nin(field string, ninValues ...interface{}) *Filter {
f := new(Filter)
f.Field = field
f.Op = OpNin
f.Value = ninValues
return f
}
// Contains create new filter with Contains operation
func Contains(field string, values ...string) *Filter {
f := new(Filter)
f.Field = field
f.Op = OpContains
f.Value = values
return f
}
// StartWith create new filter with StartWith operation
func StartWith(field string, value string) *Filter {
f := new(Filter)
f.Field = field
f.Op = OpStartWith
f.Value = value
return f
}
// EndWith create new filter with EndWith operation
func EndWith(field string, value string) *Filter {
f := new(Filter)
f.Field = field
f.Op = OpEndWith
f.Value = value
return f
}
// Exists match the documents that contain the field
func Exists(field string, value bool) *Filter {
f := new(Filter)
f.Field = field
f.Op = OpExists
f.Value = value
return f
}
// Exists match the documents that contain the field
func ElemMatch(field string, filter *Filter) *Filter {
f := new(Filter)
f.Field = field
f.Op = OpElemMatch
f.Value = filter
return f
}
// BuildFilter = Build gom filter
func BuildFilter(filter *Filter) bson.M {
main := bson.M{}
inside := bson.M{}
switch filter.Op {
case OpAnd, OpOr:
insideArr := []interface{}{}
for _, fi := range filter.Items {
fRes := BuildFilter(fi)
insideArr = append(insideArr, fRes)
}
main[string(filter.Op)] = insideArr
case OpEq, OpNe, OpGt, OpGte, OpLt, OpLte, OpIn, OpNin, OpSort, OpExists:
inside[string(filter.Op)] = filter.Value
main[filter.Field] = inside
// case OpSort:
// inside.Set(string(filter.Op), filter.Value)
// main.Set(filter.Field, inside)
case OpBetween, OpRange:
switch filter.Value.([]interface{})[0].(type) {
case int:
gt := 0
lt := 0
if filter.Value != nil {
gt = filter.Value.([]interface{})[0].(int)
lt = filter.Value.([]interface{})[1].(int)
}
main[filter.Field] = bson.M{
"$gt": gt,
"$lt": lt,
}
case time.Time:
gt := time.Now()
lt := time.Now()
if filter.Value != nil {
gt = filter.Value.([]interface{})[0].(time.Time)
lt = filter.Value.([]interface{})[1].(time.Time)
}
main[filter.Field] = bson.M{
"$gt": gt,
"$lt": lt,
}
}
case OpBetweenEq, OpRangeEq:
switch filter.Value.([]interface{})[0].(type) {
case int:
gt := 0
lt := 0
if filter.Value != nil {
gt = filter.Value.([]interface{})[0].(int)
lt = filter.Value.([]interface{})[1].(int)
}
main[filter.Field] = bson.M{
"$gte": gt,
"$lte": lt,
}
case time.Time:
gt := time.Now()
lt := time.Now()
if filter.Value != nil {
gt = filter.Value.([]interface{})[0].(time.Time)
lt = filter.Value.([]interface{})[1].(time.Time)
}
main[filter.Field] = bson.M{
"$gte": gt,
"$lte": lt,
}
}
case OpStartWith:
main[filter.Field] = bson.M{
"$regex": fmt.Sprintf("^%s.*$", filter.Value),
"$options": "i",
}
case OpEndWith:
main[filter.Field] = bson.M{
"$regex": fmt.Sprintf("^.*%s$", filter.Value),
"$options": "i",
}
case OpContains:
if len(filter.Value.([]string)) > 1 {
bfs := []interface{}{}
for _, ff := range filter.Value.([]string) {
pfm := bson.M{}
pfm[filter.Field] = bson.M{
"$regex": fmt.Sprintf(".*%s.*", ff),
"$options": "i",
}
bfs = append(bfs, pfm)
}
main["$or"] = bfs
} else {
main[filter.Field] = bson.M{
"$regex": fmt.Sprintf(".*%s.*", filter.Value.([]string)[0]),
"$options": "i",
}
}
case OpNot:
// field := filter.Items[0].Field
// main.Set(field, toolkit.M{}.Set("$not", filter.Items[0].Field))
// toolkit.Println(toolkit.JsonStringIndent(main, "\n"))
case OpElemMatch:
inside[string(filter.Op)] = BuildFilter(filter.Value.(*Filter))
main[filter.Field] = inside
}
return main
}