-
Notifications
You must be signed in to change notification settings - Fork 19
/
flow.go
371 lines (313 loc) · 11.2 KB
/
flow.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
367
368
369
370
371
package definition
import (
"encoding/json"
"fmt"
"strings"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/excellent/types"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils"
"github.com/Masterminds/semver"
"github.com/pkg/errors"
)
// CurrentSpecVersion is the flow spec version supported by this library
var CurrentSpecVersion = semver.MustParse("13.0")
type flow struct {
// spec properties
uuid assets.FlowUUID
name string
specVersion *semver.Version
language utils.Language
flowType flows.FlowType
revision int
expireAfterMinutes int
localization flows.Localization
nodes []flows.Node
// optional properties not used by engine itself
ui json.RawMessage
// properties set after validation
dependencies *dependencies
results []*flows.ResultSpec
waitingExits []flows.ExitUUID
// internal state
nodeMap map[flows.NodeUUID]flows.Node
validated bool
}
// NewFlow creates a new flow
func NewFlow(uuid assets.FlowUUID, name string, language utils.Language, flowType flows.FlowType, revision int, expireAfterMinutes int, localization flows.Localization, nodes []flows.Node, ui json.RawMessage) flows.Flow {
f := &flow{
uuid: uuid,
name: name,
specVersion: CurrentSpecVersion,
language: language,
flowType: flowType,
revision: revision,
expireAfterMinutes: expireAfterMinutes,
localization: localization,
nodes: nodes,
nodeMap: make(map[flows.NodeUUID]flows.Node, len(nodes)),
ui: ui,
}
for _, node := range f.nodes {
f.nodeMap[node.UUID()] = node
}
return f
}
func (f *flow) UUID() assets.FlowUUID { return f.uuid }
func (f *flow) Name() string { return f.name }
func (f *flow) SpecVersion() *semver.Version { return f.specVersion }
func (f *flow) Revision() int { return f.revision }
func (f *flow) Language() utils.Language { return f.language }
func (f *flow) Type() flows.FlowType { return f.flowType }
func (f *flow) ExpireAfterMinutes() int { return f.expireAfterMinutes }
func (f *flow) Nodes() []flows.Node { return f.nodes }
func (f *flow) Localization() flows.Localization { return f.localization }
func (f *flow) UI() json.RawMessage { return f.ui }
func (f *flow) GetNode(uuid flows.NodeUUID) flows.Node { return f.nodeMap[uuid] }
// Validates that we are structurally currect. The SessionAssets `sa` is optional but if provided,
// we will also check that all dependencies actually exist, and refresh their names.
func (f *flow) Validate(sa flows.SessionAssets) error {
return f.validate(sa, false, nil)
}
// Validates that we are structurally currect, have all the dependencies we need, and all our flow dependencies are also valid
func (f *flow) ValidateRecursively(sa flows.SessionAssets, missing func(assets.Reference)) error {
return f.validate(sa, true, missing)
}
func (f *flow) validate(sa flows.SessionAssets, recursive bool, missing func(assets.Reference)) error {
// if this flow has already been validated, don't need to do it again - avoid unnecessary work
// but also prevents looping if recursively validating flows
if f.validated {
return nil
}
// track UUIDs used by nodes and actions to ensure that they are unique
seenUUIDs := make(map[utils.UUID]bool)
for _, node := range f.nodes {
uuidAlreadySeen := seenUUIDs[utils.UUID(node.UUID())]
if uuidAlreadySeen {
return errors.Errorf("node UUID %s isn't unique", node.UUID())
}
seenUUIDs[utils.UUID(node.UUID())] = true
if err := node.Validate(f, seenUUIDs); err != nil {
return errors.Wrapf(err, "validation failed for node[uuid=%s]", node.UUID())
}
}
// extract all dependencies (assets, contacts)
deps := newDependencies(f.ExtractDependencies())
// and validate that all assets are available in the session assets
if sa != nil {
missingAssets := make([]assets.Reference, 0)
deps.refresh(sa, func(r assets.Reference) { missingAssets = append(missingAssets, r) })
if len(missingAssets) > 0 {
// if we have callback for missing dependencies, call that
if missing != nil {
for _, dep := range missingAssets {
missing(dep)
}
} else {
// otherwise error
depStrings := make([]string, len(missingAssets))
for d := range missingAssets {
depStrings[d] = missingAssets[d].String()
}
return errors.Errorf("missing dependencies: %s", strings.Join(depStrings, ","))
}
}
}
f.validated = true
f.dependencies = deps
f.results = f.ExtractResults()
f.waitingExits = f.ExtractExitsFromWaits()
if recursive {
if sa == nil {
panic("can't do recursive flow validation without session assets")
}
// go validate any non-missing flow dependencies
for _, flowRef := range deps.Flows {
flowDep, err := sa.Flows().Get(flowRef.UUID)
if err == nil {
flowDep.(*flow).validate(sa, true, missing)
}
}
}
return nil
}
// Context returns the properties available in expressions
func (f *flow) Context(env utils.Environment) map[string]types.XValue {
return map[string]types.XValue{
"__default__": types.NewXText(f.name),
"uuid": types.NewXText(string(f.UUID())),
"name": types.NewXText(f.name),
"revision": types.NewXNumberFromInt(f.revision),
}
}
// Reference returns a reference to this flow asset
func (f *flow) Reference() *assets.FlowReference {
if f == nil {
return nil
}
return assets.NewFlowReference(f.uuid, f.name)
}
func (f *flow) inspect(inspect func(flows.Inspectable)) {
// inspect each node
for _, n := range f.Nodes() {
n.Inspect(inspect)
}
}
// ExtractTemplates extracts all non-empty templates
func (f *flow) ExtractTemplates() []string {
templates := make([]string, 0)
include := flows.NewTemplateEnumerator(f.Localization(), func(template string) {
if template != "" {
templates = append(templates, template)
}
})
f.inspect(func(item flows.Inspectable) {
item.EnumerateTemplates(include)
})
return templates
}
// RewriteTemplates rewrites all templates
func (f *flow) RewriteTemplates(rewrite func(string) string) {
f.inspect(func(item flows.Inspectable) {
item.EnumerateTemplates(flows.NewTemplateRewriter(f.Localization(), rewrite))
})
}
// ExtractDependencies extracts all asset dependencies
func (f *flow) ExtractDependencies() []assets.Reference {
dependencies := make([]assets.Reference, 0)
dependenciesSeen := make(map[string]bool)
addDependency := func(r assets.Reference) {
if !utils.IsNil(r) && !r.Variable() {
key := fmt.Sprintf("%s:%s", r.Type(), r.Identity())
if !dependenciesSeen[key] {
dependencies = append(dependencies, r)
dependenciesSeen[key] = true
}
}
}
include := flows.NewTemplateEnumerator(f.Localization(), func(template string) {
fieldRefs := flows.ExtractFieldReferences(template)
for _, f := range fieldRefs {
addDependency(f)
}
})
f.inspect(func(item flows.Inspectable) {
item.EnumerateTemplates(include)
item.EnumerateDependencies(f.Localization(), func(r assets.Reference) {
addDependency(r)
})
})
return dependencies
}
// ExtractResultNames extracts all result names
func (f *flow) ExtractResults() []*flows.ResultSpec {
specs := make([]*flows.ResultSpec, 0)
f.inspect(func(item flows.Inspectable) {
item.EnumerateResults(func(spec *flows.ResultSpec) {
specs = append(specs, spec)
})
})
return flows.MergeResultSpecs(specs)
}
// ExtractExitsFromWaits extracts all exits coming from nodes with waits
func (f *flow) ExtractExitsFromWaits() []flows.ExitUUID {
exitUUIDs := make([]flows.ExitUUID, 0)
include := func(e flows.ExitUUID) { exitUUIDs = append(exitUUIDs, e) }
for _, n := range f.nodes {
if n.Router() != nil && n.Router().Wait() != nil {
for _, e := range n.Exits() {
include(e.UUID())
}
}
}
return exitUUIDs
}
var _ flows.Flow = (*flow)(nil)
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
func init() {
utils.Validator.RegisterAlias("flow_type", "eq=messaging|eq=messaging_offline|eq=voice")
}
// the set of fields common to all new flow spec versions
type flowHeader struct {
UUID assets.FlowUUID `json:"uuid" validate:"required,uuid4"`
Name string `json:"name"`
SpecVersion *semver.Version `json:"spec_version" validate:"required"`
}
type flowEnvelope struct {
flowHeader
Language utils.Language `json:"language" validate:"required"`
Type flows.FlowType `json:"type" validate:"required,flow_type"`
Revision int `json:"revision"`
ExpireAfterMinutes int `json:"expire_after_minutes"`
Localization localization `json:"localization"`
Nodes []*node `json:"nodes"`
UI json.RawMessage `json:"_ui,omitempty"`
}
// additional properties that a validated flow can have
type validatedFlowEnvelope struct {
*flowEnvelope
Dependencies *dependencies `json:"_dependencies"`
Results []*flows.ResultSpec `json:"_results"`
WaitingExits []flows.ExitUUID `json:"_waiting_exits"`
}
// IsSpecVersionSupported determines if we can read the given flow version
func IsSpecVersionSupported(ver *semver.Version) bool {
// major versions change flow schema
// we currently have no support for migrations but that will change in future
return ver.Major() == CurrentSpecVersion.Major()
}
// ReadFlow reads a single flow definition from the passed in byte array
func ReadFlow(data json.RawMessage) (flows.Flow, error) {
header := &flowHeader{}
if err := utils.UnmarshalAndValidate(data, header); err != nil {
return nil, errors.Wrap(err, "unable to read flow header")
}
// can't do anything with a newer major version than this library supports
if !IsSpecVersionSupported(header.SpecVersion) {
return nil, errors.Errorf("spec version %s is newer than this library (%s)", header.SpecVersion, CurrentSpecVersion)
}
// TODO flow spec migrations
e := &flowEnvelope{}
if err := utils.UnmarshalAndValidate(data, e); err != nil {
return nil, err
}
nodes := make([]flows.Node, len(e.Nodes))
for n := range e.Nodes {
nodes[n] = e.Nodes[n]
}
if e.Localization == nil {
e.Localization = make(localization)
}
return NewFlow(e.UUID, e.Name, e.Language, e.Type, e.Revision, e.ExpireAfterMinutes, e.Localization, nodes, e.UI), nil
}
// MarshalJSON marshals this flow into JSON
func (f *flow) MarshalJSON() ([]byte, error) {
e := &flowEnvelope{
flowHeader: flowHeader{
UUID: f.uuid,
Name: f.name,
SpecVersion: f.specVersion,
},
Language: f.language,
Type: f.flowType,
Revision: f.revision,
ExpireAfterMinutes: f.expireAfterMinutes,
Localization: f.localization.(localization),
Nodes: make([]*node, len(f.nodes)),
UI: f.ui,
}
for i := range f.nodes {
e.Nodes[i] = f.nodes[i].(*node)
}
if f.validated {
return json.Marshal(&validatedFlowEnvelope{
flowEnvelope: e,
Dependencies: f.dependencies,
Results: f.results,
WaitingExits: f.waitingExits,
})
}
return json.Marshal(e)
}