| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package collections |
| 15 |
|
| 16 |
import ( |
| 17 |
"errors" |
| 18 |
"fmt" |
| 19 |
"reflect" |
| 20 |
"strings" |
| 21 |
|
| 22 |
"github.com/gohugoio/hugo/common/hmaps" |
| 23 |
"github.com/gohugoio/hugo/common/hreflect" |
| 24 |
) |
| 25 |
|
| 26 |
// Merge creates a copy of the final parameter in params and merges the preceding |
| 27 |
// parameters into it in reverse order. |
| 28 |
// |
| 29 |
// Currently only maps are supported. Key handling is case insensitive. |
| 30 |
func (ns *Namespace) Merge(params ...any) (any, error) { |
| 31 |
if len(params) < 2 { |
| 32 |
return nil, errors.New("merge requires at least two parameters") |
| 33 |
} |
| 34 |
|
| 35 |
var err error |
| 36 |
result := params[len(params)-1] |
| 37 |
|
| 38 |
for i := len(params) - 2; i >= 0; i-- { |
| 39 |
result, err = ns.merge(params[i], result) |
| 40 |
if err != nil { |
| 41 |
return nil, err |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
return result, nil |
| 46 |
} |
| 47 |
|
| 48 |
// merge creates a copy of dst and merges src into it. |
| 49 |
func (ns *Namespace) merge(src, dst any) (any, error) { |
| 50 |
vdst, vsrc := reflect.ValueOf(dst), reflect.ValueOf(src) |
| 51 |
|
| 52 |
if vdst.Kind() != reflect.Map { |
| 53 |
return nil, fmt.Errorf("destination must be a map, got %T", dst) |
| 54 |
} |
| 55 |
|
| 56 |
if !hreflect.IsTruthfulValue(vsrc) { |
| 57 |
return dst, nil |
| 58 |
} |
| 59 |
|
| 60 |
if vsrc.Kind() != reflect.Map { |
| 61 |
return nil, fmt.Errorf("source must be a map, got %T", src) |
| 62 |
} |
| 63 |
|
| 64 |
if vsrc.Type().Key() != vdst.Type().Key() { |
| 65 |
return nil, fmt.Errorf("incompatible map types, got %T to %T", src, dst) |
| 66 |
} |
| 67 |
|
| 68 |
return mergeMap(vdst, vsrc).Interface(), nil |
| 69 |
} |
| 70 |
|
| 71 |
func caseInsensitiveLookup(m, k reflect.Value) (reflect.Value, bool) { |
| 72 |
if m.Type().Key().Kind() != reflect.String || k.Kind() != reflect.String { |
| 73 |
// Fall back to direct lookup. |
| 74 |
v := m.MapIndex(k) |
| 75 |
return v, hreflect.IsTruthfulValue(v) |
| 76 |
} |
| 77 |
|
| 78 |
k2 := reflect.New(m.Type().Key()).Elem() |
| 79 |
|
| 80 |
iter := m.MapRange() |
| 81 |
for iter.Next() { |
| 82 |
k2.SetIterKey(iter) |
| 83 |
if strings.EqualFold(k.String(), k2.String()) { |
| 84 |
return iter.Value(), true |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return reflect.Value{}, false |
| 89 |
} |
| 90 |
|
| 91 |
func mergeMap(dst, src reflect.Value) reflect.Value { |
| 92 |
out := reflect.MakeMap(dst.Type()) |
| 93 |
|
| 94 |
// If the destination is Params, we must lower case all keys. |
| 95 |
_, lowerCase := dst.Interface().(hmaps.Params) |
| 96 |
|
| 97 |
k := reflect.New(dst.Type().Key()).Elem() |
| 98 |
v := reflect.New(dst.Type().Elem()).Elem() |
| 99 |
|
| 100 |
// Copy the destination map. |
| 101 |
iter := dst.MapRange() |
| 102 |
for iter.Next() { |
| 103 |
k.SetIterKey(iter) |
| 104 |
v.SetIterValue(iter) |
| 105 |
out.SetMapIndex(k, v) |
| 106 |
} |
| 107 |
|
| 108 |
// Add all keys in src not already in destination. |
| 109 |
// Maps of the same type will be merged. |
| 110 |
k = reflect.New(src.Type().Key()).Elem() |
| 111 |
sv := reflect.New(src.Type().Elem()).Elem() |
| 112 |
|
| 113 |
iter = src.MapRange() |
| 114 |
for iter.Next() { |
| 115 |
sv.SetIterValue(iter) |
| 116 |
k.SetIterKey(iter) |
| 117 |
|
| 118 |
dv, found := caseInsensitiveLookup(dst, k) |
| 119 |
|
| 120 |
if found { |
| 121 |
// If both are the same map key type, merge. |
| 122 |
dve := dv.Elem() |
| 123 |
if dve.Kind() == reflect.Map { |
| 124 |
sve := sv.Elem() |
| 125 |
if sve.Kind() != reflect.Map { |
| 126 |
continue |
| 127 |
} |
| 128 |
|
| 129 |
if dve.Type().Key() == sve.Type().Key() { |
| 130 |
out.SetMapIndex(k, mergeMap(dve, sve)) |
| 131 |
} |
| 132 |
} |
| 133 |
} else { |
| 134 |
kk := k |
| 135 |
if lowerCase && k.Kind() == reflect.String { |
| 136 |
kk = reflect.ValueOf(strings.ToLower(k.String())) |
| 137 |
} |
| 138 |
out.SetMapIndex(kk, sv) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return out |
| 143 |
} |