-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
lteq.go
59 lines (52 loc) · 1.37 KB
/
lteq.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
package distil
import "fmt"
type lteq struct {
rows []map[string]interface{}
}
func (a *lteq) 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
}
if filter.Value == nil || row[filter.Field] == nil {
return nil
}
// The `lteq` operator can only be run on number/datetime datatypes.
switch filter.Operator.Type {
case operatorTypeNumber:
// Cast to decimal.Decimals and compare.
datum, err := castToNumber(filter.Value)
if err != nil {
return err
}
baseline, err := castToNumber(row[filter.Field])
if err != nil {
return err
}
if ((*baseline).Cmp(*datum) == -1) || (*baseline).Equals(*datum) {
a.rows = append(a.rows, row)
}
break
case operatorTypeDateime:
// Cast to time.Time and compare.
datum, err := castToDatetime(filter.Value)
if err != nil {
return err
}
baseline, err := castToDatetime(row[filter.Field])
if err != nil {
return err
}
if baseline.Before(*datum) || baseline.Equal(*datum) {
a.rows = append(a.rows, row)
}
break
default:
return fmt.Errorf("Invalid datatype. Expected number/dateime when applying %v operator, got %T for `%v`", filter.Operator.Type, filter.Value, filter.Field)
}
return nil
}
func (a *lteq) Result() []map[string]interface{} {
return a.rows
}