2019-07-11 01:12:44 -04:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package collections
|
|
|
|
|
|
|
|
import (
|
2024-01-28 16:34:10 -05:00
|
|
|
"errors"
|
2022-05-02 10:07:52 -04:00
|
|
|
"fmt"
|
2019-07-11 01:12:44 -04:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/common/hreflect"
|
2020-08-27 22:34:45 -04:00
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
2019-07-11 01:12:44 -04:00
|
|
|
)
|
|
|
|
|
2022-12-21 07:11:08 -05:00
|
|
|
// Merge creates a copy of the final parameter in params and merges the preceding
|
2020-08-27 22:34:45 -04:00
|
|
|
// parameters into it in reverse order.
|
2022-12-21 07:11:08 -05:00
|
|
|
//
|
2020-08-27 22:34:45 -04:00
|
|
|
// Currently only maps are supported. Key handling is case insensitive.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) Merge(params ...any) (any, error) {
|
2020-08-27 22:34:45 -04:00
|
|
|
if len(params) < 2 {
|
|
|
|
return nil, errors.New("merge requires at least two parameters")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
result := params[len(params)-1]
|
|
|
|
|
|
|
|
for i := len(params) - 2; i >= 0; i-- {
|
|
|
|
result, err = ns.merge(params[i], result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
2019-07-11 01:12:44 -04:00
|
|
|
|
2020-08-27 22:34:45 -04:00
|
|
|
// merge creates a copy of dst and merges src into it.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) merge(src, dst any) (any, error) {
|
2019-07-11 01:12:44 -04:00
|
|
|
vdst, vsrc := reflect.ValueOf(dst), reflect.ValueOf(src)
|
|
|
|
|
|
|
|
if vdst.Kind() != reflect.Map {
|
2022-05-02 10:07:52 -04:00
|
|
|
return nil, fmt.Errorf("destination must be a map, got %T", dst)
|
2019-07-11 01:12:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !hreflect.IsTruthfulValue(vsrc) {
|
|
|
|
return dst, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if vsrc.Kind() != reflect.Map {
|
2022-05-02 10:07:52 -04:00
|
|
|
return nil, fmt.Errorf("source must be a map, got %T", src)
|
2019-07-11 01:12:44 -04:00
|
|
|
}
|
|
|
|
|
2019-12-18 15:31:34 -05:00
|
|
|
if vsrc.Type().Key() != vdst.Type().Key() {
|
2022-05-02 10:07:52 -04:00
|
|
|
return nil, fmt.Errorf("incompatible map types, got %T to %T", src, dst)
|
2019-07-11 01:12:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return mergeMap(vdst, vsrc).Interface(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func caseInsensitiveLookup(m, k reflect.Value) (reflect.Value, bool) {
|
|
|
|
if m.Type().Key().Kind() != reflect.String || k.Kind() != reflect.String {
|
|
|
|
// Fall back to direct lookup.
|
|
|
|
v := m.MapIndex(k)
|
|
|
|
return v, hreflect.IsTruthfulValue(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range m.MapKeys() {
|
|
|
|
if strings.EqualFold(k.String(), key.String()) {
|
|
|
|
return m.MapIndex(key), true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return reflect.Value{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeMap(dst, src reflect.Value) reflect.Value {
|
|
|
|
out := reflect.MakeMap(dst.Type())
|
|
|
|
|
2019-12-19 14:50:30 -05:00
|
|
|
// If the destination is Params, we must lower case all keys.
|
|
|
|
_, lowerCase := dst.Interface().(maps.Params)
|
|
|
|
|
2019-07-11 01:12:44 -04:00
|
|
|
// Copy the destination map.
|
|
|
|
for _, key := range dst.MapKeys() {
|
|
|
|
v := dst.MapIndex(key)
|
|
|
|
out.SetMapIndex(key, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add all keys in src not already in destination.
|
|
|
|
// Maps of the same type will be merged.
|
|
|
|
for _, key := range src.MapKeys() {
|
|
|
|
sv := src.MapIndex(key)
|
|
|
|
dv, found := caseInsensitiveLookup(dst, key)
|
|
|
|
|
|
|
|
if found {
|
2019-12-19 14:50:30 -05:00
|
|
|
// If both are the same map key type, merge.
|
2019-07-11 01:12:44 -04:00
|
|
|
dve := dv.Elem()
|
|
|
|
if dve.Kind() == reflect.Map {
|
|
|
|
sve := sv.Elem()
|
2020-10-28 22:44:38 -04:00
|
|
|
if sve.Kind() != reflect.Map {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-19 14:50:30 -05:00
|
|
|
if dve.Type().Key() == sve.Type().Key() {
|
2019-07-11 01:12:44 -04:00
|
|
|
out.SetMapIndex(key, mergeMap(dve, sve))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-12-19 14:50:30 -05:00
|
|
|
if lowerCase && key.Kind() == reflect.String {
|
|
|
|
key = reflect.ValueOf(strings.ToLower(key.String()))
|
|
|
|
}
|
2019-07-11 01:12:44 -04:00
|
|
|
out.SetMapIndex(key, sv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|