-
Notifications
You must be signed in to change notification settings - Fork 699
/
maputil.go
50 lines (41 loc) · 1.13 KB
/
maputil.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
package maputil
// ReadEmbeddedMap reads element k from the map m as a map[string]interface{}.
func ReadEmbeddedMap(m map[string]interface{}, k string) (map[string]interface{}, bool) {
if v, ok := m[k]; ok {
vCasted, ok := v.(map[string]interface{})
return vCasted, ok
}
return nil, false
}
// ReadEmbeddedSlice reads element k from the map m as a []interface{}.
func ReadEmbeddedSlice(m map[string]interface{}, k string) ([]interface{}, bool) {
if v, ok := m[k]; ok {
vCasted, ok := v.([]interface{})
return vCasted, ok
}
return nil, false
}
// ReadEmbeddedString reads element k from the map m as a string.
func ReadEmbeddedString(m map[string]interface{}, k string) (string, bool) {
if v, ok := m[k]; ok {
vCasted, ok := v.(string)
return vCasted, ok
}
return "", false
}
// HasElement returns true if nested element k exists.
func HasElement(m map[string]interface{}, k ...string) bool {
exists := false
kLastIndex := len(k) - 1
for i, k := range k {
isLastKey := i == kLastIndex
if isLastKey {
_, exists = m[k]
} else {
if m, exists = ReadEmbeddedMap(m, k); !exists {
break
}
}
}
return exists
}