-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.go
63 lines (53 loc) · 1.14 KB
/
build.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
package stream
import (
"errors"
"fmt"
)
// Build a stream from JSON definition.
func Run(stream Stream, defs []string) (Stream, error) {
if defs == nil {
return stream, nil
}
ctx := Context{"input": &StreamContext{stream, Multiplexer(stream)}}
var res Stream = nil
for _, d := range defs {
funcDef, err := ParseJson([]byte(d))
if err != nil {
return nil, err
}
p, err := build(ctx, funcDef)
if err != nil {
return nil, err
}
res = p
}
return res, nil
}
func build(ctx Context, def FArg) (Stream, error) {
smap, ok := def.(map[string]interface{})
if !ok {
return nil, errors.New(fmt.Sprintf("build: Expected map[string]interface{}, got %v", def))
}
if len(smap) != 1 {
return nil, errors.New(fmt.Sprintf("build: Expected one string key, got %v", def))
}
var name string
var args []FArg
for k, v := range smap {
name = k
tmp, ok := v.([]interface{})
if ok {
args = make([]FArg, len(tmp))
for i, v := range tmp {
args[i] = v
}
} else {
args = []FArg{v}
}
}
fn, ok := getFn(name)
if !ok {
return nil, errors.New(fmt.Sprintf("build: No such function %s", name))
}
return fn(ctx, args)
}