Debian Codegraph @

query.go

raw symbols SCIP
1 package gojsonq
2
3 import (
4 "fmt"
5 "reflect"
6 "strings"
7 )
8
9 const (
10 operatorEq = "="
11 operatorEqEng = "eq"
12 operatorNotEq = "!="
13 operatorNotEqEng = "neq"
14 operatorNotEqAnother = "<>"
15 operatorGt = ">"
16 operatorGtEng = "gt"
17 operatorLt = "<"
18 operatorLtEng = "lt"
19 operatorGtE = ">="
20 operatorGtEEng = "gte"
21 operatorLtE = "<="
22 operatorLtEEng = "lte"
23 operatorStrictContains = "strictContains"
24 operatorContains = "contains"
25 operatorEndsWith = "endsWith"
26 operatorStartsWith = "startsWith"
27 operatorIn = "in"
28 operatorNotIn = "notIn"
29 operatorLenEq = "leneq"
30 operatorLenNotEq = "lenneq"
31 operatorLenGt = "lengt"
32 operatorLenGte = "lengte"
33 operatorLenLt = "lenlt"
34 operatorLenLte = "lenlte"
35 )
36
37 func defaultQueries() map[string]QueryFunc {
38 return map[string]QueryFunc{
39 operatorEq: eq,
40 operatorEqEng: eq,
41 operatorNotEq: neq,
42 operatorNotEqEng: neq,
43 operatorNotEqAnother: neq,
44 operatorGt: gt,
45 operatorGtEng: gt,
46 operatorLt: lt,
47 operatorLtEng: lt,
48 operatorGtE: gte,
49 operatorGtEEng: gte,
50 operatorLtE: lte,
51 operatorLtEEng: lte,
52 operatorStrictContains: strStrictContains,
53 operatorContains: strContains,
54 operatorStartsWith: strStartsWith,
55 operatorEndsWith: strEndsWith,
56 operatorIn: in,
57 operatorNotIn: notIn,
58 operatorLenEq: lenEq,
59 operatorLenNotEq: lenNotEq,
60 operatorLenGt: lenGt,
61 operatorLenGte: lenGte,
62 operatorLenLt: lenLt,
63 operatorLenLte: lenLte,
64 }
65 }
66
67 // QueryFunc describes a conditional function which perform comparison
68 type QueryFunc func(x, y interface{}) (bool, error)
69
70 // eq checks whether x, y are deeply eq
71 func eq(x, y interface{}) (bool, error) {
72 // if the y value is numeric (int/int8-int64/float32/float64) then convert to float64
73 if fv, ok := toFloat64(y); ok {
74 y = fv
75 }
76 return reflect.DeepEqual(x, y), nil
77 }
78
79 // neq checks whether x, y are deeply not equal
80 func neq(x, y interface{}) (bool, error) {
81 b, err := eq(x, y)
82 return !b, err
83 }
84
85 // gt checks whether x is greather than y
86 func gt(x, y interface{}) (bool, error) {
87 xv, ok := x.(float64)
88 if !ok {
89 return false, fmt.Errorf("%v must be numeric", x)
90 }
91 // if the y value is numeric (int/int8-int64/float32/float64) then convert to float64
92 if fv, ok := toFloat64(y); ok {
93 return xv > fv, nil
94 }
95 return false, nil
96 }
97
98 // lt checks whether x is less than y
99 func lt(x, y interface{}) (bool, error) {
100 xv, ok := x.(float64)
101 if !ok {
102 return false, fmt.Errorf("%v must be numeric", x)
103 }
104 // if the y value is numeric (int/int8-int64/float32/float64) then convert to float64
105 if fv, ok := toFloat64(y); ok {
106 return xv < fv, nil
107 }
108 return false, nil
109 }
110
111 // gte checks whether x is greater than or equal to y
112 func gte(x, y interface{}) (bool, error) {
113 xv, ok := x.(float64)
114 if !ok {
115 return false, fmt.Errorf("%v must be numeric", x)
116 }
117 // if the y value is numeric (int/int8-int64/float32/float64) then convert to float64
118 if fv, ok := toFloat64(y); ok {
119 return xv >= fv, nil
120 }
121 return false, nil
122 }
123
124 // lte checks whether x is less than or equal to y
125 func lte(x, y interface{}) (bool, error) {
126 xv, ok := x.(float64)
127 if !ok {
128 return false, fmt.Errorf("%v must be numeric", x)
129 }
130 // if the y value is numeric (int/int8-int64/float32/float64) then convert to float64
131 if fv, ok := toFloat64(y); ok {
132 return xv <= fv, nil
133 }
134 return false, nil
135 }
136
137 // strStrictContains checks if x contains y
138 // This is case sensitive search
139 func strStrictContains(x, y interface{}) (bool, error) {
140 xv, okX := x.(string)
141 if !okX {
142 return false, fmt.Errorf("%v must be string", x)
143 }
144 yv, okY := y.(string)
145 if !okY {
146 return false, fmt.Errorf("%v must be string", y)
147 }
148 return strings.Contains(xv, yv), nil
149 }
150
151 // strContains checks if x contains y
152 // This is case insensitive search
153 func strContains(x, y interface{}) (bool, error) {
154 xv, okX := x.(string)
155 if !okX {
156 return false, fmt.Errorf("%v must be string", x)
157 }
158 yv, okY := y.(string)
159 if !okY {
160 return false, fmt.Errorf("%v must be string", y)
161 }
162 return strings.Contains(strings.ToLower(xv), strings.ToLower(yv)), nil
163 }
164
165 // strStartsWith checks if x starts with y
166 func strStartsWith(x, y interface{}) (bool, error) {
167 xv, okX := x.(string)
168 if !okX {
169 return false, fmt.Errorf("%v must be string", x)
170 }
171 yv, okY := y.(string)
172 if !okY {
173 return false, fmt.Errorf("%v must be string", y)
174 }
175 return strings.HasPrefix(xv, yv), nil
176 }
177
178 // strEndsWith checks if x ends with y
179 func strEndsWith(x, y interface{}) (bool, error) {
180 xv, okX := x.(string)
181 if !okX {
182 return false, fmt.Errorf("%v must be string", x)
183 }
184 yv, okY := y.(string)
185 if !okY {
186 return false, fmt.Errorf("%v must be string", y)
187 }
188 return strings.HasSuffix(xv, yv), nil
189 }
190
191 // in checks if x exists in y e.g: in("id", []int{1,3,5,8})
192 func in(x, y interface{}) (bool, error) {
193 if yv, ok := y.([]string); ok {
194 for _, v := range yv {
195 if ok, _ := eq(x, v); ok {
196 return true, nil
197 }
198 }
199 }
200 if yv, ok := y.([]int); ok {
201 for _, v := range yv {
202 if ok, _ := eq(x, v); ok {
203 return true, nil
204 }
205 }
206 }
207 if yv, ok := y.([]float64); ok {
208 for _, v := range yv {
209 if ok, _ := eq(x, v); ok {
210 return true, nil
211 }
212 }
213 }
214 return false, nil
215 }
216
217 // notIn checks if x doesn't exists in y e.g: in("id", []int{1,3,5,8})
218 func notIn(x, y interface{}) (bool, error) {
219 b, err := in(x, y)
220 return !b, err
221 }
222
223 // lenEq checks if the string/array/list value is equal
224 func lenEq(x, y interface{}) (bool, error) {
225 yv, ok := y.(int)
226 if !ok {
227 return false, fmt.Errorf("%v must be integer", y)
228 }
229 xv, err := length(x)
230 if err != nil {
231 return false, err
232 }
233
234 return xv == yv, nil
235 }
236
237 // lenNotEq checks if the string/array/list value is not equal
238 func lenNotEq(x, y interface{}) (bool, error) {
239 yv, ok := y.(int)
240 if !ok {
241 return false, fmt.Errorf("%v must be integer", y)
242 }
243 xv, err := length(x)
244 if err != nil {
245 return false, err
246 }
247
248 return xv != yv, nil
249 }
250
251 // lenGt checks if the string/array/list value is greater
252 func lenGt(x, y interface{}) (bool, error) {
253 yv, ok := y.(int)
254 if !ok {
255 return false, fmt.Errorf("%v must be integer", y)
256 }
257 xv, err := length(x)
258 if err != nil {
259 return false, err
260 }
261
262 return xv > yv, nil
263 }
264
265 // lenLt checks if the string/array/list value is less
266 func lenLt(x, y interface{}) (bool, error) {
267 yv, ok := y.(int)
268 if !ok {
269 return false, fmt.Errorf("%v must be integer", y)
270 }
271 xv, err := length(x)
272 if err != nil {
273 return false, err
274 }
275
276 return xv < yv, nil
277 }
278
279 // lenGte checks if the string/array/list value is greater than equal
280 func lenGte(x, y interface{}) (bool, error) {
281 yv, ok := y.(int)
282 if !ok {
283 return false, fmt.Errorf("%v must be integer", y)
284 }
285 xv, err := length(x)
286 if err != nil {
287 return false, err
288 }
289
290 return xv >= yv, nil
291 }
292
293 // lenLte checks if the string/array/list value is less than equal
294 func lenLte(x, y interface{}) (bool, error) {
295 yv, ok := y.(int)
296 if !ok {
297 return false, fmt.Errorf("%v must be integer", y)
298 }
299 xv, err := length(x)
300 if err != nil {
301 return false, err
302 }
303
304 return xv <= yv, nil
305 }