Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-04-21 10:08:42 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-04-21 20:28:18 +0300
commitbca40cf0c9c7b75e6d5b4a9ac8b927eb17590c7e (patch)
tree42f6533a89e1d6461cdfd04c9e82964310f731fd /tpl
parent057e5a22af937459082c3096ba3095b343d1a8bf (diff)
Fix Params case handling in where with slices of structs (e.g. Pages)
Fixes #7009
Diffstat (limited to 'tpl')
-rw-r--r--tpl/collections/where.go11
-rw-r--r--tpl/collections/where_test.go18
2 files changed, 28 insertions, 1 deletions
diff --git a/tpl/collections/where.go b/tpl/collections/where.go
index cada675f3..c371f6ae8 100644
--- a/tpl/collections/where.go
+++ b/tpl/collections/where.go
@@ -382,12 +382,21 @@ func (ns *Namespace) checkWhereArray(seqv, kv, mv reflect.Value, path []string,
vvv = reflect.ValueOf(params.Get(path...))
} else {
vvv = rvv
- for _, elemName := range path {
+ for i, elemName := range path {
var err error
vvv, err = evaluateSubElem(vvv, elemName)
+
if err != nil {
continue
}
+
+ if i < len(path)-1 && vvv.IsValid() {
+ if params, ok := vvv.Interface().(maps.Params); ok {
+ // The current path element is the map itself, .Params.
+ vvv = reflect.ValueOf(params.Get(path[i+1:]...))
+ break
+ }
+ }
}
}
} else {
diff --git a/tpl/collections/where_test.go b/tpl/collections/where_test.go
index 75ee109f9..47b61bdca 100644
--- a/tpl/collections/where_test.go
+++ b/tpl/collections/where_test.go
@@ -164,6 +164,24 @@ func TestWhere(t *testing.T) {
{1: "a", 2: "m"},
},
},
+ // Case insensitive maps.Params
+ // Slice of structs
+ {
+ seq: []TstParams{{params: maps.Params{"i": 0, "color": "indigo"}}, {params: maps.Params{"i": 1, "color": "blue"}}, {params: maps.Params{"i": 2, "color": "green"}}, {params: maps.Params{"i": 3, "color": "blue"}}},
+ key: ".Params.COLOR", match: "blue",
+ expect: []TstParams{{params: maps.Params{"i": 1, "color": "blue"}}, {params: maps.Params{"i": 3, "color": "blue"}}},
+ },
+ {
+ seq: []TstParams{{params: maps.Params{"nested": map[string]interface{}{"color": "indigo"}}}, {params: maps.Params{"nested": map[string]interface{}{"color": "blue"}}}},
+ key: ".Params.NEsTED.COLOR", match: "blue",
+ expect: []TstParams{{params: maps.Params{"nested": map[string]interface{}{"color": "blue"}}}},
+ },
+ {
+ seq: []TstParams{{params: maps.Params{"i": 0, "color": "indigo"}}, {params: maps.Params{"i": 1, "color": "blue"}}, {params: maps.Params{"i": 2, "color": "green"}}, {params: maps.Params{"i": 3, "color": "blue"}}},
+ key: ".Params", match: "blue",
+ expect: []TstParams{},
+ },
+ // Slice of maps
{
seq: []maps.Params{
{"a": "a1", "b": "b1"}, {"a": "a2", "b": "b2"},