-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlt.go
65 lines (58 loc) · 1.43 KB
/
lt.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
package distil
import "fmt"
type lt struct {
rows []map[string]interface{}
}
// Filter implements distiller().
func (a *lt) 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 the values we're checking aren't nil.
if filter.Value == nil || row[filter.Field] == nil {
return nil
}
// Handle equality check for the different operator types this filter can
// action.
switch filter.Operator.Type {
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
}
// Compare.
if (*baseline).Cmp(*datum) == -1 {
a.rows = append(a.rows, row)
}
break
case operatorTypeDateime:
// Cast to time.Time.
datum, err := castToDatetime(filter.Value)
if err != nil {
return err
}
baseline, err := castToDatetime(row[filter.Field])
if err != nil {
return err
}
// Compare.
if baseline.Before(*datum) {
a.rows = append(a.rows, row)
}
break
default:
return fmt.Errorf("Invalid datatype. Expected string/dateime when applying %v operator, got %T for `%v`", filter.Operator.Type, filter.Value, filter.Field)
}
return nil
}
// Result implements distiller().
func (a *lt) Result() []map[string]interface{} {
return a.rows
}