@@ -18,32 +18,29 @@ func stripPrefix(s, prefix string) string {
18
18
return path
19
19
}
20
20
21
- func deepGet (item interface {}, path string ) interface {} {
22
- if path == "" {
23
- return item
21
+ func deepGetImpl (v reflect.Value , path []string ) interface {} {
22
+ if ! v .IsValid () {
23
+ log .Printf ("invalid value\n " )
24
+ return nil
24
25
}
25
-
26
- path = stripPrefix (path , "." )
27
- parts := strings .Split (path , "." )
28
- itemValue := reflect .ValueOf (item )
29
-
30
- if len (parts ) > 0 {
31
- switch itemValue .Kind () {
32
- case reflect .Struct :
33
- fieldValue := itemValue .FieldByName (parts [0 ])
34
- if fieldValue .IsValid () {
35
- return deepGet (fieldValue .Interface (), strings .Join (parts [1 :], "." ))
36
- }
37
- case reflect .Map :
38
- mapValue := itemValue .MapIndex (reflect .ValueOf (parts [0 ]))
39
- if mapValue .IsValid () {
40
- return deepGet (mapValue .Interface (), strings .Join (parts [1 :], "." ))
41
- }
42
- default :
43
- log .Printf ("Can't group by %s (value %v, kind %s)\n " , path , itemValue , itemValue .Kind ())
44
- }
26
+ if len (path ) == 0 {
27
+ return v .Interface ()
28
+ }
29
+ switch v .Kind () {
30
+ case reflect .Struct :
31
+ return deepGetImpl (v .FieldByName (path [0 ]), path [1 :])
32
+ case reflect .Map :
33
+ return deepGetImpl (v .MapIndex (reflect .ValueOf (path [0 ])), path [1 :])
34
+ default :
35
+ log .Printf ("unable to index by %s (value %v, kind %s)\n " , path [0 ], v , v .Kind ())
45
36
return nil
46
37
}
38
+ }
47
39
48
- return itemValue .Interface ()
40
+ func deepGet (item interface {}, path string ) interface {} {
41
+ var parts []string
42
+ if path != "" {
43
+ parts = strings .Split (stripPrefix (path , "." ), "." )
44
+ }
45
+ return deepGetImpl (reflect .ValueOf (item ), parts )
49
46
}
0 commit comments