-
Notifications
You must be signed in to change notification settings - Fork 0
/
nesting.go
52 lines (38 loc) · 1.04 KB
/
nesting.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
/*
* Public Domain Software
*
* I (Matthias Ladkau) am the author of the source code in this file.
* I have placed the source code in this file in the public domain.
*
* For further information see: http://creativecommons.org/publicdomain/zero/1.0/
*/
package datautil
import "fmt"
/*
GetNestedValue gets a value from a nested object structure.
*/
func GetNestedValue(d map[string]interface{}, path []string) (interface{}, error) {
var ret interface{}
var err error
getNestedMap := func(d map[string]interface{}, key string) (map[string]interface{}, error) {
val := d[key]
newMap, ok := val.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Unexpected data type %T as value of %v", val, key)
}
return newMap, nil
}
// Drill into the object structure and return the requested value.
nestedMap := d
atomLevel := len(path) - 1
for i, elem := range path {
if i < atomLevel {
if nestedMap, err = getNestedMap(nestedMap, elem); err != nil {
break
}
} else {
ret = nestedMap[elem]
}
}
return ret, err
}