-
Notifications
You must be signed in to change notification settings - Fork 20
/
inspect.go
217 lines (185 loc) · 5.95 KB
/
inspect.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
package flows
import (
"fmt"
"strings"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/excellent/tools"
"github.com/nyaruka/goflow/utils"
)
var fieldRefPaths = [][]string{
{"fields"},
{"contact", "fields"},
{"parent", "fields"},
{"parent", "contact", "fields"},
{"child", "fields"},
{"child", "contact", "fields"},
}
// Inspectable is implemented by various flow components to allow walking the definition and extracting things like dependencies
type Inspectable interface {
Inspect(func(Inspectable))
EnumerateTemplates(TemplateIncluder)
EnumerateDependencies(Localization, func(assets.Reference))
EnumerateResults(func(*ResultSpec))
}
// ResultSpec is possible result that a flow might generate
type ResultSpec struct {
Key string `json:"key"`
Name string `json:"name"`
Categories []string `json:"categories,omitempty"`
}
// NewResultSpec creates a new result spec
func NewResultSpec(name string, categories []string) *ResultSpec {
return &ResultSpec{
Key: utils.Snakify(name),
Name: name,
Categories: categories,
}
}
func (r *ResultSpec) String() string {
return fmt.Sprintf("key=%s|name=%s|categories=%s", r.Key, r.Name, strings.Join(r.Categories, ","))
}
// MergeResultSpecs merges result specs based on key
func MergeResultSpecs(specs []*ResultSpec) []*ResultSpec {
merged := make([]*ResultSpec, 0, len(specs))
byKey := make(map[string]*ResultSpec)
for _, spec := range specs {
existing := byKey[spec.Key]
if existing != nil {
// if we already have a result spec with this key, merge categories
for _, category := range spec.Categories {
if !utils.StringSliceContains(existing.Categories, category, false) {
existing.Categories = append(existing.Categories, category)
}
}
} else {
// if not, add as new unique result spec
merged = append(merged, spec)
byKey[spec.Key] = spec
}
}
return merged
}
// TemplateIncluder is interface passed to EnumerateTemplates to include templates on flow entities
type TemplateIncluder interface {
String(*string)
Slice([]string)
Map(map[string]string)
Translations(Localizable, string)
}
type templateEnumerator struct {
localization Localization
include func(string)
}
// NewTemplateEnumerator creates a template includer for enumerating templates
func NewTemplateEnumerator(localization Localization, include func(string)) TemplateIncluder {
return &templateEnumerator{localization: localization, include: include}
}
func (t *templateEnumerator) String(s *string) {
t.include(*s)
}
func (t *templateEnumerator) Slice(a []string) {
for s := range a {
t.include(a[s])
}
}
func (t *templateEnumerator) Map(m map[string]string) {
for k := range m {
t.include(m[k])
}
}
func (t *templateEnumerator) Translations(localizable Localizable, key string) {
for _, lang := range t.localization.Languages() {
translations := t.localization.GetTranslations(lang)
t.Slice(translations.GetTextArray(localizable.LocalizationUUID(), key))
}
}
type templateRewriter struct {
localization Localization
rewrite func(string) string
}
// NewTemplateRewriter creates a template includer for rewriting templates
func NewTemplateRewriter(localization Localization, rewrite func(string) string) TemplateIncluder {
return &templateRewriter{localization: localization, rewrite: rewrite}
}
func (t *templateRewriter) String(s *string) {
*s = t.rewrite(*s)
}
func (t *templateRewriter) Slice(a []string) {
for s := range a {
a[s] = t.rewrite(a[s])
}
}
func (t *templateRewriter) Map(m map[string]string) {
for k := range m {
m[k] = t.rewrite(m[k])
}
}
func (t *templateRewriter) Translations(localizable Localizable, key string) {
for _, lang := range t.localization.Languages() {
translations := t.localization.GetTranslations(lang)
t.Slice(translations.GetTextArray(localizable.LocalizationUUID(), key))
}
}
// wrapper for an asset reference to make it inspectable
type inspectableReference struct {
ref assets.Reference
}
// InspectReference inspects the given asset reference if it's non-nil
func InspectReference(ref assets.Reference, inspect func(Inspectable)) {
if ref != nil {
inspectableReference{ref: ref}.Inspect(inspect)
}
}
// Inspect inspects this object and any children
func (r inspectableReference) Inspect(inspect func(Inspectable)) {
inspect(r)
}
// EnumerateTemplates enumerates all expressions on this object and its children
func (r inspectableReference) EnumerateTemplates(include TemplateIncluder) {
if r.ref != nil && r.ref.Variable() {
switch typed := r.ref.(type) {
case *assets.GroupReference:
include.String(&typed.NameMatch)
case *assets.LabelReference:
include.String(&typed.NameMatch)
}
}
}
// EnumerateDependencies enumerates all dependencies on this object and its children
func (r inspectableReference) EnumerateDependencies(localization Localization, include func(assets.Reference)) {
if r.ref != nil && !r.ref.Variable() {
include(r.ref)
}
}
// EnumerateResults enumerates all potential results on this object
// Asset references can't contain results.
func (r inspectableReference) EnumerateResults(include func(*ResultSpec)) {}
// ExtractFieldReferences extracts fields references from the given template
func ExtractFieldReferences(template string) []*assets.FieldReference {
fieldRefs := make([]*assets.FieldReference, 0)
tools.FindContextRefsInTemplate(template, RunContextTopLevels, func(path []string) {
isField, fieldKey := isFieldRefPath(path)
if isField {
fieldRefs = append(fieldRefs, assets.NewFieldReference(fieldKey, ""))
}
})
return fieldRefs
}
// checks whether the given context path is a reference to a contact field
func isFieldRefPath(path []string) (bool, string) {
for _, possible := range fieldRefPaths {
if len(path) == len(possible)+1 {
matches := true
for i := range possible {
if strings.ToLower(path[i]) != possible[i] {
matches = false
break
}
}
if matches {
return true, strings.ToLower(path[len(possible)])
}
}
}
return false, ""
}