-
Notifications
You must be signed in to change notification settings - Fork 18
/
convert.go
59 lines (47 loc) · 1.19 KB
/
convert.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
package larker
import (
"go.starlark.net/starlark"
"gopkg.in/yaml.v2"
)
func convertList(l *starlark.List) (result []interface{}) {
iter := l.Iterate()
defer iter.Done()
var listValue starlark.Value
for iter.Next(&listValue) {
switch value := listValue.(type) {
case *starlark.List:
result = append(result, convertList(value))
case *starlark.Dict:
result = append(result, convertDict(value))
default:
result = append(result, convertPrimitive(value))
}
}
return
}
func convertDict(d *starlark.Dict) yaml.MapSlice {
var slice yaml.MapSlice
for _, dictTuple := range d.Items() {
var sliceItem yaml.MapItem
key := dictTuple[0]
switch value := dictTuple[1].(type) {
case *starlark.List:
sliceItem = yaml.MapItem{Key: key, Value: convertList(value)}
case *starlark.Dict:
sliceItem = yaml.MapItem{Key: key, Value: convertDict(value)}
default:
sliceItem = yaml.MapItem{Key: key, Value: convertPrimitive(value)}
}
slice = append(slice, sliceItem)
}
return slice
}
func convertPrimitive(value starlark.Value) interface{} {
switch typedValue := value.(type) {
case starlark.Int:
res, _ := typedValue.Int64()
return res
default:
return typedValue
}
}