forked from continuum-samreen-shaikh/relay-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.go
595 lines (542 loc) · 16.6 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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
package relay
import (
"errors"
"reflect"
"strconv"
"strings"
)
//DataSet : DataSet is data type used for filtering data.
type DataSet struct {
bMatched bool
CurRec interface{}
}
//IsColumnNameValid : To validate Column Name
func IsColumnNameValid(Column *string, val reflect.Value) bool {
ColumnUp := strings.ToUpper(*Column)
var ColumnFound = false
for j := 0; j < val.NumField(); j++ {
if ColumnUp == strings.ToUpper(val.Type().Field(j).Name) {
*Column = val.Type().Field(j).Name
ColumnFound = true
break
}
}
return ColumnFound
}
//GetProfilesSubQueries : Function to Get Sub Queries with ProfileIDs
func GetProfilesSubQueries(filterConditions string) ([]string, error) {
subQuery := GetSubQueries(filterConditions, OPAND, OPOR)
if strings.Contains(filterConditions, ProfileKeyCheck) && len(subQuery) > 1 {
subQueryLen := len(subQuery)
//ProfileID filter needs to be at the end
if !strings.Contains(subQuery[subQueryLen-1], ProfileKeyCheck) {
if strings.Contains(subQuery[0], ProfileKeyCheck) {
return nil, errors.New("Profile ID Filter Key cannot be first argument!!!")
} else {
for i := range subQuery {
if strings.Contains(subQuery[i], ProfileKeyCheck) {
subQuery[subQueryLen-1], subQuery[i] = subQuery[i], subQuery[subQueryLen-1]
break
}
}
}
}
}
return subQuery, nil
}
//FilterProfiles : Filters Profile data on given filterConditions.
func FilterProfiles(filterConditions string, sortCond string, val reflect.Value, arraySlice []interface{}) ([]interface{}, int, error) {
var isProfileIDFound = false
arraySliceRS := make(map[int]DataSet)
var arraySliceRet []interface{}
var arraySliceRetZeroScore []interface{}
var arraySliceRetProNil []interface{}
var arraySliceRetNotFound []interface{}
for ind := range arraySlice {
arraySliceRS[ind] = DataSet{bMatched: false, CurRec: arraySlice[ind]}
}
subQuery, err := GetProfilesSubQueries(filterConditions)
if err != nil {
return nil, 0, err
}
for i := range subQuery {
var LOP, Key, Column, Op, CurQuery string
CurQuery = subQuery[i]
if CurQuery[:1] == OPAND || CurQuery[:1] == OPOR {
LOP = CurQuery[:1]
CurQuery = CurQuery[1:]
} else {
LOP = OPBLANK
}
Key, Column, Op = GetQueryDetails(CurQuery)
if Key == "" {
return nil, 0, errors.New("Filter Key not found!!!")
}
if Column == "" {
return nil, 0, errors.New("Filter Column not found!!!")
}
var ColumnSlice []string
if strings.Contains(Column, ".") {
ColumnSlice = strings.Split(Column, ".")
Column = ColumnSlice[0]
}
if !IsColumnNameValid(&Column, val) {
return nil, 0, errors.New("Filter [" + Column + "] No such column exist!!!")
}
for k := 0; k < len(arraySliceRS); k++ {
bConsiderMe := false
if LOP == OPBLANK || LOP == OPOR {
bConsiderMe = !arraySliceRS[k].bMatched
} else if LOP == OPAND {
bConsiderMe = arraySliceRS[k].bMatched
}
if bConsiderMe {
bMatched := false
CurField := reflect.Indirect(reflect.ValueOf(arraySliceRS[k].CurRec)).FieldByName(Column)
if isPrimitive(CurField) {
if bMatched, err = processPrimitive(CurField, Key, Op); err != nil {
return nil, 0, err
}
} else if CurField.Kind() == reflect.Slice {
bMatched = true
if reflect.Indirect(reflect.ValueOf(arraySliceRS[k].CurRec)).FieldByName(Column).IsNil() {
arraySliceRetProNil = append(arraySliceRetProNil, arraySliceRS[k].CurRec)
} else {
sliceLen := CurField.Len()
if !IsColumnNameValid(&ColumnSlice[1], CurField.Index(0)) {
return nil, 0, errors.New("Filter [" + ColumnSlice[1] + "] No such column exist!!!")
}
for lenslice := 0; lenslice < sliceLen; lenslice++ {
CurFieldVal := CurField.Index(lenslice).FieldByName(ColumnSlice[1])
if CurFieldVal.String() == Key {
if lenslice != 0 {
x, y := CurField.Index(lenslice).Interface(), CurField.Index(0).Interface()
CurField.Index(lenslice).Set(reflect.ValueOf(y))
CurField.Index(0).Set(reflect.ValueOf(x))
}
isProfileIDFound = true
if strings.Contains(sortCond, "RPTSCORE") {
ScoreVal := CurField.Index(0).FieldByName("Score")
if ScoreVal.Int() == 0 {
arraySliceRetZeroScore = append(arraySliceRetZeroScore, arraySliceRS[k].CurRec)
} else {
arraySliceRet = append(arraySliceRet, arraySliceRS[k].CurRec)
}
} else {
arraySliceRet = append(arraySliceRet, arraySliceRS[k].CurRec)
}
break
} else if lenslice+1 == sliceLen {
arraySliceRetNotFound = append(arraySliceRetNotFound, arraySliceRS[k].CurRec)
}
}
}
}
arraySliceRS[k] = DataSet{bMatched: bMatched, CurRec: arraySliceRS[k].CurRec}
}
}
}
var arraySliceRetLen = 0
arraySlice = make([]interface{}, 0)
if !isProfileIDFound {
for l := 0; l < len(arraySliceRS); l++ {
if arraySliceRS[l].bMatched {
arraySlice = append(arraySlice, arraySliceRS[l].CurRec)
}
}
} else if isProfileIDFound {
arraySliceRetLen = len(arraySliceRet)
arraySlice = append(arraySlice, arraySliceRet...)
arraySlice = append(arraySlice, arraySliceRetZeroScore...)
arraySlice = append(arraySlice, arraySliceRetNotFound...)
arraySlice = append(arraySlice, arraySliceRetProNil...)
}
return arraySlice, arraySliceRetLen, nil
}
//Filter : Filters data on given filterConditions.
func Filter(filterConditions string, val reflect.Value, arraySlice []interface{}) ([]interface{}, error) {
var err error
arraySliceRS := make(map[int]DataSet)
for ind := range arraySlice {
arraySliceRS[ind] = DataSet{bMatched: false, CurRec: arraySlice[ind]}
}
filterConditions = CleanConditions(filterConditions)
arraySliceRS, err = markRecords(filterConditions, val, arraySliceRS)
if err != nil {
return nil, err
}
arraySlice = make([]interface{}, 0)
for l := 0; l < len(arraySliceRS); l++ {
if arraySliceRS[l].bMatched {
arraySlice = append(arraySlice, arraySliceRS[l].CurRec)
}
}
return arraySlice, nil
}
//markRecords : mark records to be filtered based on filterConditions.
func markRecords(filterConditions string, val reflect.Value, arraySlice map[int]DataSet) (map[int]DataSet, error) {
subQuery := GetSubQueries(filterConditions, OPAND+STARTBRACE, OPOR+STARTBRACE)
for i := range subQuery {
var LOP, Key, Column, Op, CurQuery string
CurQuery = subQuery[i]
if CurQuery[:1] == OPAND || CurQuery[:1] == OPOR {
LOP = CurQuery[:1]
CurQuery = CurQuery[1:]
} else {
LOP = OPBLANK
}
Key, Column, Op = GetQueryDetails(CurQuery)
if Key == "" {
return nil, errors.New("Filter Key not found!!!")
}
if Column == "" {
return nil, errors.New("Filter Column not found!!!")
}
var ColumnStruct []string
if strings.Contains(Column, ".") {
ColumnStruct = strings.Split(Column, ".")
Column = ColumnStruct[0]
}
ColumnUp := strings.ToUpper(Column)
var ColumnFound = false
for j := 0; j < val.NumField(); j++ {
if ColumnUp == strings.ToUpper(val.Type().Field(j).Name) {
Column = val.Type().Field(j).Name
ColumnFound = true
}
}
if !ColumnFound {
return nil, errors.New("Filter [" + Column + "] No such column exist!!!")
}
for k := 0; k < len(arraySlice); k++ {
bConsiderMe := false
if LOP == OPBLANK || LOP == OPOR {
bConsiderMe = !arraySlice[k].bMatched
} else if LOP == OPAND {
bConsiderMe = arraySlice[k].bMatched
}
var err error
if bConsiderMe {
bMatched := false
CurField := reflect.Indirect(reflect.ValueOf(arraySlice[k].CurRec)).FieldByName(Column)
if isPrimitive(CurField) {
if bMatched, err = processPrimitive(CurField, Key, Op); err != nil {
return nil, err
}
} else if CurField.Kind() == reflect.Struct {
//to avoid out of bound errors we need to check this
if len(ColumnStruct) < 2 {
return nil, errors.New("Filter applied to struct column but inner field not specified")
}
innerField := CurField.FieldByName(ColumnStruct[1])
if !innerField.IsValid() {
return nil, errors.New("Filter [" + ColumnStruct[1] + "] No such column exist!!!")
}
if isPrimitive(innerField) {
if bMatched, err = processPrimitive(innerField, Key, Op); err != nil {
return nil, err
}
} else {
return nil, errors.New("Filter [" + ColumnStruct[1] + "] Column is not primitive type")
}
}
arraySlice[k] = DataSet{bMatched: bMatched, CurRec: arraySlice[k].CurRec}
}
}
}
return arraySlice, nil
}
func isPrimitive(CurField reflect.Value) bool {
switch CurField.Kind() {
case reflect.Bool, reflect.String, reflect.Float32, reflect.Float64, reflect.Int, reflect.Int32, reflect.Int64:
return true
}
return false
}
func processPrimitive(CurField reflect.Value, Key string, Op string) (bMatched bool, err error) {
switch CurField.Kind() {
case reflect.Bool:
b, _ := strconv.ParseBool(Key)
if Op == "" || Op == "==" {
if CurField.Bool() == b {
bMatched = true
}
} else if Op == "!" {
if CurField.Bool() != b {
bMatched = true
}
} else {
return bMatched, errors.New("Filter Invalid Operator [" + Op + "] Applied!!!")
}
case reflect.String:
if Key == NILQUERY {
Key = ""
}
if Op == "" {
CurStr := strings.ToLower(CurField.String())
CurKey := strings.ToLower(Key)
if strings.Contains(CurStr, CurKey) {
bMatched = true
}
} else if Op == "==" {
if strings.Contains(CurField.String(), Key) {
bMatched = true
}
} else if Op == "===" {
if CurField.String() == Key {
bMatched = true
}
} else if Op == "!" {
if CurField.String() != Key {
bMatched = true
}
} else if Op == ">" {
if CurField.String() > Key {
bMatched = true
}
} else if Op == ">=" {
if CurField.String() >= Key {
bMatched = true
}
} else if Op == "<" {
if CurField.String() < Key {
bMatched = true
}
} else if Op == "<=" {
if CurField.String() <= Key {
bMatched = true
}
} else {
return bMatched, errors.New("Filter Invalid Operator [" + Op + "] Applied!!!")
}
case reflect.Float32, reflect.Float64:
f, _ := strconv.ParseFloat(Key, 64)
if Op == "" || Op == "==" {
if CurField.Float() == f {
bMatched = true
}
} else if Op == "!" {
if CurField.Float() != f {
bMatched = true
}
} else if Op == ">" {
if CurField.Float() > f {
bMatched = true
}
} else if Op == ">=" {
if CurField.Float() >= f {
bMatched = true
}
} else if Op == "<" {
if CurField.Float() < f {
bMatched = true
}
} else if Op == "<=" {
if CurField.Float() <= f {
bMatched = true
}
} else {
return bMatched, errors.New("Filter Invalid Operator [" + Op + "] Applied!!!")
}
case reflect.Int, reflect.Int32, reflect.Int64:
i, _ := strconv.ParseInt(Key, 10, 64)
if Op == "" || Op == "==" {
if CurField.Int() == i {
bMatched = true
}
} else if Op == "!" {
if CurField.Int() != i {
bMatched = true
}
} else if Op == ">" {
if CurField.Int() > i {
bMatched = true
}
} else if Op == ">=" {
if CurField.Int() >= i {
bMatched = true
}
} else if Op == "<" {
if CurField.Int() < i {
bMatched = true
}
} else if Op == "<=" {
if CurField.Int() <= i {
bMatched = true
}
} else {
return bMatched, errors.New("Filter Invalid Operator [" + Op + "] Applied!!!")
}
default:
return bMatched, errors.New("Field is not primitive type")
}
return bMatched, nil
}
//PriorityFilter : Filters data based on priority of given complex filterConditions.
func PriorityFilter(filterConditions string, val reflect.Value, arraySlice []interface{}) ([]interface{}, error) {
var err error
arraySliceRS := make(map[int]DataSet)
for ind := range arraySlice {
arraySliceRS[ind] = DataSet{bMatched: false, CurRec: arraySlice[ind]}
}
filterConditions = CleanConditions(filterConditions)
arraySliceRS, err = ResolveFilterConditions(filterConditions, val, arraySliceRS)
if err != nil {
return nil, err
}
arraySlice = make([]interface{}, 0)
for l := 0; l < len(arraySliceRS); l++ {
if arraySliceRS[l].bMatched {
arraySlice = append(arraySlice, arraySliceRS[l].CurRec)
}
}
return arraySlice, nil
}
//ResolveFilterConditions : Resolve Filter Conditions based on priority
func ResolveFilterConditions(filter string, val reflect.Value, dataMap map[int]DataSet) (map[int]DataSet, error) {
var err error
dataMapRet := make(map[int]DataSet)
var bExtractForward, bIsComplexFilter, bImbalance bool
var pendingFilter, curCondition, curOperator string
if strings.HasPrefix(filter, STARTBRACE+STARTBRACE) && strings.HasSuffix(filter, ENDBRACE+ENDBRACE) {
iBalancePos, iLength := GetBalancePosition(filter)
if iBalancePos == iLength-1 {
filter = filter[1 : len(filter)-1]
} else {
bImbalance = true
curCondition = filter[:iBalancePos+1]
curOperator = filter[iBalancePos+1 : iBalancePos+2]
pendingFilter = filter[iBalancePos+2:]
}
}
if bImbalance {
dataMapCopy := make(map[int]DataSet)
for k, v := range dataMap {
dataMapCopy[k] = v
}
dataMap, err = ResolveFilterConditions(pendingFilter, val, dataMap)
if err != nil {
return nil, err
}
dataMapCopy, err = ResolveFilterConditions(curCondition, val, dataMapCopy)
if err != nil {
return nil, err
}
dataMapRet, err = MergeFilterResults(dataMap, dataMapCopy, curOperator)
if err != nil {
return nil, err
}
} else {
if strings.HasPrefix(filter, STARTBRACE) && strings.HasSuffix(filter, ENDBRACE+ENDBRACE) {
bExtractForward = true
bIsComplexFilter = true
} else if strings.HasPrefix(filter, STARTBRACE+STARTBRACE) && strings.HasSuffix(filter, ENDBRACE) {
bExtractForward = false
bIsComplexFilter = true
} else if strings.Contains(filter, STARTBRACE+STARTBRACE) {
bExtractForward = true
bIsComplexFilter = true
} else if strings.Contains(filter, ENDBRACE+ENDBRACE) {
bExtractForward = false
bIsComplexFilter = true
} else {
bIsComplexFilter = false
}
if bIsComplexFilter {
pendingFilter, curCondition, curOperator = ExtractConditions(filter, bExtractForward)
dataMapCopy := make(map[int]DataSet)
for k, v := range dataMap {
dataMapCopy[k] = v
}
dataMap, err = ResolveFilterConditions(pendingFilter, val, dataMap)
if err != nil {
return nil, err
}
dataMapCopy, err = markRecords(curCondition, val, dataMapCopy)
if err != nil {
return nil, err
}
dataMapRet, err = MergeFilterResults(dataMap, dataMapCopy, curOperator)
if err != nil {
return nil, err
}
} else {
curCondition = filter
dataMapRet, err = markRecords(curCondition, val, dataMap)
if err != nil {
return nil, err
}
}
}
return dataMapRet, nil
}
//ExtractConditions : Extracts simplest conditions from complex conditions.
func ExtractConditions(sFilter string, bExtractForward bool) (pendingFilter string, curCondition string, curOperator string) {
var i, iAnd, iOr int
i = -1
if bExtractForward {
iAnd = strings.Index(sFilter, OPAND)
iOr = strings.Index(sFilter, OPOR)
} else {
iAnd = strings.LastIndex(sFilter, OPAND)
iOr = strings.LastIndex(sFilter, OPOR)
}
if iAnd > -1 && iOr > -1 {
i = iOr
if bExtractForward && (iAnd < iOr) {
i = iAnd
} else if !bExtractForward && (iAnd > iOr) {
i = iAnd
}
} else if iAnd > -1 {
i = iAnd
} else if iOr > -1 {
i = iOr
}
if bExtractForward {
curCondition = sFilter[0:i]
curOperator = sFilter[i : i+1]
pendingFilter = sFilter[i+1:]
} else {
curCondition = sFilter[i+1:]
curOperator = sFilter[i : i+1]
pendingFilter = sFilter[:i]
}
return pendingFilter, curCondition, curOperator
}
//MergeFilterResults : Merges Filter Conditions results
func MergeFilterResults(dataMapLeft, dataMapRight map[int]DataSet, curOperation string) (map[int]DataSet, error) {
if len(dataMapLeft) != len(dataMapRight) {
return nil, errors.New("MergeFilterResults Length Check Failed!!!")
}
if curOperation != OPOR && curOperation != OPAND {
return nil, errors.New("Filter Invalid Operator [" + curOperation + "] Applied!!!")
}
for i := 0; i < len(dataMapLeft); i++ {
bMatched := false
if curOperation == OPOR {
bMatched = dataMapLeft[i].bMatched || dataMapRight[i].bMatched
} else if curOperation == OPAND {
bMatched = dataMapLeft[i].bMatched && dataMapRight[i].bMatched
}
dataMapLeft[i] = DataSet{bMatched: bMatched, CurRec: dataMapLeft[i].CurRec}
}
return dataMapLeft, nil
}
//GetBalancePosition : Gets balancing position for condition.
func GetBalancePosition(sFilter string) (iBalancePos int, iLength int) {
var iStart, iEnd int
iLength = len(sFilter)
for i := 0; i < iLength; i++ {
if sFilter[i:i+1] == STARTBRACE {
iStart++
} else if sFilter[i:i+1] == ENDBRACE {
iEnd++
}
if (iStart == iEnd) && (iStart != 0) {
iBalancePos = i
break
}
}
return iBalancePos, iLength
}