-
Notifications
You must be signed in to change notification settings - Fork 16
/
template.go
109 lines (94 loc) · 2.86 KB
/
template.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
package dynaml
import (
"fmt"
"path"
"strings"
"github.com/mandelsoft/spiff/debug"
"github.com/mandelsoft/spiff/yaml"
)
type SubstitutionExpr struct {
Template Expression
}
func (e SubstitutionExpr) Evaluate(binding Binding, locally bool) (interface{}, EvaluationInfo, bool) {
debug.Debug("evaluating expression to determine template: %s\n", binding)
n, info, ok := e.Template.Evaluate(binding, false)
if !ok || IsExpression(n) {
return e, info, ok
}
inp := map[string]yaml.Node{}
template, ok := n.(TemplateValue)
if !ok {
return info.Error("template value required")
}
prepared := node_copy(template.Prepared)
inp[yaml.SELF] = yaml.ResolverNode(NewNode(n, binding), template.resolver)
debug.Debug("resolving template '%s' %s\n", strings.Join(template.Path, "."), binding)
result, state := binding.WithLocalScope(inp).Flow(prepared, false)
info = DefaultInfo()
if result != nil && result.Undefined() {
info.Undefined = true
}
if state != nil {
if state.HasError() {
debug.Debug("resolving template failed: " + state.Error())
return info.PropagateError(e, state, "resolution of template '%s' failed", strings.Join(template.Path, "."))
} else {
debug.Debug("resolving template delayed: " + state.Error())
return e, info, true
}
}
debug.Debug("resolving template succeeded")
info.Source = result.SourceName()
return result.Value(), info, true
}
func (e SubstitutionExpr) String() string {
return fmt.Sprintf("*(%s)", e.Template)
}
type TemplateValue struct {
Path []string
Prepared yaml.Node
Orig yaml.Node
resolver Binding
}
var _ StaticallyScopedValue = TemplateValue{}
var _ yaml.ComparableValue = TemplateValue{}
func NewTemplateValue(path []string, prepared yaml.Node, orig yaml.Node, binding Binding) TemplateValue {
return TemplateValue{path, prepared, orig, binding}
}
func (e TemplateValue) String() string {
return fmt.Sprintf("<template %s: %s>", path.Join(e.Path...), Shorten(Short(e.Prepared, false)))
}
func (e TemplateValue) MarshalYAML() (tag string, value interface{}, err error) {
return e.Orig.MarshalYAML()
}
func (e TemplateValue) StaticResolver() Binding {
return e.resolver
}
func (e TemplateValue) SetStaticResolver(binding Binding) StaticallyScopedValue {
e.resolver = binding
return e
}
func (e TemplateValue) EquivalentTo(val interface{}) bool {
o, ok := val.(TemplateValue)
return ok && e.Orig.EquivalentToNode(o.Orig)
}
func node_copy(node yaml.Node) yaml.Node {
if node == nil {
return nil
}
switch val := node.Value().(type) {
case []yaml.Node:
list := make([]yaml.Node, len(val))
for i, v := range val {
list[i] = node_copy(v)
}
return yaml.NewNode(list, node.SourceName())
case map[string]yaml.Node:
m := make(map[string]yaml.Node)
for k, v := range val {
m[k] = node_copy(v)
}
return yaml.NewNode(m, node.SourceName())
}
return yaml.NewNode(node.Value(), node.SourceName())
}