-
Notifications
You must be signed in to change notification settings - Fork 18
/
convertmain.go
79 lines (65 loc) · 1.93 KB
/
convertmain.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
package larker
import (
"github.com/cirruslabs/cirrus-cli/pkg/yamlhelper"
"go.starlark.net/starlark"
"gopkg.in/yaml.v3"
"strings"
)
func convertTasks(starlarkTasks *starlark.List) *yaml.Node {
yamlList := convertList(starlarkTasks)
if yamlList == nil || len(yamlList.Content) == 0 {
return nil
}
// Adapt a list of tasks to a YAML configuration format that expects a map on it's outer layer
var serializableMainResult []*yaml.Node
for _, listItem := range yamlList.Content {
serializableMainResult = append(serializableMainResult, yamlhelper.NewStringNode("task"))
serializableMainResult = append(serializableMainResult, listItem)
}
return yamlhelper.NewMapNode(serializableMainResult)
}
func convertList(l *starlark.List) *yaml.Node {
iter := l.Iterate()
defer iter.Done()
var listValue starlark.Value
var items []*yaml.Node
for iter.Next(&listValue) {
switch value := listValue.(type) {
case *starlark.List:
items = append(items, convertList(value))
case *starlark.Dict:
items = append(items, convertDict(value))
default:
var valueNode yaml.Node
_ = valueNode.Encode(convertPrimitive(value))
items = append(items, &valueNode)
}
}
return yamlhelper.NewSeqNode(items)
}
func convertDict(d *starlark.Dict) *yaml.Node {
var items []*yaml.Node
for _, dictTuple := range d.Items() {
items = append(items, yamlhelper.NewStringNode(strings.Trim(dictTuple[0].String(), "'\"")))
switch value := dictTuple[1].(type) {
case *starlark.List:
items = append(items, convertList(value))
case *starlark.Dict:
items = append(items, convertDict(value))
default:
var valueNode yaml.Node
_ = valueNode.Encode(convertPrimitive(value))
items = append(items, &valueNode)
}
}
return yamlhelper.NewMapNode(items)
}
func convertPrimitive(value starlark.Value) interface{} {
switch typedValue := value.(type) {
case starlark.Int:
res, _ := typedValue.Int64()
return res
default:
return typedValue
}
}