mirror of
https://github.com/gohugoio/hugo.git
synced 2024-12-28 12:50:51 +00:00
Support nested keys/fields with missing values with the where
function
Before this commit `where` would produce an error and bail building the site. Now, `where` simply skips an element of a collection and does not add it to the final result. Closes #5637 Closes #5416
This commit is contained in:
parent
7e4b18c5ae
commit
908692fae5
2 changed files with 38 additions and 20 deletions
|
@ -357,7 +357,7 @@ func (ns *Namespace) checkWhereArray(seqv, kv, mv reflect.Value, path []string,
|
|||
var err error
|
||||
vvv, err = evaluateSubElem(vvv, elemName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
continue
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -159,6 +159,15 @@ func TestWhere(t *testing.T) {
|
|||
{"foo": TstX{A: "c", B: "d"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
seq: []map[string]TstX{
|
||||
{"baz": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}, {"foo": TstX{A: "e", B: "f"}},
|
||||
},
|
||||
key: "foo.B", match: "d",
|
||||
expect: []map[string]TstX{
|
||||
{"foo": TstX{A: "c", B: "d"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
seq: []map[string]TstX{
|
||||
{"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}, {"foo": TstX{A: "e", B: "f"}},
|
||||
|
@ -450,9 +459,17 @@ func TestWhere(t *testing.T) {
|
|||
key: "b", op: ">", match: false,
|
||||
expect: []map[string]bool{},
|
||||
},
|
||||
{
|
||||
seq: []map[string]bool{
|
||||
{"a": true, "b": false}, {"c": true, "b": true}, {"d": true, "b": false},
|
||||
},
|
||||
key: "b.z", match: false,
|
||||
expect: []map[string]bool{},
|
||||
},
|
||||
{seq: (*[]TstX)(nil), key: "A", match: "a", expect: false},
|
||||
{seq: TstX{A: "a", B: "b"}, key: "A", match: "a", expect: false},
|
||||
{seq: []map[string]*TstX{{"foo": nil}}, key: "foo.B", match: "d", expect: false},
|
||||
{seq: []map[string]*TstX{{"foo": nil}}, key: "foo.B", match: "d", expect: []map[string]*TstX{}},
|
||||
{seq: []map[string]*TstX{{"foo": nil}}, key: "foo.B.Z", match: "d", expect: []map[string]*TstX{}},
|
||||
{
|
||||
seq: []TstX{
|
||||
{A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"},
|
||||
|
@ -484,6 +501,7 @@ func TestWhere(t *testing.T) {
|
|||
},
|
||||
},
|
||||
} {
|
||||
t.Run(fmt.Sprintf("test case %d for key %s", i, test.key), func(t *testing.T) {
|
||||
var results interface{}
|
||||
var err error
|
||||
|
||||
|
@ -499,12 +517,12 @@ func TestWhere(t *testing.T) {
|
|||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("[%d] failed: %s", i, err)
|
||||
continue
|
||||
}
|
||||
if !reflect.DeepEqual(results, test.expect) {
|
||||
t.Errorf("[%d] Where clause matching %v with %v, got %v but expected %v", i, test.key, test.match, results, test.expect)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
var err error
|
||||
|
|
Loading…
Reference in a new issue