-
Notifications
You must be signed in to change notification settings - Fork 8
/
yaml.go
140 lines (122 loc) · 3.44 KB
/
yaml.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
package goflow
import (
"os"
"gopkg.in/yaml.v2"
)
// Load will decode the yaml file at yamlFilename into a GraphRenderer, installing all
// the wrappers.
//
// Even if a wrapper returns an error, this function will go through all the wrappers.
func Load(yamlFilename string, wrappers []GraphWrapper) (GraphRenderer, error) {
file, err := os.Open(yamlFilename)
if err != nil {
return nil, Error{
Filename: yamlFilename,
Err: err,
}
}
defer file.Close()
var g graphLoader
if err := yaml.NewDecoder(file).Decode(&g); err != nil {
return nil, ParseYAMLError(err)
}
if err := file.Close(); err != nil {
return nil, Error{
Filename: yamlFilename,
Err: err,
}
}
graph, err := loadGraph(g, wrappers)
if err != nil {
return nil, Error{
Filename: yamlFilename,
Err: err,
}
}
return graph, nil
}
func loadGraph(g graphLoader, wrappers []GraphWrapper) (GraphRenderer, error) {
var wrapped GraphRenderer = g
errs := make([]error, 0)
for _, w := range wrappers {
w, err := w(g.Unmarshal, wrapped)
if err != nil {
errs = append(errs, err)
}
if w != nil {
wrapped = w
}
}
if len(errs) > 0 {
return nil, MultiError{Errs: errs}
}
return wrapped, nil
}
type graphLoader struct {
NameVal string
PkgVal string
NodesVal []nodeLoader
Unmarshal func(interface{}) error
}
func (g graphLoader) Name() string { return g.NameVal }
func (g graphLoader) Pkg() string { return g.PkgVal }
func (g graphLoader) Imports() []Import { return nil }
func (g graphLoader) Doc() string { return "" }
func (g graphLoader) Dependencies() []Field { return nil }
func (g graphLoader) Inputs() []Field { return nil }
func (g graphLoader) Outputs() []Field { return nil }
func (g graphLoader) Nodes() []NodeRenderer {
nodes := make([]NodeRenderer, len(g.NodesVal))
for i, n := range g.NodesVal {
nodes[i] = n
}
return nodes
}
func (g *graphLoader) UnmarshalYAML(unmarshal func(interface{}) error) error {
var gl struct {
Name string `yaml:"name"`
Pkg string `yaml:"package"`
Nodes []nodeLoader `yaml:"nodes"`
}
if err := unmarshal(&gl); err != nil {
return err
}
g.NameVal = gl.Name
g.PkgVal = gl.Pkg
if g.PkgVal == "" {
g.PkgVal = "graphs"
}
g.NodesVal = gl.Nodes
g.Unmarshal = func(v interface{}) error {
return ParseYAMLError(unmarshal(v))
}
return nil
}
type nodeLoader struct {
IDVal string
Unmarshal func(interface{}) error
}
func (n nodeLoader) ID() string { return n.IDVal }
func (n nodeLoader) Doc() string { return "" }
func (n nodeLoader) Previous() []string { return nil }
func (n nodeLoader) Imports() []Import { return nil }
func (n nodeLoader) Dependencies() []Field { return nil }
func (n nodeLoader) Inputs() []Field { return nil }
func (n nodeLoader) Outputs() []Field { return nil }
func (n nodeLoader) Run(inputs, outputs []Field) (string, error) { return "", nil }
func (n nodeLoader) UnmarshalFunc() func(interface{}) error {
return n.Unmarshal
}
func (n *nodeLoader) UnmarshalYAML(unmarshal func(interface{}) error) error {
var nl struct {
ID string `yaml:"id"`
}
if err := unmarshal(&nl); err != nil {
return err
}
n.IDVal = nl.ID
n.Unmarshal = func(v interface{}) error {
return ParseYAMLError(unmarshal(v))
}
return nil
}