-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
starts_with.go
50 lines (44 loc) · 1.19 KB
/
starts_with.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
package distil
import (
"fmt"
"strings"
)
type startsWith struct {
rows []map[string]interface{}
}
func (a *startsWith) 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
}
switch filter.Operator.Type {
case operatorTypeString:
// Ensure values are compared case-insensitive.
s, substr := strings.ToUpper(row[filter.Field].(string)), strings.ToUpper(filter.Value.(string))
// Match.
if strings.HasPrefix(s, substr) {
a.rows = append(a.rows, row)
}
case operatorTypeNumber:
// Cast to number.
datum, err := castToNumber(filter.Value)
if err != nil {
return err
}
baseline, err := castToNumber(row[filter.Field])
if err != nil {
return err
}
if strings.HasPrefix(baseline.String(), datum.String()) {
a.rows = append(a.rows, row)
}
default:
return fmt.Errorf("Invalid datatype. Expected string/number when applying %v operator, got %T for `%v`", filter.Operator.Type, filter.Value, filter.Field)
}
return nil
}
// Result implements distiller().
func (a *startsWith) Result() []map[string]interface{} {
return a.rows
}