-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdistiller.go
153 lines (138 loc) · 4.29 KB
/
distiller.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
package distil
import (
"errors"
"fmt"
"reflect"
"time"
"github.com/shopspring/decimal"
)
// The different operator types available.
const (
operatorTypeString = "string"
operatorTypeNumber = "number"
operatorTypeDateime = "datetime"
operatorTypeArray = "array"
operatorTypeBoolean = "boolean"
)
// Define methods distiller should implement.
type distiller interface {
Filter(map[string]interface{}, *Filter) error
Result() []map[string]interface{}
}
// validateDataType ensures filter value passed has correct datatype for the
// filters operator.
func validateDataType(filter *Filter) error {
// Retrieve value.
datum := filter.Value
// Handle all our operator types.
switch filter.Operator.Type {
case operatorTypeString:
_, err := castToString(datum)
if err != nil {
return fmt.Errorf("Invalid datatype. Expected string datatype, got %T for `%v`", datum, filter.Field)
}
case operatorTypeDateime:
_, err := castToDatetime(datum)
if err != nil {
return fmt.Errorf("Invalid datatype. Expected datetime datatype, got %T for `%v`", datum, filter.Field)
}
case operatorTypeNumber:
_, err := castToNumber(datum)
if err != nil {
return fmt.Errorf("Invalid datatype. Expected number datatype, got %T for `%v`", datum, filter.Field)
}
case operatorTypeArray:
_, err := castToSlice(datum)
if err != nil {
return fmt.Errorf("Invalid datatype. Expected array datatype, got %T for `%v` %v", datum, filter.Field, err)
}
case operatorTypeBoolean:
_, err := castToBoolean(datum)
if err != nil {
return fmt.Errorf("Invalid datatype. Expected boolean datatype, got %T for `%v` %v", datum, filter.Field, err)
}
default:
return fmt.Errorf("Unknown field type: %s", filter.Operator.Type)
}
return nil
}
// castToDatetime attempts to cast given interface{} to a string.
func castToString(datum interface{}) (*string, error) {
str, ok := datum.(string)
if !ok {
return nil, fmt.Errorf("Invalid datatype. Expected string datatype, got %T", datum)
}
return &str, nil
}
// castToNumber attempts to cast given interface{} to decimal.Decimal.
func castToNumber(datum interface{}) (*decimal.Decimal, error) {
var err error
var d decimal.Decimal
// Handle different float/integer datatypes, casting to float64.
switch datumTyped := datum.(type) {
case int:
d = decimal.NewFromFloat(float64(datumTyped))
case int32:
d = decimal.NewFromFloat(float64(datumTyped))
case int64:
d = decimal.NewFromFloat(float64(datumTyped))
case float32:
d = decimal.NewFromFloat(float64(datumTyped))
case float64:
d = decimal.NewFromFloat(datumTyped)
default:
return nil, fmt.Errorf("Invalid datatype. Expected number datatype, got %T", datum)
}
return &d, err
}
// castToDatetime attempts to cast given interface{} to a datetime.
func castToDatetime(datum interface{}) (*time.Time, error) {
var t *time.Time
// Handle the different time.Time possibities, casting to *time.Time.
switch datumTyped := datum.(type) {
case time.Time:
t = &datumTyped
case *time.Time:
if datumTyped == nil {
return nil, errors.New("Invalid datatype. Received nil *time.Time for datetime field")
}
t = datumTyped
case string:
parsed, err := time.Parse(time.RFC3339, datumTyped)
if err != nil {
return nil, errors.New("Invalid datatype. Invalid date string passed for datetime field. RFC3339 datetime string required.")
}
t = &parsed
default:
return nil, fmt.Errorf("Invalid datatype. Expected string or time.Time, got %T", datum)
}
return t, nil
}
// castToSlice attempts to cast given interface{} to []string{}.
func castToSlice(datum interface{}) ([]*string, error) {
var err error
var results []*string
// Handle different float/integer datatypes, casting to float64.
switch reflect.TypeOf(datum).Kind() {
case reflect.Slice:
s := reflect.ValueOf(datum)
for i := 0; i < s.Len(); i++ {
str, e := castToString(s.Index(i).String())
if e != nil {
return nil, e
}
results = append(results, str)
}
default:
return nil, fmt.Errorf("Invalid datatype. Expected array datatype, got %T", datum)
}
return results, err
}
// castToBoolean attempts to cast given interface{} to a boolean.
func castToBoolean(datum interface{}) (*bool, error) {
val, ok := datum.(bool)
if !ok {
return nil, fmt.Errorf("Invalid datatype. Expected boolean datatype, got %T", datum)
}
return &val, nil
}