-
Notifications
You must be signed in to change notification settings - Fork 20
/
field.go
366 lines (316 loc) · 9.41 KB
/
field.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package flows
import (
"fmt"
"sort"
"strings"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/excellent/types"
"github.com/nyaruka/goflow/utils"
"github.com/pkg/errors"
)
// Field represents a contact field
type Field struct {
assets.Field
}
// NewField creates a new field from the given asset
func NewField(asset assets.Field) *Field {
return &Field{Field: asset}
}
// Asset returns the underlying asset
func (f *Field) Asset() assets.Field { return f.Field }
// Reference returns a reference to this field
func (f *Field) Reference() *assets.FieldReference {
if f == nil {
return nil
}
return assets.NewFieldReference(f.Key(), f.Name())
}
// Value represents a value in each of the field types
type Value struct {
Text types.XText `json:"text" validate:"required"`
Datetime *types.XDateTime `json:"datetime,omitempty"`
Number *types.XNumber `json:"number,omitempty"`
State utils.LocationPath `json:"state,omitempty"`
District utils.LocationPath `json:"district,omitempty"`
Ward utils.LocationPath `json:"ward,omitempty"`
}
// NewValue creates an empty value
func NewValue(text types.XText, datetime *types.XDateTime, number *types.XNumber, state utils.LocationPath, district utils.LocationPath, ward utils.LocationPath) *Value {
return &Value{
Text: text,
Datetime: datetime,
Number: number,
State: state,
District: district,
Ward: ward,
}
}
// Equals determines whether two values are equal
func (v *Value) Equals(o *Value) bool {
if v == nil && o == nil {
return true
}
if (v == nil && o != nil) || (v != nil && o == nil) {
return false
}
dateEqual := (v.Datetime == nil && o.Datetime == nil) || (v.Datetime != nil && o.Datetime != nil && v.Datetime.Equals(*o.Datetime))
numEqual := (v.Number == nil && o.Number == nil) || (v.Number != nil && o.Number != nil && v.Number.Equals(*o.Number))
return v.Text.Equals(o.Text) && dateEqual && numEqual && v.State == o.State && v.District == o.District && v.Ward == o.Ward
}
// FieldValue represents a field and a set of values for that field
type FieldValue struct {
field *Field
*Value
}
// NewFieldValue creates a new field value
func NewFieldValue(field *Field, value *Value) *FieldValue {
return &FieldValue{field: field, Value: value}
}
// ToXValue returns a representation of this object for use in expressions
func (v *FieldValue) ToXValue(env utils.Environment) types.XValue {
// the typed value of no value is nil
if v == nil {
return nil
}
switch v.field.Type() {
case assets.FieldTypeText:
return v.Text
case assets.FieldTypeDatetime:
if v.Datetime != nil {
return *v.Datetime
}
case assets.FieldTypeNumber:
if v.Number != nil {
return *v.Number
}
case assets.FieldTypeState:
if v.State != "" {
return types.NewXText(string(v.State))
}
case assets.FieldTypeDistrict:
if v.District != "" {
return types.NewXText(string(v.District))
}
case assets.FieldTypeWard:
if v.Ward != "" {
return types.NewXText(string(v.Ward))
}
}
return nil
}
// QueryValue returns the value for use in contact queries
func (v *FieldValue) QueryValue() interface{} {
// the typed value of no value is nil
if v == nil {
return nil
}
switch v.field.Type() {
case assets.FieldTypeText:
return v.Text.Native()
case assets.FieldTypeDatetime:
if v.Datetime != nil {
return (*v.Datetime).Native()
}
case assets.FieldTypeNumber:
if v.Number != nil {
return (*v.Number).Native()
}
// we only search against location names and not full paths
case assets.FieldTypeState:
if v.State != "" {
return v.State.Name()
}
case assets.FieldTypeDistrict:
if v.District != "" {
return v.District.Name()
}
case assets.FieldTypeWard:
if v.Ward != "" {
return v.Ward.Name()
}
}
return nil
}
// FieldValues is the set of all field values for a contact
type FieldValues map[string]*FieldValue
// NewFieldValues creates a new field value map
func NewFieldValues(a SessionAssets, values map[string]*Value, missing assets.MissingCallback) (FieldValues, error) {
allFields := a.Fields().All()
fieldValues := make(FieldValues, len(allFields))
for _, field := range allFields {
value := values[field.Key()]
if value != nil {
if value.Text.Empty() {
return nil, errors.Errorf("field values can't be empty")
}
fieldValues[field.Key()] = NewFieldValue(field, value)
} else {
fieldValues[field.Key()] = nil
}
}
// log any unmatched field keys as missing assets
for key := range values {
_, valid := fieldValues[key]
if !valid {
missing(assets.NewFieldReference(key, ""))
}
}
return fieldValues, nil
}
// 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
}
// Get gets the value set for the given field
func (f FieldValues) Get(field *Field) *Value {
fieldVal := f[field.Key()]
if fieldVal != nil {
return fieldVal.Value
}
return nil
}
// Set sets the value for the given field (can be null to clear it)
func (f FieldValues) Set(field *Field, value *Value) {
if value == nil {
f.Clear(field)
} else {
fieldValue := NewFieldValue(field, value)
f[field.Key()] = fieldValue
}
}
// Clear clears the value set for the given field
func (f FieldValues) Clear(field *Field) {
delete(f, field.Key())
}
// Parse parses a raw string field value into the different possible types
func (f FieldValues) Parse(env utils.Environment, fields *FieldAssets, field *Field, rawValue string) *Value {
if rawValue == "" {
return nil
}
runEnv := env.(RunEnvironment)
var asText = types.NewXText(rawValue)
var asDateTime *types.XDateTime
var asNumber *types.XNumber
if parsedNumber, xerr := types.ToXNumber(env, asText); xerr == nil {
asNumber = &parsedNumber
}
if parsedDate, xerr := types.ToXDateTimeWithTimeFill(env, asText); xerr == nil {
asDateTime = &parsedDate
}
var asLocation *utils.Location
// for locations, if it has a '>' then it is explicit, look it up that way
if utils.IsPossibleLocationPath(rawValue) {
asLocation, _ = runEnv.LookupLocation(utils.LocationPath(rawValue))
} else {
var matchingLocations []*utils.Location
if field.Type() == assets.FieldTypeWard {
parent := f.getFirstLocationValue(runEnv, fields, assets.FieldTypeDistrict)
if parent != nil {
matchingLocations, _ = runEnv.FindLocationsFuzzy(rawValue, LocationLevelWard, parent)
}
} else if field.Type() == assets.FieldTypeDistrict {
parent := f.getFirstLocationValue(runEnv, fields, assets.FieldTypeState)
if parent != nil {
matchingLocations, _ = runEnv.FindLocationsFuzzy(rawValue, LocationLevelDistrict, parent)
}
} else if field.Type() == assets.FieldTypeState {
matchingLocations, _ = runEnv.FindLocationsFuzzy(rawValue, LocationLevelState, nil)
}
if len(matchingLocations) > 0 {
asLocation = matchingLocations[0]
}
}
var asState, asDistrict, asWard utils.LocationPath
if asLocation != nil {
switch asLocation.Level() {
case LocationLevelState:
asState = utils.LocationPath(asLocation.Path())
case LocationLevelDistrict:
asState = utils.LocationPath(asLocation.Parent().Path())
asDistrict = utils.LocationPath(asLocation.Path())
case LocationLevelWard:
asState = utils.LocationPath(asLocation.Parent().Parent().Path())
asDistrict = utils.LocationPath(asLocation.Parent().Path())
asWard = utils.LocationPath(asLocation.Path())
}
}
return &Value{
Text: asText,
Datetime: asDateTime,
Number: asNumber,
State: asState,
District: asDistrict,
Ward: asWard,
}
}
// Context returns the properties available in expressions
func (f FieldValues) Context(env utils.Environment) map[string]types.XValue {
entries := make(map[string]types.XValue, len(f)+1)
lines := make([]string, 0, len(f))
for k, v := range f {
val := v.ToXValue(env)
entries[string(k)] = val
if !utils.IsNil(val) {
lines = append(lines, fmt.Sprintf("%s: %s", v.field.Name(), types.Render(val)))
}
}
sort.Strings(lines)
entries["__default__"] = types.NewXText(strings.Join(lines, "\n"))
return entries
}
func (f FieldValues) getFirstLocationValue(env RunEnvironment, fields *FieldAssets, valueType assets.FieldType) *utils.Location {
// do we have a field of this type?
field := fields.FirstOfType(valueType)
if field == nil {
return nil
}
// does this contact have a value for that field?
value := f[field.Key()].ToXValue(env)
if value == nil {
return nil
}
location, err := env.LookupLocation(utils.LocationPath(value.(types.XText).Native()))
if err != nil {
return nil
}
return location
}
// FieldAssets provides access to all field assets
type FieldAssets struct {
all []*Field
byKey map[string]*Field
}
// NewFieldAssets creates a new set of field assets
func NewFieldAssets(fields []assets.Field) *FieldAssets {
s := &FieldAssets{
all: make([]*Field, len(fields)),
byKey: make(map[string]*Field, len(fields)),
}
for i, asset := range fields {
field := NewField(asset)
s.all[i] = field
s.byKey[field.Key()] = field
}
return s
}
// Get returns the contact field with the given key
func (s *FieldAssets) Get(key string) *Field {
return s.byKey[key]
}
// All returns all the fields in this set
func (s *FieldAssets) All() []*Field {
return s.all
}
// FirstOfType returns the first field in this set with the given value type
func (s *FieldAssets) FirstOfType(valueType assets.FieldType) *Field {
for _, field := range s.all {
if field.Type() == valueType {
return field
}
}
return nil
}