tpl: Extend merge to accept multiple parameters

Fixes #7595
This commit is contained in:
Cameron Moore 2020-08-27 21:34:45 -05:00 committed by Bjørn Erik Pedersen
parent f9ebaaed1b
commit 047af7cfe5
3 changed files with 106 additions and 65 deletions

View file

@ -116,10 +116,12 @@ func init() {
[][2]string{ [][2]string{
{ {
`{{ (querify "foo" 1 "bar" 2 "baz" "with spaces" "qux" "this&that=those") | safeHTML }}`, `{{ (querify "foo" 1 "bar" 2 "baz" "with spaces" "qux" "this&that=those") | safeHTML }}`,
`bar=2&baz=with+spaces&foo=1&qux=this%26that%3Dthose`}, `bar=2&baz=with+spaces&foo=1&qux=this%26that%3Dthose`,
},
{ {
`<a href="https://www.google.com?{{ (querify "q" "test" "page" 3) | safeURL }}">Search</a>`, `<a href="https://www.google.com?{{ (querify "q" "test" "page" 3) | safeURL }}">Search</a>`,
`<a href="https://www.google.com?page=3&amp;q=test">Search</a>`}, `<a href="https://www.google.com?page=3&amp;q=test">Search</a>`,
},
}, },
) )
@ -186,15 +188,22 @@ func init() {
ns.AddMethodMapping(ctx.Merge, ns.AddMethodMapping(ctx.Merge,
[]string{"merge"}, []string{"merge"},
[][2]string{ [][2]string{
{`{{ dict "title" "Hugo Rocks!" | collections.Merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") | sort }}`, {
`[Yes, Hugo Rocks! Hugo Rocks!]`}, `{{ dict "title" "Hugo Rocks!" | collections.Merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") | sort }}`,
{`{{ merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") (dict "title" "Hugo Rocks!") | sort }}`, `[Yes, Hugo Rocks! Hugo Rocks!]`,
`[Yes, Hugo Rocks! Hugo Rocks!]`}, },
{
`{{ merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") (dict "title" "Hugo Rocks!") | sort }}`,
`[Yes, Hugo Rocks! Hugo Rocks!]`,
},
{
`{{ merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") (dict "title" "Hugo Rocks!") (dict "extra" "For reals!") | sort }}`,
`[Yes, Hugo Rocks! For reals! Hugo Rocks!]`,
},
}, },
) )
return ns return ns
} }
internal.AddTemplateFuncsNamespace(f) internal.AddTemplateFuncsNamespace(f)

View file

@ -17,17 +17,35 @@ import (
"reflect" "reflect"
"strings" "strings"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/common/hreflect" "github.com/gohugoio/hugo/common/hreflect"
"github.com/gohugoio/hugo/common/maps"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
// Merge creates a copy of dst and merges src into it. // Merge creates a copy of the final parameter and merges the preceeding
// Currently only maps supported. Key handling is case insensitive. // parameters into it in reverse order.
func (ns *Namespace) Merge(src, dst interface{}) (interface{}, error) { // Currently only maps are supported. Key handling is case insensitive.
func (ns *Namespace) Merge(params ...interface{}) (interface{}, error) {
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
}
// merge creates a copy of dst and merges src into it.
func (ns *Namespace) merge(src, dst interface{}) (interface{}, error) {
vdst, vsrc := reflect.ValueOf(dst), reflect.ValueOf(src) vdst, vsrc := reflect.ValueOf(dst), reflect.ValueOf(src)
if vdst.Kind() != reflect.Map { if vdst.Kind() != reflect.Map {
@ -60,14 +78,12 @@ func caseInsensitiveLookup(m, k reflect.Value) (reflect.Value, bool) {
if strings.EqualFold(k.String(), key.String()) { if strings.EqualFold(k.String(), key.String()) {
return m.MapIndex(key), true return m.MapIndex(key), true
} }
} }
return reflect.Value{}, false return reflect.Value{}, false
} }
func mergeMap(dst, src reflect.Value) reflect.Value { func mergeMap(dst, src reflect.Value) reflect.Value {
out := reflect.MakeMap(dst.Type()) out := reflect.MakeMap(dst.Type())
// If the destination is Params, we must lower case all keys. // If the destination is Params, we must lower case all keys.

View file

@ -15,82 +15,108 @@ package collections
import ( import (
"bytes" "bytes"
"fmt"
"reflect" "reflect"
"runtime"
"strings"
"testing" "testing"
"github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/parser" "github.com/gohugoio/hugo/parser"
"github.com/gohugoio/hugo/parser/metadecoders" "github.com/gohugoio/hugo/parser/metadecoders"
qt "github.com/frankban/quicktest" qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/deps"
) )
func TestMerge(t *testing.T) { func TestMerge(t *testing.T) {
ns := New(&deps.Deps{}) ns := New(&deps.Deps{})
simpleMap := map[string]interface{}{"a": 1, "b": 2} simpleMap := map[string]interface{}{"a": 1, "b": 2}
for i, test := range []struct { for i, test := range []struct {
name string name string
dst interface{} params []interface{}
src interface{}
expect interface{} expect interface{}
isErr bool isErr bool
}{ }{
{ {
"basic", "basic",
map[string]interface{}{"a": 1, "b": 2}, []interface{}{
map[string]interface{}{"a": 42, "c": 3}, map[string]interface{}{"a": 42, "c": 3},
map[string]interface{}{"a": 1, "b": 2, "c": 3}, false}, map[string]interface{}{"a": 1, "b": 2},
},
map[string]interface{}{"a": 1, "b": 2, "c": 3}, false,
},
{
"multi",
[]interface{}{
map[string]interface{}{"a": 42, "c": 3, "e": 11},
map[string]interface{}{"a": 1, "b": 2},
map[string]interface{}{"a": 9, "c": 4, "d": 7},
},
map[string]interface{}{"a": 9, "b": 2, "c": 4, "d": 7, "e": 11}, false,
},
{ {
"basic case insensitive", "basic case insensitive",
map[string]interface{}{"a": 1, "b": 2}, []interface{}{
map[string]interface{}{"A": 42, "c": 3}, map[string]interface{}{"A": 42, "c": 3},
map[string]interface{}{"a": 1, "b": 2, "c": 3}, false}, map[string]interface{}{"a": 1, "b": 2},
},
map[string]interface{}{"a": 1, "b": 2, "c": 3}, false,
},
{ {
"nested", "nested",
map[string]interface{}{"a": 1, "b": map[string]interface{}{"d": 1, "e": 2}}, []interface{}{
map[string]interface{}{"a": 42, "c": 3, "b": map[string]interface{}{"d": 55, "e": 66, "f": 3}}, map[string]interface{}{"a": 42, "c": 3, "b": map[string]interface{}{"d": 55, "e": 66, "f": 3}},
map[string]interface{}{"a": 1, "b": map[string]interface{}{"d": 1, "e": 2, "f": 3}, "c": 3}, false}, map[string]interface{}{"a": 1, "b": map[string]interface{}{"d": 1, "e": 2}},
},
map[string]interface{}{"a": 1, "b": map[string]interface{}{"d": 1, "e": 2, "f": 3}, "c": 3}, false,
},
{ {
// https://github.com/gohugoio/hugo/issues/6633 // https://github.com/gohugoio/hugo/issues/6633
"params dst", "params dst",
maps.Params{"a": 1, "b": 2}, []interface{}{
map[string]interface{}{"a": 42, "c": 3}, map[string]interface{}{"a": 42, "c": 3},
maps.Params{"a": int(1), "b": int(2), "c": int(3)}, false}, maps.Params{"a": 1, "b": 2},
},
maps.Params{"a": int(1), "b": int(2), "c": int(3)}, false,
},
{ {
"params dst, upper case src", "params dst, upper case src",
maps.Params{"a": 1, "b": 2}, []interface{}{
map[string]interface{}{"a": 42, "C": 3}, map[string]interface{}{"a": 42, "C": 3},
maps.Params{"a": int(1), "b": int(2), "c": int(3)}, false}, maps.Params{"a": 1, "b": 2},
},
maps.Params{"a": int(1), "b": int(2), "c": int(3)}, false,
},
{ {
"params src", "params src",
map[string]interface{}{"a": 1, "c": 2}, []interface{}{
maps.Params{"a": 42, "c": 3}, maps.Params{"a": 42, "c": 3},
map[string]interface{}{"a": int(1), "c": int(2)}, false}, map[string]interface{}{"a": 1, "c": 2},
},
map[string]interface{}{"a": int(1), "c": int(2)}, false,
},
{ {
"params src, upper case dst", "params src, upper case dst",
map[string]interface{}{"a": 1, "C": 2}, []interface{}{
maps.Params{"a": 42, "c": 3}, maps.Params{"a": 42, "c": 3},
map[string]interface{}{"a": int(1), "C": int(2)}, false}, map[string]interface{}{"a": 1, "C": 2},
},
map[string]interface{}{"a": int(1), "C": int(2)}, false,
},
{ {
"nested, params dst", "nested, params dst",
maps.Params{"a": 1, "b": maps.Params{"d": 1, "e": 2}}, []interface{}{
map[string]interface{}{"a": 42, "c": 3, "b": map[string]interface{}{"d": 55, "e": 66, "f": 3}}, map[string]interface{}{"a": 42, "c": 3, "b": map[string]interface{}{"d": 55, "e": 66, "f": 3}},
maps.Params{"a": 1, "b": maps.Params{"d": 1, "e": 2, "f": 3}, "c": 3}, false}, maps.Params{"a": 1, "b": maps.Params{"d": 1, "e": 2}},
{"src nil", simpleMap, nil, simpleMap, false}, },
maps.Params{"a": 1, "b": maps.Params{"d": 1, "e": 2, "f": 3}, "c": 3}, false,
},
{"src nil", []interface{}{nil, simpleMap}, simpleMap, false},
// Error cases. // Error cases.
{"dst not a map", "not a map", nil, nil, true}, {"dst not a map", []interface{}{nil, "not a map"}, nil, true},
{"src not a map", simpleMap, "not a map", nil, true}, {"src not a map", []interface{}{"not a map", simpleMap}, nil, true},
{"different map types", simpleMap, map[int]interface{}{32: "a"}, nil, true}, {"different map types", []interface{}{map[int]interface{}{32: "a"}, simpleMap}, nil, true},
{"all nil", nil, nil, nil, true}, {"all nil", []interface{}{nil, nil}, nil, true},
} { } {
test := test test := test
@ -101,9 +127,7 @@ func TestMerge(t *testing.T) {
c := qt.New(t) c := qt.New(t)
srcStr, dstStr := fmt.Sprint(test.src), fmt.Sprint(test.dst) result, err := ns.Merge(test.params...)
result, err := ns.Merge(test.src, test.dst)
if test.isErr { if test.isErr {
c.Assert(err, qt.Not(qt.IsNil), errMsg) c.Assert(err, qt.Not(qt.IsNil), errMsg)
@ -112,14 +136,6 @@ func TestMerge(t *testing.T) {
c.Assert(err, qt.IsNil) c.Assert(err, qt.IsNil)
c.Assert(result, qt.DeepEquals, test.expect, errMsg) c.Assert(result, qt.DeepEquals, test.expect, errMsg)
// map sort in fmt was fixed in go 1.12.
if !strings.HasPrefix(runtime.Version(), "go1.11") {
// Verify that the original maps are preserved.
c.Assert(fmt.Sprint(test.src), qt.Equals, srcStr)
c.Assert(fmt.Sprint(test.dst), qt.Equals, dstStr)
}
}) })
} }
} }
@ -172,9 +188,9 @@ V22 = "v22_2"
qt.DeepEquals, qt.DeepEquals,
map[string]interface{}{ map[string]interface{}{
"V1": "v1_1", "V2": "v2_2", "V1": "v1_1", "V2": "v2_2",
"V2s": map[string]interface{}{"V21": "v21_1", "V22": "v22_2"}}) "V2s": map[string]interface{}{"V21": "v21_1", "V22": "v22_2"},
})
} }
} }
func TestCaseInsensitiveMapLookup(t *testing.T) { func TestCaseInsensitiveMapLookup(t *testing.T) {