File tree Expand file tree Collapse file tree 2 files changed +30
-14
lines changed Expand file tree Collapse file tree 2 files changed +30
-14
lines changed Original file line number Diff line number Diff line change @@ -6,18 +6,6 @@ import (
6
6
"strings"
7
7
)
8
8
9
- func stripPrefix (s , prefix string ) string {
10
- path := s
11
- for {
12
- if strings .HasPrefix (path , "." ) {
13
- path = path [1 :]
14
- continue
15
- }
16
- break
17
- }
18
- return path
19
- }
20
-
21
9
func deepGetImpl (v reflect.Value , path []string ) interface {} {
22
10
if ! v .IsValid () {
23
11
log .Printf ("invalid value\n " )
@@ -40,7 +28,7 @@ func deepGetImpl(v reflect.Value, path []string) interface{} {
40
28
func deepGet (item interface {}, path string ) interface {} {
41
29
var parts []string
42
30
if path != "" {
43
- parts = strings .Split (stripPrefix (path , "." ), "." )
31
+ parts = strings .Split (strings . TrimPrefix (path , "." ), "." )
44
32
}
45
33
return deepGetImpl (reflect .ValueOf (item ), parts )
46
34
}
Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ func TestDeepGetSimpleDotPrefix(t *testing.T) {
34
34
item := context.RuntimeContainer {
35
35
ID : "expected" ,
36
36
}
37
- value := deepGet (item , "... ID" )
37
+ value := deepGet (item , ".ID" )
38
38
assert .IsType (t , "" , value )
39
39
40
40
assert .Equal (t , "expected" , value )
@@ -51,3 +51,31 @@ func TestDeepGetMap(t *testing.T) {
51
51
52
52
assert .Equal (t , "value" , value )
53
53
}
54
+
55
+ func TestDeepGet (t * testing.T ) {
56
+ for _ , tc := range []struct {
57
+ desc string
58
+ item interface {}
59
+ path string
60
+ want interface {}
61
+ }{
62
+ {
63
+ "map key empty string" ,
64
+ map [string ]map [string ]map [string ]string {
65
+ "" : map [string ]map [string ]string {
66
+ "" : map [string ]string {
67
+ "" : "foo" ,
68
+ },
69
+ },
70
+ },
71
+ "..." ,
72
+ "foo" ,
73
+ },
74
+ } {
75
+ t .Run (tc .desc , func (t * testing.T ) {
76
+ got := deepGet (tc .item , tc .path )
77
+ assert .IsType (t , tc .want , got )
78
+ assert .Equal (t , tc .want , got )
79
+ })
80
+ }
81
+ }
You can’t perform that action at this time.
0 commit comments