-
Notifications
You must be signed in to change notification settings - Fork 139
/
query.go
305 lines (277 loc) · 7.18 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
package gojsonq
import (
"fmt"
"reflect"
"strings"
)
const (
operatorEq = "="
operatorEqEng = "eq"
operatorNotEq = "!="
operatorNotEqEng = "neq"
operatorNotEqAnother = "<>"
operatorGt = ">"
operatorGtEng = "gt"
operatorLt = "<"
operatorLtEng = "lt"
operatorGtE = ">="
operatorGtEEng = "gte"
operatorLtE = "<="
operatorLtEEng = "lte"
operatorStrictContains = "strictContains"
operatorContains = "contains"
operatorEndsWith = "endsWith"
operatorStartsWith = "startsWith"
operatorIn = "in"
operatorNotIn = "notIn"
operatorLenEq = "leneq"
operatorLenNotEq = "lenneq"
operatorLenGt = "lengt"
operatorLenGte = "lengte"
operatorLenLt = "lenlt"
operatorLenLte = "lenlte"
)
func defaultQueries() map[string]QueryFunc {
return map[string]QueryFunc{
operatorEq: eq,
operatorEqEng: eq,
operatorNotEq: neq,
operatorNotEqEng: neq,
operatorNotEqAnother: neq,
operatorGt: gt,
operatorGtEng: gt,
operatorLt: lt,
operatorLtEng: lt,
operatorGtE: gte,
operatorGtEEng: gte,
operatorLtE: lte,
operatorLtEEng: lte,
operatorStrictContains: strStrictContains,
operatorContains: strContains,
operatorStartsWith: strStartsWith,
operatorEndsWith: strEndsWith,
operatorIn: in,
operatorNotIn: notIn,
operatorLenEq: lenEq,
operatorLenNotEq: lenNotEq,
operatorLenGt: lenGt,
operatorLenGte: lenGte,
operatorLenLt: lenLt,
operatorLenLte: 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
}