Fix Params case handling in where with slices of structs (e.g. Pages)
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 21 Apr 2021 07:08:42 +0000 (09:08 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 21 Apr 2021 17:28:18 +0000 (19:28 +0200)
Fixes #7009

tpl/collections/where.go
tpl/collections/where_test.go

index cada675f3d19306641d2136ca7967a716f00fd20..c371f6ae86a1c2f7e973f050fbf0c17a7bc63c03 100644 (file)
@@ -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 {
index 75ee109f9df4878bc4492de2ecd3055982bee839..47b61bdcafeab05fe78bef07dab9f6c94e57fc12 100644 (file)
@@ -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"},