Skip to content

fix(reflect): revert deep get of map with dot(s) in key name #684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 1 addition & 18 deletions internal/template/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,7 @@ func deepGetImpl(v reflect.Value, path []string) interface{} {
case reflect.Struct:
return deepGetImpl(v.FieldByName(path[0]), path[1:])
case reflect.Map:
// If the first part of the path is a key in the map, we use it directly
if mapValue := v.MapIndex(reflect.ValueOf(path[0])); mapValue.IsValid() {
return deepGetImpl(mapValue, path[1:])
}

// 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
for i := 2; i <= len(path); i++ {
joinedPath := strings.Join(path[0:i], ".")
if mapValue := v.MapIndex(reflect.ValueOf(joinedPath)); mapValue.IsValid() {
if i == len(path) {
return mapValue.Interface()
}
return deepGetImpl(mapValue, path[i:])
}
}

log.Printf("unable to find key from the path expression %s in map %v\n", strings.Join(path, "."), v)
return nil
return deepGetImpl(v.MapIndex(reflect.ValueOf(path[0])), path[1:])
case reflect.Slice, reflect.Array:
i, err := parseAllocateInt(path[0])
if err != nil {
Expand Down
22 changes: 0 additions & 22 deletions internal/template/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,6 @@ func TestDeepGet(t *testing.T) {
"...",
"foo",
},
{
"map with dot in key",
map[string]map[string]string{
"Foo": {
"foo.bar.baz.qux": "quux",
},
},
"Foo.foo.bar.baz.qux",
"quux",
},
{
"nested maps with dot in keys",
map[string]map[string]map[string]string{
"Foo": {
"foo.bar": {
"baz.qux": "quux",
},
},
},
"Foo.foo.bar.baz.qux",
"quux",
},
{"struct", s, "X", "foo"},
{"pointer to struct", sp, "X", "foo"},
{"double pointer to struct", &sp, ".X", nil},
Expand Down