-
Notifications
You must be signed in to change notification settings - Fork 3
/
preprocess.go
67 lines (60 loc) · 1.33 KB
/
preprocess.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
package cwl
import (
"github.com/commondream/yamlast"
)
func (l *loader) preprocess(n node) (node, error) {
switch n.Kind {
case yamlast.MappingNode:
for i := 0; i < len(n.Children)-1; i += 2 {
k := n.Children[i]
v := n.Children[i+1]
switch k.Value {
case "$import":
if _, ok := l.resolver.(noResolver); ok {
return n, nil
}
b, _, err := l.resolver.Resolve(l.base, v.Value)
if err != nil {
return nil, err
}
yamlnode, err := yamlast.Parse(b)
if err != nil {
return nil, err
}
// TODO set line/col/file of the new nodes
return yamlnode.Children[0], nil
case "$include":
if _, ok := l.resolver.(noResolver); ok {
return n, nil
}
b, _, err := l.resolver.Resolve(l.base, v.Value)
if err != nil {
return nil, err
}
// TODO check line/col of the new node is correct
return node(&yamlast.Node{
Kind: yamlast.ScalarNode,
Line: n.Line,
Column: n.Column,
Value: string(b),
}), nil
// TODO $mixin
default:
x, err := l.preprocess(v)
if err != nil {
return nil, err
}
n.Children[i+1] = x
}
}
case yamlast.SequenceNode:
for i, c := range n.Children {
x, err := l.preprocess(c)
if err != nil {
return nil, err
}
n.Children[i] = x
}
}
return n, nil
}