-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
170 lines (148 loc) · 3.6 KB
/
keys.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
package dot
import (
"encoding/json"
"reflect"
)
// Keys will get the list of keys for an arbitrary structure (non-recursively). In the result below, the result will
// be ["A", "B"], though it's best to not assume the elements are ordered.
func Keys(obj interface{}, parentPath ...string) []string {
if obj == nil {
return nil
}
strParentPath := ""
if len(parentPath) > 0 {
strParentPath = parentPath[0]
}
asMap, ok := obj.(map[string]interface{})
if ok {
var keys []string
for k := range asMap {
adjustedKey := k
if len(strParentPath) > 0 {
adjustedKey = strParentPath + "." + adjustedKey
}
keys = append(keys, adjustedKey)
}
return keys
}
defer func() {
if r := recover(); r != nil {
_ = r
}
}()
var keys []string
fields := reflect.TypeOf(obj)
if fields.Kind() == reflect.Struct {
num := fields.NumField()
for i := 0; i < num; i++ {
field := fields.Field(i)
keys = append(keys, field.Name)
}
} else if fields.Kind() == reflect.Ptr {
fields = fields.Elem()
num := fields.NumField()
for i := 0; i < num; i++ {
field := fields.Field(i)
keys = append(keys, field.Name)
}
}
for i, k := range keys {
adjustedKey := k
if len(strParentPath) > 0 {
adjustedKey = strParentPath + "." + adjustedKey
}
keys[i] = adjustedKey
}
return keys
}
func KeysWithoutReflection(obj interface{}, parentPath ...string) []string {
if obj == nil {
return []string{}
}
strParentPath := ""
if len(parentPath) > 0 {
strParentPath = parentPath[0]
}
asMap, ok := obj.(map[string]interface{})
if ok {
var keys []string
for k := range asMap {
adjustedKey := k
if len(strParentPath) > 0 {
adjustedKey = strParentPath + "." + adjustedKey
}
keys = append(keys, adjustedKey)
}
return keys
}
// TODO: find faster way - this way also does not include nils where omitempty is specified
// json to str, str to map
strJson, err := json.Marshal(obj)
if err != nil {
return []string{}
}
if err := json.Unmarshal(strJson, &asMap); err != nil {
return []string{}
}
var keys []string
for k := range asMap {
adjustedKey := k
if len(strParentPath) > 0 {
adjustedKey = strParentPath + "." + adjustedKey
}
keys = append(keys, adjustedKey)
}
return keys
}
// KeysRecursive is just like Keys, only recursive. The ordering of elements in the resulting slice is not to be
// assumed at any time
func KeysRecursive(obj interface{}, parentPath ...string) []string {
strParentPath := ""
if len(parentPath) > 0 {
strParentPath = parentPath[0]
}
var allKeys []string
keys := Keys(obj)
for _, k := range keys {
adjustedChildPath := k
if len(strParentPath) > 0 {
adjustedChildPath = strParentPath + "." + k
}
allKeys = append(allKeys, adjustedChildPath)
v, _ := Get(obj, k)
if v != nil {
allKeys = append(allKeys, KeysRecursive(v, adjustedChildPath)...)
}
}
return allKeys
}
// KeysRecursiveLeaves is like KeysRecursive, except it returns only items with no "children"
func KeysRecursiveLeaves(obj interface{}, parentPath ...string) []string {
if obj == nil {
return nil
}
strParentPath := ""
if len(parentPath) > 0 {
strParentPath = parentPath[0]
}
var allKeys []string
keys := Keys(obj)
for _, k := range keys {
adjustedChildPath := k
if len(strParentPath) > 0 {
adjustedChildPath = strParentPath + "." + k
}
v, _ := Get(obj, k)
if v != nil {
leaves := KeysRecursiveLeaves(v, adjustedChildPath)
if len(leaves) == 0 {
allKeys = append(allKeys, adjustedChildPath)
} else {
allKeys = append(allKeys, leaves...)
}
} else {
allKeys = append(allKeys, adjustedChildPath)
}
}
return allKeys
}