Skip to content

Commit

Permalink
Fix Params case handling in where with slices of structs (e.g. Pages)
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Apr 21, 2021
1 parent 057e5a2 commit bca40cf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 10 additions & 1 deletion tpl/collections/where.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions tpl/collections/where_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down

0 comments on commit bca40cf

Please sign in to comment.