This repository has been archived by the owner on Jan 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
152 lines (133 loc) · 3.32 KB
/
config.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
package graphpipe
import (
"fmt"
"io/ioutil"
"log"
"launchpad.net/goyaml"
)
type NodeConfig struct {
Name string
Type string
Inject []string
Input []string
Config interface{}
}
type Config struct {
Verbose bool
Services, Nodes []NodeConfig
}
func readConfig(bytes []byte) (config *Config, err error) {
config = &Config{}
// first pass to get node types
err = goyaml.Unmarshal(bytes, config)
if err != nil {
return
}
// second pass to get node configs
for _, nodes := range [][]NodeConfig{config.Services, config.Nodes} {
for i := range nodes {
// Is there a way not to remarshal the config?
if nodes[i].Config != nil {
remarshal, err := goyaml.Marshal(nodes[i].Config)
if err != nil {
return nil, err
}
nodes[i].Config = NewConfig(nodes[i].Type)
err = goyaml.Unmarshal(remarshal, nodes[i].Config)
if err != nil {
return nil, err
}
} else {
nodes[i].Config = NewConfig(nodes[i].Type)
}
}
}
return
}
// Construct a graphpipe from a YAML config.
func GraphPipeFromYAML(yaml []byte) (*GraphPipe, error) {
config, err := readConfig(yaml)
if err != nil {
return nil, err
}
ncount := len(config.Nodes)
pipe := &GraphPipe{
verbose: config.Verbose,
nodes: make([]Node, ncount),
children: make([][]int, ncount),
}
// services map
servicesMap := make(map[string]interface{})
// create node and inject services
newNode := func(nodeConfig NodeConfig) (interface{}, error) {
if config.Verbose {
log.Printf("Creating: %+v\n", nodeConfig)
}
var injects []interface{}
for _, serviceName := range nodeConfig.Inject {
service := servicesMap[serviceName]
injects = append(injects, service)
}
something, err := NewNode(nodeConfig.Type, nodeConfig.Config, injects...)
if something == nil && err == nil {
err = fmt.Errorf("Creation of %s failed", nodeConfig.Name)
}
return something, err
}
// setup Services
for _, serviceConfig := range config.Services {
service, err := newNode(serviceConfig)
if err != nil {
return nil, err
}
servicesMap[serviceConfig.Name] = service
}
// setup Nodes
nodesMap := make(map[string]int)
hasSource := false
for i, nodeConfig := range config.Nodes {
nodeV, err := newNode(nodeConfig)
if err != nil {
return nil, err
}
node := nodeV.(Node)
pipe.nodes[i] = node
_, isSource := node.(SourceNode)
hasSource = hasSource || isSource
nodesMap[nodeConfig.Name] = i
servicesMap[nodeConfig.Name] = node
}
if !hasSource {
return nil, fmt.Errorf("You must have at least one source node!")
}
// setup input for nodes
for i, nodeConfig := range config.Nodes {
node := pipe.nodes[i]
if len(nodeConfig.Input) > 0 {
var sources []Node
for _, nodeName := range nodeConfig.Input {
depIndex := nodesMap[nodeName]
dep := pipe.nodes[depIndex]
sources = append(sources, dep)
pipe.children[depIndex] = append(pipe.children[depIndex], i)
}
if pipe.verbose {
log.Println("Setting input for node", nodeConfig.Name)
}
if err := SetInput(node, sources...); err != nil {
return nil, err
}
}
}
if pipe.verbose {
log.Println("Children:", pipe.children)
}
return pipe, nil
}
func GraphPipeFromYAMLFile(filename string) (*GraphPipe, error) {
bytes, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return GraphPipeFromYAML(bytes)
}