-
Notifications
You must be signed in to change notification settings - Fork 50
/
mapnode.go
153 lines (117 loc) · 2.94 KB
/
mapnode.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
package mapx
import (
"fmt"
"github.com/spf13/cast"
)
type MapXNode struct {
value interface{}
}
func msiToMsn(src map[string]interface{}) map[string]*MapXNode {
target := make(map[string]*MapXNode)
for k, v := range src {
target[k] = interfaceToMapNode(v)
}
return target
}
func interfaceToMapNode(val interface{}) *MapXNode {
switch val := val.(type) {
case map[string]interface{}:
msn := msiToMsn(val)
return &MapXNode{value: msn}
case []map[string]interface{}:
slice := make([]map[string]*MapXNode, len(val))
for i, elem := range val {
slice[i] = msiToMsn(elem)
}
return &MapXNode{value: slice}
case []interface{}:
slice := make([]interface{}, len(val))
for i, v := range val {
slice[i] = interfaceToMapNode(v).value
}
return &MapXNode{value: slice}
default:
return &MapXNode{value: val}
}
}
func nodeMsnToMsi(msn map[string]*MapXNode) map[string]interface{} {
msi := make(map[string]interface{})
for k, node := range msn {
switch val := node.value.(type) {
case map[string]*MapXNode:
subMsi := make(map[string]interface{})
for k, node := range val {
switch val := node.value.(type) {
case map[string]*MapXNode:
subMsi[k] = nodeMsnToMsi(val)
case []interface{}:
subMsi[k] = nodeSliceToSlice(val)
default:
subMsi[k] = val
}
}
msi[k] = subMsi
case []interface{}:
msi[k] = nodeSliceToSlice(val)
default:
msi[k] = node.value
}
}
return msi
}
func nodeSliceToSlice(val []interface{}) []interface{} {
slice := make([]interface{}, len(val))
for i, elem := range val {
switch val := elem.(type) {
case map[string]*MapXNode:
slice[i] = nodeMsnToMsi(val)
case []interface{}:
slice[i] = nodeSliceToSlice(val)
default:
slice[i] = val
}
}
return slice
}
func (n *MapXNode) Data() interface{} {
switch val := n.value.(type) {
case map[string]*MapXNode:
return nodeMsnToMsi(val)
case []interface{}:
return nodeSliceToSlice(val)
default:
return val
}
}
func (n *MapXNode) IsMap() bool {
_, ok := n.value.(map[string]*MapXNode)
return ok
}
func (n *MapXNode) Map() (*MapX, error) {
if msn, ok := n.value.(map[string]*MapXNode); ok {
return &MapX{
msn: msn,
}, nil
}
return nil, fmt.Errorf("value should be of type map[string]*MapXNode but instead is %T", n.value)
}
func (n *MapXNode) Msi() (map[string]interface{}, error) {
var ok bool
var msn map[string]*MapXNode
if msn, ok = n.value.(map[string]*MapXNode); !ok {
return nil, fmt.Errorf("value should be of type map[string]*MapXNode but instead is %T", n.value)
}
msi := nodeMsnToMsi(msn)
return msi, nil
}
func (n *MapXNode) Slice() ([]interface{}, error) {
var ok bool
var slice []interface{}
if slice, ok = n.value.([]interface{}); !ok {
return nil, fmt.Errorf("value should be of type []interface{} but instead is %T", n.value)
}
return nodeSliceToSlice(slice), nil
}
func (n *MapXNode) StringSlice() ([]string, error) {
return cast.ToStringSliceE(n.value)
}