/
otree.go
214 lines (187 loc) · 5.19 KB
/
otree.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package nanocms_compiler
import (
"fmt"
"reflect"
"github.com/go-yaml/yaml"
)
/*
A representation of an object tree, preserving ordering.
*/
type OTree struct {
_data map[interface{}]interface{}
_kidx []interface{}
}
func NewOTree() *OTree {
return new(OTree).Flush()
}
// Flush the content of the tree
func (tree *OTree) Flush() *OTree {
tree._data = make(map[interface{}]interface{})
tree._kidx = make([]interface{}, 0)
return tree
}
// LoadMapSlice loads a yaml.MapSlice object that keeps the ordering
func (tree *OTree) LoadMapSlice(data yaml.MapSlice) *OTree {
for _, item := range data {
kind := reflect.TypeOf(item.Value).Kind()
switch kind {
case reflect.Slice:
tree.Set(item.Key, tree.getMapSlice(item.Value.(yaml.MapSlice), nil))
case reflect.String:
tree.Set(item.Key, item.Value)
default:
panic(fmt.Errorf("Unknown type '%s' while loading state", kind))
}
}
return tree
}
func (tree *OTree) getArray(data interface{}) []interface{} {
cnt := make([]interface{}, 0)
for _, element := range data.([]interface{}) {
switch reflect.TypeOf(element).Kind() {
case reflect.String:
cnt = append(cnt, element.(string))
case reflect.Slice:
cnt = append(cnt, tree.getMapSlice(element.(yaml.MapSlice), nil))
default:
panic(fmt.Sprintf("Value %s has an unsupported type %s", element, reflect.TypeOf(element)))
}
}
return cnt
}
func (tree *OTree) getMapSlice(data yaml.MapSlice, cnt *OTree) *OTree {
if cnt == nil {
cnt = NewOTree()
}
for _, item := range data {
if item.Value != nil {
kind := reflect.TypeOf(item.Value).Kind()
switch kind {
case reflect.Slice:
i_val_t := reflect.TypeOf(item.Value)
if i_val_t.Kind() == reflect.Slice && i_val_t.Elem().Kind() == reflect.Interface {
cnt.Set(item.Key, tree.getArray(item.Value))
} else {
cnt.Set(item.Key, tree.getMapSlice(item.Value.(yaml.MapSlice), nil))
}
case reflect.String:
cnt.Set(item.Key, item.Value)
case reflect.Bool:
cnt.Set(item.Key, item.Value)
default:
panic(fmt.Errorf("Unknown type '%s' while loading state", kind))
}
} else {
cnt.Set(item.Key, nil)
}
}
return cnt
}
// Set the key/value
func (tree *OTree) Set(key interface{}, value interface{}) *OTree {
if tree.Exists(key) {
tree._data[key] = value
} else {
tree._kidx = append(tree._kidx, key)
tree._data[key] = value
}
return tree
}
// Get key with the default
func (tree *OTree) Get(key interface{}, bydefault interface{}) interface{} {
if tree.Exists(key) {
return tree._data[key]
}
return bydefault
}
// GetBranch of the current tree. If branch is not an OTree object or not found, nil is returned.
func (tree *OTree) GetBranch(key interface{}) *OTree {
obj := tree.Get(key, nil)
if obj != nil && reflect.TypeOf(obj).Elem().Kind() == reflect.Struct {
return obj.(*OTree)
}
return nil
}
// GetList returns an object as an array of the interfaces. If an object is not a slice, nil is returned.
func (tree *OTree) GetList(key interface{}) []interface{} {
obj := tree.Get(key, nil)
if reflect.TypeOf(obj).Kind() == reflect.Slice {
return obj.([]interface{})
}
return nil
}
// GetString returns a string, blindly assuming it is one.
// XXX: better implementation needed. :)
func (tree *OTree) GetString(key interface{}) string {
return tree.Get(key, nil).(string)
}
// Check if key is there
func (tree *OTree) Exists(key interface{}) bool {
_, ex := tree._data[key]
return ex
}
// Delete key. Nothing happens if the key wasn't there.
func (tree *OTree) Delete(key interface{}) *OTree {
if tree.Exists(key) {
for i, k := range tree._kidx {
if k == key {
delete(tree._data, key)
tree._kidx = append(tree._kidx[:i], tree._kidx[i+1:]...)
return tree
}
}
}
return tree
}
// Return keys
func (tree *OTree) Keys() []interface{} {
return tree._kidx
}
func (tree *OTree) Items() [][]interface{} {
return nil
}
func (tree *OTree) _to_structure(cnt map[string]interface{}, obj interface{}) interface{} {
if obj == nil {
return nil
}
if cnt == nil {
cnt = make(map[string]interface{})
}
objType := reflect.TypeOf(obj).Kind()
if objType == reflect.Ptr {
for _, obj_k := range obj.(*OTree).Keys() {
cnt[obj_k.(string)] = tree._to_structure(nil, obj.(*OTree).Get(obj_k, nil))
}
} else if objType == reflect.Map {
for obj_k := range obj.(map[interface{}]interface{}) {
cnt[obj_k.(string)] = tree._to_structure(nil, obj.(map[interface{}]interface{})[obj_k])
}
} else if objType == reflect.Slice {
arr := make([]interface{}, 0)
for _, element := range obj.([]interface{}) {
arr = append(arr, tree._to_structure(nil, element))
}
return arr
} else if objType == reflect.String {
return obj.(string)
} else if objType == reflect.Bool {
return obj.(bool)
} else {
fmt.Println("unsupported DSL type:", objType)
}
return cnt
}
// ToYAML exports ordered tree to an unordered YAML (!)
func (tree *OTree) ToYAML() string {
obj := tree._to_structure(nil, tree._data)
data, _ := yaml.Marshal(&obj)
return string(data)
}
func (tree *OTree) Serialise() map[string]interface{} {
obj := tree._to_structure(nil, tree._data)
shallowObj := make(map[string]interface{})
for k, v := range obj.(map[string]interface{}) {
shallowObj[k] = v
}
return shallowObj
}