Skip to content

Commit 8648033

Browse files
committed
feat: Automatically dereference pointer types in deepGet
This matches the behavior of Go, and makes it possible to use `groupBy` and friends on a slice of `*RuntimeContainers`.
1 parent a0318cd commit 8648033

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

internal/template/reflect.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ func deepGetImpl(v reflect.Value, path []string) interface{} {
1414
if len(path) == 0 {
1515
return v.Interface()
1616
}
17+
if v.Kind() == reflect.Pointer {
18+
v = v.Elem()
19+
}
20+
if v.Kind() == reflect.Pointer {
21+
log.Printf("unable to descend into pointer of a pointer\n")
22+
return nil
23+
}
1724
switch v.Kind() {
1825
case reflect.Struct:
1926
return deepGetImpl(v.FieldByName(path[0]), path[1:])

internal/template/reflect_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func TestDeepGetMap(t *testing.T) {
5353
}
5454

5555
func TestDeepGet(t *testing.T) {
56+
s := struct{ X string }{"foo"}
57+
sp := &s
58+
5659
for _, tc := range []struct {
5760
desc string
5861
item interface{}
@@ -71,6 +74,9 @@ func TestDeepGet(t *testing.T) {
7174
"...",
7275
"foo",
7376
},
77+
{"struct", s, "X", "foo"},
78+
{"pointer to struct", sp, "X", "foo"},
79+
{"double pointer to struct", &sp, ".X", nil},
7480
} {
7581
t.Run(tc.desc, func(t *testing.T) {
7682
got := deepGet(tc.item, tc.path)

0 commit comments

Comments
 (0)