-
Notifications
You must be signed in to change notification settings - Fork 2
/
load.go
48 lines (45 loc) · 821 Bytes
/
load.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
package flow
import (
"encoding/json"
)
type config struct {
Gadgets []struct {
Type, Name string
}
Wires []struct {
From, To string
Capacity int
}
Feeds []struct {
Tag string
Data interface{}
To string
}
Labels []struct {
External, Internal string
}
}
// Load a circuit from a JSON description in a string.
func (c *Circuit) LoadJSON(data []byte) error {
var conf config
err := json.Unmarshal(data, &conf)
if err == nil {
for _, g := range conf.Gadgets {
c.Add(g.Name, g.Type)
}
for _, w := range conf.Wires {
c.Connect(w.From, w.To, w.Capacity)
}
for _, f := range conf.Feeds {
if f.Tag != "" {
c.Feed(f.To, Tag{f.Tag, f.Data})
} else {
c.Feed(f.To, f.Data)
}
}
for _, l := range conf.Labels {
c.Label(l.External, l.Internal)
}
}
return err
}