/
makemap.go
64 lines (58 loc) · 1.49 KB
/
makemap.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
package dynaml
import (
"github.com/mandelsoft/spiff/yaml"
"strconv"
)
func func_makemap(arguments []interface{}, binding Binding) (interface{}, EvaluationInfo, bool) {
info := DefaultInfo()
result := make(map[string]yaml.Node)
var key string
if len(arguments) == 1 {
list, ok := arguments[0].([]yaml.Node)
if !ok {
return info.Error("single argument flavor of mapentry requires a list")
}
for i, elem := range list {
m, ok := elem.Value().(map[string]yaml.Node)
if !ok {
return info.Error("entry %d is no map entry", i)
}
k, ok := m["key"]
if !ok {
return info.Error("entry %d has no entry 'key'", i)
}
switch elem := k.Value().(type) {
case string:
key = elem
case int64:
key = strconv.FormatInt(elem, 10)
case bool:
key = strconv.FormatBool(elem)
default:
return info.Error("invalid type for 'key' value of entry %d", i)
}
v, ok := m["value"]
if !ok {
return info.Error("entry %d has no entry 'value'", i)
}
result[key] = v
}
} else if len(arguments)%2 == 0 {
for i := 0; i < len(arguments); i += 2 {
switch elem := arguments[i].(type) {
case string:
key = elem
case int64:
key = strconv.FormatInt(elem, 10)
case bool:
key = strconv.FormatBool(elem)
default:
return info.Error("invalid type for key value of arument pair %d", i/2)
}
result[key] = NewNode(arguments[i+1], binding)
}
} else {
return info.Error("mapentry takes one or two arguments")
}
return result, info, true
}