-
Notifications
You must be signed in to change notification settings - Fork 33
/
query.go
320 lines (278 loc) · 7.08 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
package simdb
import (
"fmt"
"reflect"
"strings"
)
const (
signEq = "="
signEqEng = "eq"
signNotEq = "!="
signNotEqEng = "neq"
signNotEqAnother = "<>"
signGt = ">"
signGtEng = "gt"
signLt = "<"
signLtEng = "lt"
signGtE = ">="
signGtEEng = "gte"
signLtE = "<="
signLtEEng = "lte"
signStrictContains = "strictContains"
signContains = "contains"
signEndsWith = "endsWith"
signStartsWith = "startsWith"
signIn = "in"
signNotIn = "notIn"
signLenEq = "leneq"
signLenNotEq = "lenneq"
signLenGt = "lengt"
signLenGte = "lengte"
signLenLt = "lenlt"
signLenLte = "lenlte"
)
// QueryFunc describes a conditional function which perform comparison
type QueryFunc func(x, y interface{}) (bool, error)
// eq checks whether x, y are deeply eq
func eq(x, y interface{}) (bool, error) {
// if the y value is numeric (int/int8-int64/float32/float64) then convert to float64
if fv, ok := toFloat64(y); ok {
y = fv
}
return reflect.DeepEqual(x, y), nil
}
// neq checks whether x, y are deeply not equal
func neq(x, y interface{}) (bool, error) {
b, err := eq(x, y)
return !b, err
}
// gt checks whether x is greather than y
func gt(x, y interface{}) (bool, error) {
xv, ok := x.(float64)
if !ok {
return false, fmt.Errorf("%v must be numeric", x)
}
// if the y value is numeric (int/int8-int64/float32/float64) then convert to float64
if fv, ok := toFloat64(y); ok {
return xv > fv, nil
}
return false, nil
}
// lt checks whether x is less than y
func lt(x, y interface{}) (bool, error) {
xv, ok := x.(float64)
if !ok {
return false, fmt.Errorf("%v must be numeric", x)
}
// if the y value is numeric (int/int8-int64/float32/float64) then convert to float64
if fv, ok := toFloat64(y); ok {
return xv < fv, nil
}
return false, nil
}
// gte checks whether x is greater than or equal to y
func gte(x, y interface{}) (bool, error) {
xv, ok := x.(float64)
if !ok {
return false, fmt.Errorf("%v must be numeric", x)
}
// if the y value is numeric (int/int8-int64/float32/float64) then convert to float64
if fv, ok := toFloat64(y); ok {
return xv >= fv, nil
}
return false, nil
}
// lte checks whether x is less than or equal to y
func lte(x, y interface{}) (bool, error) {
xv, ok := x.(float64)
if !ok {
return false, fmt.Errorf("%v must be numeric", x)
}
// if the y value is numeric (int/int8-int64/float32/float64) then convert to float64
if fv, ok := toFloat64(y); ok {
return xv <= fv, nil
}
return false, nil
}
// strStrictContains checks if x contains y
// This is case sensitive search
func strStrictContains(x, y interface{}) (bool, error) {
xv, okX := x.(string)
if !okX {
return false, fmt.Errorf("%v must be string", x)
}
yv, okY := y.(string)
if !okY {
return false, fmt.Errorf("%v must be string", y)
}
return strings.Contains(xv, yv), nil
}
// strContains checks if x contains y
// This is case insensitive search
func strContains(x, y interface{}) (bool, error) {
xv, okX := x.(string)
if !okX {
return false, fmt.Errorf("%v must be string", x)
}
yv, okY := y.(string)
if !okY {
return false, fmt.Errorf("%v must be string", y)
}
return strings.Contains(strings.ToLower(xv), strings.ToLower(yv)), nil
}
// strStartsWith checks if x starts with y
func strStartsWith(x, y interface{}) (bool, error) {
xv, okX := x.(string)
if !okX {
return false, fmt.Errorf("%v must be string", x)
}
yv, okY := y.(string)
if !okY {
return false, fmt.Errorf("%v must be string", y)
}
return strings.HasPrefix(xv, yv), nil
}
// strEndsWith checks if x ends with y
func strEndsWith(x, y interface{}) (bool, error) {
xv, okX := x.(string)
if !okX {
return false, fmt.Errorf("%v must be string", x)
}
yv, okY := y.(string)
if !okY {
return false, fmt.Errorf("%v must be string", y)
}
return strings.HasSuffix(xv, yv), nil
}
// in checks if x exists in y e.g: in("id", []int{1,3,5,8})
func in(x, y interface{}) (bool, error) {
if yv, ok := y.([]string); ok {
for _, v := range yv {
if ok, _ := eq(x, v); ok {
return true, nil
}
}
}
if yv, ok := y.([]int); ok {
for _, v := range yv {
if ok, _ := eq(x, v); ok {
return true, nil
}
}
}
if yv, ok := y.([]float64); ok {
for _, v := range yv {
if ok, _ := eq(x, v); ok {
return true, nil
}
}
}
return false, nil
}
// notIn checks if x doesn't exists in y e.g: in("id", []int{1,3,5,8})
func notIn(x, y interface{}) (bool, error) {
b, err := in(x, y)
return !b, err
}
// lenEq checks if the string/array/list value is equal
func lenEq(x, y interface{}) (bool, error) {
yv, ok := y.(int)
if !ok {
return false, fmt.Errorf("%v must be integer", y)
}
xv, err := length(x)
if err != nil {
return false, err
}
return xv == yv, nil
}
// lenNotEq checks if the string/array/list value is not equal
func lenNotEq(x, y interface{}) (bool, error) {
yv, ok := y.(int)
if !ok {
return false, fmt.Errorf("%v must be integer", y)
}
xv, err := length(x)
if err != nil {
return false, err
}
return xv != yv, nil
}
// lenGt checks if the string/array/list value is greater
func lenGt(x, y interface{}) (bool, error) {
yv, ok := y.(int)
if !ok {
return false, fmt.Errorf("%v must be integer", y)
}
xv, err := length(x)
if err != nil {
return false, err
}
return xv > yv, nil
}
// lenLt checks if the string/array/list value is less
func lenLt(x, y interface{}) (bool, error) {
yv, ok := y.(int)
if !ok {
return false, fmt.Errorf("%v must be integer", y)
}
xv, err := length(x)
if err != nil {
return false, err
}
return xv < yv, nil
}
// lenGte checks if the string/array/list value is greater than equal
func lenGte(x, y interface{}) (bool, error) {
yv, ok := y.(int)
if !ok {
return false, fmt.Errorf("%v must be integer", y)
}
xv, err := length(x)
if err != nil {
return false, err
}
return xv >= yv, nil
}
// lenLte checks if the string/array/list value is less than equal
func lenLte(x, y interface{}) (bool, error) {
yv, ok := y.(int)
if !ok {
return false, fmt.Errorf("%v must be integer", y)
}
xv, err := length(x)
if err != nil {
return false, err
}
return xv <= yv, nil
}
func loadDefaultQueryMap() map[string]QueryFunc {
// queryMap contains the registered conditional functions
var queryMap = make(map[string]QueryFunc)
queryMap[signEq] = eq
queryMap[signEqEng] = eq
queryMap[signNotEq] = neq
queryMap[signNotEqEng] = neq
queryMap[signNotEqAnother] = neq // also an alias of not equal
queryMap[signGt] = gt
queryMap[signGtEng] = gt
queryMap[signLt] = lt
queryMap[signLtEng] = lt
queryMap[signGtE] = gte
queryMap[signGtEEng] = gte
queryMap[signLtE] = lte
queryMap[signLtEEng] = lte
queryMap[signStrictContains] = strStrictContains
queryMap[signContains] = strContains
queryMap[signStartsWith] = strStartsWith
queryMap[signEndsWith] = strEndsWith
queryMap[signIn] = in
queryMap[signNotIn] = notIn
queryMap[signLenEq] = lenEq
queryMap[signLenNotEq] = lenNotEq
queryMap[signLenGt] = lenGt
queryMap[signLenGte] = lenGte
queryMap[signLenLt] = lenLt
queryMap[signLenLte] = lenLte
return queryMap
}