-
Notifications
You must be signed in to change notification settings - Fork 20
/
generic.go
106 lines (90 loc) · 2.5 KB
/
generic.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
package definition
import (
"encoding/json"
"strings"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils/uuids"
)
// ReadFlowFromGeneric tries to read a flow in the current spec from the given generic map
func ReadFlowFromGeneric(data map[string]interface{}) (flows.Flow, error) {
marshaled, err := json.Marshal(data)
if err != nil {
return nil, err
}
return ReadFlow(marshaled, nil)
}
// MustReadFlowFromGeneric tries to read a flow from the given generic map, panics if it can't
func MustReadFlowFromGeneric(data map[string]interface{}) flows.Flow {
f, err := ReadFlowFromGeneric(data)
if err != nil {
panic(err.Error())
}
return f
}
// remap all UUIDs in the flow
func remapUUIDs(data map[string]interface{}, depMapping map[uuids.UUID]uuids.UUID) {
// copy in the dependency mappings into a master mapping of all UUIDs
mapping := make(map[uuids.UUID]uuids.UUID)
for k, v := range depMapping {
mapping[k] = v
}
replaceUUID := func(u uuids.UUID) uuids.UUID {
if u == uuids.UUID("") {
return uuids.UUID("")
}
mapped, exists := mapping[u]
if !exists {
mapped = uuids.New()
mapping[u] = mapped
}
return mapped
}
objectCallback := func(obj map[string]interface{}) {
props := objectProperties(obj)
for _, p := range props {
v := obj[p]
if p == "uuid" || strings.HasSuffix(p, "_uuid") {
asString, isString := v.(string)
if isString {
obj[p] = replaceUUID(uuids.UUID(asString))
}
} else if uuids.IsV4(p) {
newProperty := string(replaceUUID(uuids.UUID(p)))
obj[newProperty] = v
delete(obj, p)
}
}
}
arrayCallback := func(arr []interface{}) {
for i, v := range arr {
asString, isString := v.(string)
if isString && uuids.IsV4(asString) {
arr[i] = replaceUUID(uuids.UUID(asString))
}
}
}
walk(data, objectCallback, arrayCallback)
}
// extract the property names from a generic JSON object
func objectProperties(obj map[string]interface{}) []string {
props := make([]string, 0, len(obj))
for k := range obj {
props = append(props, k)
}
return props
}
// walks the given generic JSON invoking the given callbacks for each thing found
func walk(j interface{}, objectCallback func(map[string]interface{}), arrayCallback func([]interface{})) {
switch typed := j.(type) {
case map[string]interface{}:
objectCallback(typed)
for _, v := range typed {
walk(v, objectCallback, arrayCallback)
}
case []interface{}:
arrayCallback(typed)
for _, v := range typed {
walk(v, objectCallback, arrayCallback)
}
}
}