tpl/collections: Some more params merge adjustments

* If the end result is maps.Params, we need to lower the keys
* Also make sure to handle nested maps of different type

Update #6633
This commit is contained in:
Bjørn Erik Pedersen
2019-12-19 20:50:30 +01:00
committed by GitHub
parent a67d95fe1a
commit ccb1bf1abb
2 changed files with 25 additions and 3 deletions

View File

@@ -17,6 +17,8 @@ import (
"reflect"
"strings"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/common/hreflect"
"github.com/pkg/errors"
@@ -68,6 +70,9 @@ func mergeMap(dst, src reflect.Value) reflect.Value {
out := reflect.MakeMap(dst.Type())
// If the destination is Params, we must lower case all keys.
_, lowerCase := dst.Interface().(maps.Params)
// Copy the destination map.
for _, key := range dst.MapKeys() {
v := dst.MapIndex(key)
@@ -81,15 +86,18 @@ func mergeMap(dst, src reflect.Value) reflect.Value {
dv, found := caseInsensitiveLookup(dst, key)
if found {
// If both are the same map type, merge.
// If both are the same map key type, merge.
dve := dv.Elem()
if dve.Kind() == reflect.Map {
sve := sv.Elem()
if dve.Type() == sve.Type() {
if dve.Type().Key() == sve.Type().Key() {
out.SetMapIndex(key, mergeMap(dve, sve))
}
}
} else {
if lowerCase && key.Kind() == reflect.String {
key = reflect.ValueOf(strings.ToLower(key.String()))
}
out.SetMapIndex(key, sv)
}
}