-
Notifications
You must be signed in to change notification settings - Fork 20
/
field.go
363 lines (313 loc) · 9.36 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
package flows
import (
"fmt"
"sort"
"strings"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/excellent/types"
"github.com/nyaruka/goflow/utils"
)
// 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 envs.LocationPath `json:"state,omitempty"`
District envs.LocationPath `json:"district,omitempty"`
Ward envs.LocationPath `json:"ward,omitempty"`
}
// NewValue creates an empty value
func NewValue(text types.XText, datetime *types.XDateTime, number *types.XNumber, state envs.LocationPath, district envs.LocationPath, ward envs.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 envs.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 {
allFields := a.Fields().All()
fieldValues := make(FieldValues, len(allFields))
for _, field := range allFields {
value := values[field.Key()]
fieldValues.Set(field, value)
}
// log any unmatched field keys as missing assets
for key := range values {
_, valid := fieldValues[key]
if !valid {
missing(assets.NewFieldReference(key, ""), nil)
}
}
return fieldValues
}
// 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) {
var fv *FieldValue
if value != nil && !value.Text.Empty() {
fv = NewFieldValue(field, value)
}
f[field.Key()] = fv
}
// Parse parses a raw string field value into the different possible types
func (f FieldValues) Parse(env envs.Environment, fields *FieldAssets, field *Field, rawValue string) *Value {
if rawValue == "" {
return nil
}
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 *envs.Location
// to do location parsing we need an environment with location support
locations := env.LocationResolver()
if locations != nil {
// for locations, if it has a '>' then it is explicit, look it up that way
if envs.IsPossibleLocationPath(rawValue) {
asLocation = locations.LookupLocation(envs.LocationPath(rawValue))
} else {
var matchingLocations []*envs.Location
if field.Type() == assets.FieldTypeWard {
parent := f.getFirstLocationValue(env, fields, assets.FieldTypeDistrict)
if parent != nil {
matchingLocations = locations.FindLocationsFuzzy(rawValue, LocationLevelWard, parent)
}
} else if field.Type() == assets.FieldTypeDistrict {
parent := f.getFirstLocationValue(env, fields, assets.FieldTypeState)
if parent != nil {
matchingLocations = locations.FindLocationsFuzzy(rawValue, LocationLevelDistrict, parent)
}
} else if field.Type() == assets.FieldTypeState {
matchingLocations = locations.FindLocationsFuzzy(rawValue, LocationLevelState, nil)
}
if len(matchingLocations) > 0 {
asLocation = matchingLocations[0]
}
}
}
var asState, asDistrict, asWard envs.LocationPath
if asLocation != nil {
switch asLocation.Level() {
case LocationLevelState:
asState = envs.LocationPath(asLocation.Path())
case LocationLevelDistrict:
asState = envs.LocationPath(asLocation.Parent().Path())
asDistrict = envs.LocationPath(asLocation.Path())
case LocationLevelWard:
asState = envs.LocationPath(asLocation.Parent().Parent().Path())
asDistrict = envs.LocationPath(asLocation.Parent().Path())
asWard = envs.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 envs.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 envs.Environment, fields *FieldAssets, valueType assets.FieldType) *envs.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
}
return env.LocationResolver().LookupLocation(envs.LocationPath(value.(types.XText).Native()))
}
// 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
}
func (s *FieldAssets) ResolveField(key string) assets.Field {
f := s.byKey[key]
if f != nil {
return f
}
return nil
}
func (s *FieldAssets) ResolveGroup(name string) assets.Group {
return nil
}