/
flow.go
68 lines (58 loc) · 1.98 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
package resources
import (
"fmt"
"reflect"
yaml "gopkg.in/yaml.v2"
)
// AddSinkToCamelFlow adds the knative endpoint sink to a user-provided yaml flow
func AddSinkToCamelFlow(flow map[interface{}]interface{}, sink string) map[interface{}]interface{} {
var from map[interface{}]interface{}
var steps []interface{}
if from1, ok := flow["from"]; ok && reflect.TypeOf(from1).Kind() == reflect.Map {
from = from1.(map[interface{}]interface{})
} else {
from = make(map[interface{}]interface{})
}
if steps1, ok := from["steps"]; ok && reflect.TypeOf(steps1).Kind() == reflect.Slice {
steps = steps1.([]interface{})
} else {
steps = make([]interface{}, 0)
}
to := make(map[interface{}]interface{})
to["uri"] = fmt.Sprintf("knative://endpoint/%s", sink)
step := make(map[interface{}]interface{})
step["to"] = to
steps = append(steps, step)
from["steps"] = steps
flow["from"] = from
return flow
}
// UnmarshalCamelFlow unmarshals a flow from its standard yaml representation
func UnmarshalCamelFlow(data string) (map[interface{}]interface{}, error) {
f := make(map[interface{}]interface{})
err := yaml.Unmarshal([]byte(data), &f)
return f, err
}
// MarshalCamelFlow marshals a flow into its standard yaml representation
func MarshalCamelFlow(flow map[interface{}]interface{}) (string, error) {
return marshalCamelFlowStruct(flow)
}
// MarshalCamelFlows marshals a list of flows into their standard yaml representation
func MarshalCamelFlows(flows []map[interface{}]interface{}) (string, error) {
return marshalCamelFlowStruct(flows)
}
func marshalCamelFlowStruct(flow interface{}) (string, error) {
b, err := yaml.Marshal(flow)
if err != nil {
return "", err
}
return string(b), nil
}
// CopyCamelFlow returns a copy of the flow obtained through marshal and unmarshal into the standard yaml representation
func CopyCamelFlow(flow interface{}) (map[interface{}]interface{}, error) {
m, err := marshalCamelFlowStruct(flow)
if err != nil {
return nil, err
}
return UnmarshalCamelFlow(m)
}