Skip to content

Commit

Permalink
tpl: Fix reflection bug in merge
Browse files Browse the repository at this point in the history
Value.Type().Key() must only be called on map values.

Fixes #7899
  • Loading branch information
moorereason authored and bep committed Oct 29, 2020
1 parent 56a3435 commit 6d95dc9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tpl/collections/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func mergeMap(dst, src reflect.Value) reflect.Value {
dve := dv.Elem()
if dve.Kind() == reflect.Map {
sve := sv.Elem()
if sve.Kind() != reflect.Map {
continue
}

if dve.Type().Key() == sve.Type().Key() {
out.SetMapIndex(key, mergeMap(dve, sve))
}
Expand Down
9 changes: 9 additions & 0 deletions tpl/collections/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ func TestMerge(t *testing.T) {
},
maps.Params{"a": 1, "b": maps.Params{"d": 1, "e": 2, "f": 3}, "c": 3}, false,
},
{
// https://github.com/gohugoio/hugo/issues/7899
"matching keys with non-map src value",
[]interface{}{
map[string]interface{}{"k": "v"},
map[string]interface{}{"k": map[string]interface{}{"k2": "v2"}},
},
map[string]interface{}{"k": map[string]interface{}{"k2": "v2"}}, false,
},
{"src nil", []interface{}{nil, simpleMap}, simpleMap, false},
// Error cases.
{"dst not a map", []interface{}{nil, "not a map"}, nil, true},
Expand Down

0 comments on commit 6d95dc9

Please sign in to comment.