diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-07-11 07:12:44 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-07-15 20:47:35 +0200 |
commit | c624a77992c4f7c1bcb5f659e89332d8588986b7 (patch) | |
tree | b0c734650f9c2a6ea223e30154fdba5c97a4b0e2 /tpl/collections/merge.go | |
parent | 020086cb2b0396909d831abf66b8f1455e6f7e6a (diff) |
tpl/collections: Add Merge function
Merges two maps recursively and returns a new one. Merge is case-insensitive.
Fixes #5992
Diffstat (limited to 'tpl/collections/merge.go')
-rw-r--r-- | tpl/collections/merge.go | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/tpl/collections/merge.go b/tpl/collections/merge.go new file mode 100644 index 000000000..6916d0710 --- /dev/null +++ b/tpl/collections/merge.go @@ -0,0 +1,98 @@ +// 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 ( + "reflect" + "strings" + + "github.com/gohugoio/hugo/common/hreflect" + + "github.com/pkg/errors" +) + +// Merge creates a copy of dst and merges src into it. +// Currently only maps supported. Key handling is case insensitive. +func (ns *Namespace) Merge(src, dst interface{}) (interface{}, error) { + + vdst, vsrc := reflect.ValueOf(dst), reflect.ValueOf(src) + + if vdst.Kind() != reflect.Map { + return nil, errors.Errorf("destination must be a map, got %T", dst) + } + + if !hreflect.IsTruthfulValue(vsrc) { + return dst, nil + } + + if vsrc.Kind() != reflect.Map { + return nil, errors.Errorf("source must be a map, got %T", src) + } + + if vsrc.Type() != vdst.Type() { + return nil, errors.Errorf("incompatible map types, got %T to %T", src, dst) + } + + 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()) + + // 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 { + // If both are the same map type, merge. + dve := dv.Elem() + if dve.Kind() == reflect.Map { + sve := sv.Elem() + if dve.Type() == sve.Type() { + out.SetMapIndex(key, mergeMap(dve, sve)) + } + } + } else { + out.SetMapIndex(key, sv) + } + } + + return out +} |