-
Notifications
You must be signed in to change notification settings - Fork 20
/
dependencies.go
93 lines (88 loc) · 2.33 KB
/
dependencies.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
package definition
import (
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
)
type dependencies struct {
Channels []*assets.ChannelReference `json:"channels,omitempty"`
Contacts []*flows.ContactReference `json:"contacts,omitempty"`
Fields []*assets.FieldReference `json:"fields,omitempty"`
Flows []*assets.FlowReference `json:"flows,omitempty"`
Groups []*assets.GroupReference `json:"groups,omitempty"`
Labels []*assets.LabelReference `json:"labels,omitempty"`
Templates []*assets.TemplateReference `json:"templates,omitempty"`
}
func newDependencies(refs []assets.Reference) *dependencies {
d := &dependencies{}
for _, r := range refs {
switch typed := r.(type) {
case *assets.ChannelReference:
d.Channels = append(d.Channels, typed)
case *flows.ContactReference:
d.Contacts = append(d.Contacts, typed)
case *assets.FieldReference:
d.Fields = append(d.Fields, typed)
case *assets.FlowReference:
d.Flows = append(d.Flows, typed)
case *assets.GroupReference:
d.Groups = append(d.Groups, typed)
case *assets.LabelReference:
d.Labels = append(d.Labels, typed)
case *assets.TemplateReference:
d.Templates = append(d.Templates, typed)
}
}
return d
}
// refreshes the asset dependencies and notifies the caller of missing assets via the callback
func (d *dependencies) refresh(sa flows.SessionAssets, missing assets.MissingCallback) error {
for i, ref := range d.Channels {
a := sa.Channels().Get(ref.UUID)
if a == nil {
missing(ref, nil)
} else {
d.Channels[i] = a.Reference()
}
}
for i, ref := range d.Fields {
a := sa.Fields().Get(ref.Key)
if a == nil {
missing(ref, nil)
} else {
d.Fields[i] = a.Reference()
}
}
for i, ref := range d.Flows {
a, err := sa.Flows().Get(ref.UUID)
if err != nil {
missing(ref, err)
} else {
d.Flows[i] = a.Reference()
}
}
for i, ref := range d.Groups {
a := sa.Groups().Get(ref.UUID)
if a == nil {
missing(ref, nil)
} else {
d.Groups[i] = a.Reference()
}
}
for i, ref := range d.Labels {
a := sa.Labels().Get(ref.UUID)
if a == nil {
missing(ref, nil)
} else {
d.Labels[i] = a.Reference()
}
}
for i, ref := range d.Templates {
a := sa.Templates().Get(ref.UUID)
if a == nil {
missing(ref, nil)
} else {
d.Templates[i] = a.Reference()
}
}
return nil
}