forked from nyaruka/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.go
229 lines (190 loc) · 5.83 KB
/
fields.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
package flows
import (
"encoding/json"
"github.com/nyaruka/goflow/excellent/types"
"github.com/nyaruka/goflow/utils"
)
// FieldKey is the unique key for this field
type FieldKey string
// FieldValueType is the data type of values for each field
type FieldValueType string
// field value types
const (
FieldValueTypeText FieldValueType = "text"
FieldValueTypeNumber FieldValueType = "number"
FieldValueTypeDatetime FieldValueType = "datetime"
FieldValueTypeWard FieldValueType = "ward"
FieldValueTypeDistrict FieldValueType = "district"
FieldValueTypeState FieldValueType = "state"
)
var fieldLocationLevels = map[FieldValueType]LocationLevel{
FieldValueTypeState: LocationLevel(1),
FieldValueTypeDistrict: LocationLevel(2),
FieldValueTypeWard: LocationLevel(3),
}
// Field represents a contact field
type Field struct {
key FieldKey
name string
valueType FieldValueType
}
// NewField returns a new field object with the passed in uuid, key and value type
func NewField(key FieldKey, name string, valueType FieldValueType) *Field {
return &Field{key: key, name: name, valueType: valueType}
}
// Key returns the key of the field
func (f *Field) Key() FieldKey { return f.key }
// FieldValue represents a contact's value for a specific field
type FieldValue struct {
field *Field
text types.XText
datetime *types.XDateTime
number *types.XNumber
state *Location
district *Location
ward *Location
}
func (v *FieldValue) IsEmpty() bool {
return !(!v.text.Empty() || v.datetime != nil || v.number != nil || v.state != nil || v.district != nil || v.ward != nil)
}
func (v *FieldValue) TypedValue() types.XValue {
switch v.field.valueType {
case FieldValueTypeText:
return v.text
case FieldValueTypeDatetime:
if v.datetime != nil {
return *v.datetime
}
case FieldValueTypeNumber:
if v.number != nil {
return *v.number
}
case FieldValueTypeState:
return v.state
case FieldValueTypeDistrict:
return v.district
case FieldValueTypeWard:
return v.ward
}
return nil
}
// Resolve resolves the given key when this field value is referenced in an expression
func (v *FieldValue) Resolve(key string) types.XValue {
switch key {
case "text":
return v.text
}
return types.NewXResolveError(v, key)
}
// Reduce is called when this object needs to be reduced to a primitive
func (v *FieldValue) Reduce() types.XPrimitive {
return v.TypedValue().Reduce()
}
// ToXJSON is called when this type is passed to @(json(...))
func (v *FieldValue) ToXJSON() types.XText { return v.Reduce().ToXJSON() }
var _ types.XValue = (*FieldValue)(nil)
var _ types.XResolvable = (*FieldValue)(nil)
// FieldValues is the set of all field values for a contact
type FieldValues map[FieldKey]*FieldValue
// Clone returns a clone of this set of field values
func (f FieldValues) clone() FieldValues {
clone := make(FieldValues, len(f))
for k, v := range f {
clone[k] = v
}
return clone
}
func (f FieldValues) setValue(env utils.Environment, field *Field, rawValue types.XText) {
var asDate *types.XDateTime
var asNumber *types.XNumber
if parsedNumber, xerr := types.ToXNumber(rawValue); xerr == nil {
asNumber = &parsedNumber
}
if parsedDate, xerr := types.ToXDate(env, rawValue); xerr == nil {
asDate = &parsedDate
}
// TODO parse as locations
f[field.key] = &FieldValue{
field: field,
text: rawValue,
datetime: asDate,
number: asNumber,
}
}
// Length is called to get the length of this object
func (f FieldValues) Length() int {
return len(f)
}
// Resolve resolves the given key when this set of field values is referenced in an expression
func (f FieldValues) Resolve(key string) types.XValue {
val, exists := f[FieldKey(key)]
if !exists {
return types.NewXResolveError(f, key)
}
return val
}
// Reduce is called when this object needs to be reduced to a primitive
func (f FieldValues) Reduce() types.XPrimitive {
values := types.NewXEmptyMap()
for k, v := range f {
values.Put(string(k), v)
}
return values
}
// ToXJSON is called when this type is passed to @(json(...))
func (f FieldValues) ToXJSON() types.XText {
return f.Reduce().ToXJSON()
}
var _ types.XValue = (FieldValues)(nil)
var _ types.XLengthable = (FieldValues)(nil)
var _ types.XResolvable = (FieldValues)(nil)
// FieldSet defines the unordered set of all fields for a session
type FieldSet struct {
fields []*Field
fieldsByKey map[FieldKey]*Field
}
// NewFieldSet creates a new set of fields
func NewFieldSet(fields []*Field) *FieldSet {
s := &FieldSet{fields: fields, fieldsByKey: make(map[FieldKey]*Field, len(fields))}
for _, field := range s.fields {
s.fieldsByKey[field.key] = field
}
return s
}
// FindByKey finds the contact field with the given key
func (s *FieldSet) FindByKey(key FieldKey) *Field {
return s.fieldsByKey[key]
}
func (s *FieldSet) All() []*Field {
return s.fields
}
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type fieldEnvelope struct {
Key FieldKey `json:"key"`
Name string `json:"name"`
ValueType FieldValueType `json:"value_type,omitempty"`
}
// ReadField reads a contact field from the given JSON
func ReadField(data json.RawMessage) (*Field, error) {
var fe fieldEnvelope
if err := utils.UnmarshalAndValidate(data, &fe, "field"); err != nil {
return nil, err
}
return NewField(fe.Key, fe.Name, fe.ValueType), nil
}
// ReadFieldSet reads a set of contact fields from the given JSON
func ReadFieldSet(data json.RawMessage) (*FieldSet, error) {
items, err := utils.UnmarshalArray(data)
if err != nil {
return nil, err
}
fields := make([]*Field, len(items))
for d := range items {
if fields[d], err = ReadField(items[d]); err != nil {
return nil, err
}
}
return NewFieldSet(fields), nil
}