-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontains.go
68 lines (59 loc) · 1.72 KB
/
contains.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
package distil
import (
"fmt"
"reflect"
"strings"
)
type contains struct {
rows []map[string]interface{}
}
func (a *contains) Filter(row map[string]interface{}, filter *Filter) error {
// Check filter.Value datatypes can be used with our filter.Operator.
err := validateDataType(filter)
if err != nil {
return err
}
// Ensure our value has datatype of array.
if reflect.ValueOf(filter.Value).Kind() != reflect.Slice {
return fmt.Errorf("Invalid datatype. Expected array datatype for `%v`, got %T", filter.Field, row[filter.Field])
}
// Skip if the field is nil.
if row[filter.Field] == nil {
return nil
}
// Build a map of our 'contains' values.
valueMap := map[interface{}]bool{}
for _, v := range filter.Value.([]interface{}) {
switch value := v.(type) {
case string:
// Ensure values are mapped as case-insensitive.
valueMap[strings.ToUpper(value)] = true
break
case float32, float64, int, int8, int16, int32, int64:
valueMap[value.(float64)] = true
break
default:
return fmt.Errorf("Invalid datatype. Expected string datatype for array values of `%v`, got %T", filter.Field, row[filter.Field])
}
}
// Do we have a match?
switch value := row[filter.Field].(type) {
case string:
// Ensure values are checked as case-insensitive.
if valueMap[strings.ToUpper(value)] {
a.rows = append(a.rows, row)
}
break
case float32, float64, int, int8, int16, int32, int64:
if valueMap[value] {
a.rows = append(a.rows, row)
}
default:
return fmt.Errorf("Invalid datatype. Expected string datatype for array values of `%v`, got %T", filter.Field, row[filter.Field])
}
return nil
}
// Result implements distiller().
func (a *contains) Result() []map[string]interface{} {
return a.rows
}