Skip to content

Commit 59464e1

Browse files
committed
fix(reflect): handle deep get of map with dot(s) in key name
1 parent c26624f commit 59464e1

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

internal/template/reflect.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,23 @@ func deepGetImpl(v reflect.Value, path []string) interface{} {
4141
case reflect.Struct:
4242
return deepGetImpl(v.FieldByName(path[0]), path[1:])
4343
case reflect.Map:
44-
return deepGetImpl(v.MapIndex(reflect.ValueOf(path[0])), path[1:])
44+
// If the first part of the path is a key in the map, we use it directly
45+
if mapValue := v.MapIndex(reflect.ValueOf(path[0])); mapValue.IsValid() {
46+
return deepGetImpl(mapValue, path[1:])
47+
}
48+
49+
// If the first part of the path is not a key in the map, we try to find a valid key by joining the path parts
50+
for i := 2; i <= len(path); i++ {
51+
joinedPath := strings.Join(path[0:i], ".")
52+
if mapValue := v.MapIndex(reflect.ValueOf(joinedPath)); mapValue.IsValid() {
53+
if i == len(path) {
54+
return mapValue.Interface()
55+
}
56+
return deepGetImpl(mapValue, path[i:])
57+
}
58+
}
59+
60+
return nil
4561
case reflect.Slice, reflect.Array:
4662
i, err := parseAllocateInt(path[0])
4763
if err != nil {

0 commit comments

Comments
 (0)