-
Notifications
You must be signed in to change notification settings - Fork 16
/
map.go
54 lines (45 loc) · 1.13 KB
/
map.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
package dynaml
import (
"fmt"
"github.com/mandelsoft/spiff/yaml"
)
type CreateMapExpr struct {
Assignments []Assignment
}
func (e CreateMapExpr) Evaluate(binding Binding, locally bool) (interface{}, EvaluationInfo, bool) {
newMap := make(map[string]yaml.Node)
info := DefaultInfo()
resolved := true
for _, a := range e.Assignments {
key, info, ok := ResolveExpressionOrPushEvaluation(&a.Key, &resolved, &info, binding, locally)
if !ok || !resolved {
return e, info, ok
}
kstr, ok := key.(string)
if !ok {
return info.Error("assignment target must evaluate to string")
}
val, info, ok := ResolveExpressionOrPushEvaluation(&a.Value, &resolved, &info, binding, locally)
if !ok || !resolved {
return e, info, ok
}
newMap[kstr] = NewNode(val, binding)
}
return newMap, DefaultInfo(), true
}
func (e CreateMapExpr) String() string {
result := "{"
sep := " "
for _, a := range e.Assignments {
result += sep + a.String()
sep = ", "
}
return result + " }"
}
type Assignment struct {
Key Expression
Value Expression
}
func (e Assignment) String() string {
return fmt.Sprintf("%s = %s", e.Key, e.Value)
}