-
Notifications
You must be signed in to change notification settings - Fork 4
/
items.go
56 lines (47 loc) · 944 Bytes
/
items.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
package lunar
import (
"encoding/json"
"strings"
)
// Items is items under namespace
type Items map[string]string
// Get gets value of given key
func (items Items) Get(key string) string {
if v, ok := items[key]; ok {
return v
}
return ""
}
// String converts Items to json string
func (items Items) String() string {
bytes, _ := json.Marshal(items.Expand())
return string(bytes)
}
// Expand expands dot-key items to nested map
func (items Items) Expand() interface{} {
var root Node
for k, v := range items {
ks := strings.Split(k, ".")
parent := root.GetChild(ks[0])
if parent == nil {
parent = &Node{
Name: ks[0],
Value: v,
}
root.AddChildren(parent)
}
node := parent
for i := 1; i < len(ks); i++ {
child := node.GetChild(ks[i])
if child == nil {
child = &Node{
Name: ks[i],
Value: v,
}
node.AddChildren(child)
}
node = child
}
}
return root.ToMap()
}