-
Notifications
You must be signed in to change notification settings - Fork 19
/
inspect.go
63 lines (53 loc) · 1.72 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
package flows
import (
"fmt"
"strings"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/utils"
)
// 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
}
// Inspectable is implemented by various flow components to allow walking the definition and extracting things like dependencies
type Inspectable interface {
Inspect(func(Inspectable))
EnumerateTemplates(Localization, func(string))
RewriteTemplates(Localization, func(string) string)
EnumerateDependencies(Localization, func(assets.Reference))
EnumerateResults(func(*ResultSpec))
}