-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.go
237 lines (208 loc) · 5.81 KB
/
cell.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package aggro
import (
"errors"
"fmt"
"time"
"github.com/shopspring/decimal"
)
const (
fieldTypeString = "string"
fieldTypeNumber = "number"
fieldTypeDatetime = "datetime"
fieldTypeBoolean = "boolean"
)
// newCell constructs a Cell{} based on the given *Field.Type. Data being assigned to the *Field
// must be of the same datatype as *Field.Type.
func newCell(data, datum interface{}, field *Field) (Cell, error) {
var cell Cell
switch field.Type {
case fieldTypeString:
stringValue, ok := datum.(string)
if !ok {
return nil, fmt.Errorf("Expected string datatype, got %T", datum)
}
cell = &StringCell{
value: stringValue,
field: field,
data: data,
}
case fieldTypeDatetime:
datetimeCell := &DatetimeCell{
field: field,
data: data,
}
switch datumTyped := datum.(type) {
case time.Time:
datetimeCell.value = &datumTyped
case *time.Time:
if datumTyped == nil {
return nil, errors.New("Got nil *time.Time for datetime field")
}
datetimeCell.value = datumTyped
case string:
t, err := time.Parse(time.RFC3339, datumTyped)
if err != nil {
return nil, errors.New("Invalid date string passed for datetime field. RFC3339 datetime string required")
}
datetimeCell.value = &t
default:
return nil, fmt.Errorf("Expected string or time.Time, got %T", datum)
}
cell = datetimeCell
case fieldTypeNumber:
numberCell := &NumberCell{
field: field,
data: data,
}
var d decimal.Decimal
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("Expected number, got %T", datum)
}
numberCell.value = &d
cell = numberCell
case fieldTypeBoolean:
boolValue, ok := datum.(bool)
if !ok {
return nil, fmt.Errorf("Expected boolean datatype, got %T", datum)
}
booleanCell := &BooleanCell{
value: boolValue,
field: field,
data: data,
}
cell = booleanCell
default:
return nil, fmt.Errorf("Unknown field type: %s", field.Type)
}
return cell, nil
}
// Cell represents data and configuration for each of our *Table.Fields.
type Cell interface {
FieldDefinition() *Field
IsMetricable(m measurer) bool
MeasurableCell() MeasurableCell
}
// MeasurableCell returns the cells MeasurableCell{}.
type MeasurableCell interface {
Value() interface{}
}
// NumberCell implements the Cell interface.
type NumberCell struct {
value *decimal.Decimal
field *Field
data interface{}
}
// FieldDefinition returns the field definition (name) representing the cell.
func (cell *NumberCell) FieldDefinition() *Field {
return cell.field
}
// IsMetricable determines whether the measurer{} provided can be run by the cell.
func (cell *NumberCell) IsMetricable(m measurer) bool {
return true
}
// Value returns the cell value.
func (cell *NumberCell) Value() interface{} {
return cell.value
}
// MeasurableCell returns the cells MeasurableCell{}.
func (cell *NumberCell) MeasurableCell() MeasurableCell {
return cell
}
// ValueForPeriod returns the start of a given period.
func (cell *NumberCell) ValueForPeriod(period []interface{}) (string, error) {
value, err := rangeValueForPeriod(cell.value, period)
return value.String(), err
}
// DatetimeCell implements the Cell interface.
type DatetimeCell struct {
value *time.Time
field *Field
data interface{}
}
// FieldDefinition returns the field definition (name) representing the cell.
func (cell *DatetimeCell) FieldDefinition() *Field {
return cell.field
}
// IsMetricable determines whether the measurer{} provided can be run by the cell.
func (cell *DatetimeCell) IsMetricable(m measurer) bool {
return false
}
// Value returns the cell value.
func (cell *DatetimeCell) Value() interface{} {
return cell.value
}
// MeasurableCell returns the cells MeasurableCell{}.
func (cell *DatetimeCell) MeasurableCell() MeasurableCell {
return nil
}
// ValueForPeriod returns the start of a given period.
func (cell *DatetimeCell) ValueForPeriod(period DatetimePeriod, location *time.Location) (string, error) {
return datetimeValueForPeriod(cell.value, period, location)
}
// StringCell implements the Cell{} interface.
type StringCell struct {
value string
field *Field
data interface{}
}
// FieldDefinition returns the field definition (name) representing the cell.
func (cell *StringCell) FieldDefinition() *Field {
return cell.field
}
// IsMetricable determines whether the measurer{} provided can be run by the cell.
func (cell *StringCell) IsMetricable(m measurer) bool {
// We allow certain metrics to run on StringCells.
switch m.(type) {
case *cardinality, *valueCount:
return true
default:
return false
}
}
// Value returns the cell value.
func (cell *StringCell) Value() interface{} {
return cell.value
}
// MeasurableCell returns the cells MeasurableCell{}.
func (cell *StringCell) MeasurableCell() MeasurableCell {
return cell
}
// BooleanCell implements the Cell{} interface.
type BooleanCell struct {
value bool
field *Field
data interface{}
}
// FieldDefinition returns the field definition (name) representing the cell.
func (cell *BooleanCell) FieldDefinition() *Field {
return cell.field
}
// IsMetricable determines whether the measurer{} provided can be run by the cell.
func (cell *BooleanCell) IsMetricable(m measurer) bool {
// We allow certain metrics to run on BooleanCells.
switch m.(type) {
case *valueCount:
return true
default:
return false
}
}
// Value returns the cell value.
func (cell *BooleanCell) Value() interface{} {
return cell.value
}
// MeasurableCell returns the cells MeasurableCell{}.
func (cell *BooleanCell) MeasurableCell() MeasurableCell {
return cell
}