summaryrefslogtreecommitdiffstats
path: root/tpl/collections
diff options
context:
space:
mode:
authorAnton Harniakou <anton.harniakou@gmail.com>2019-02-03 14:03:44 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-02-06 19:53:34 +0100
commit908692fae5c5840a0db8c7dd389b59dd3b8026b9 (patch)
tree8622e52ecfb3108e456af5279c1e10936955e83b /tpl/collections
parent7e4b18c5ae409435760ebd86ff9ee3061db34a5d (diff)
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
Diffstat (limited to 'tpl/collections')
-rw-r--r--tpl/collections/where.go2
-rw-r--r--tpl/collections/where_test.go56
2 files changed, 38 insertions, 20 deletions
diff --git a/tpl/collections/where.go b/tpl/collections/where.go
index 859353ff0..2c5dc7f3f 100644
--- a/tpl/collections/where.go
+++ b/tpl/collections/where.go
@@ -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 {
diff --git a/tpl/collections/where_test.go b/tpl/collections/where_test.go
index bf3d5011c..fb768cfde 100644
--- a/tpl/collections/where_test.go
+++ b/tpl/collections/where_test.go
@@ -161,6 +161,15 @@ func TestWhere(t *testing.T) {
},
{
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"}},
},
key: ".foo.B", match: "d",
@@ -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,27 +501,28 @@ func TestWhere(t *testing.T) {
},
},
} {
- var results interface{}
- var err error
+ t.Run(fmt.Sprintf("test case %d for key %s", i, test.key), func(t *testing.T) {
+ var results interface{}
+ var err error
- if len(test.op) > 0 {
- results, err = ns.Where(test.seq, test.key, test.op, test.match)
- } else {
- results, err = ns.Where(test.seq, test.key, test.match)
- }
- if b, ok := test.expect.(bool); ok && !b {
- if err == nil {
- t.Errorf("[%d] Where didn't return an expected error", i)
+ if len(test.op) > 0 {
+ results, err = ns.Where(test.seq, test.key, test.op, test.match)
+ } else {
+ results, err = ns.Where(test.seq, test.key, test.match)
}
- } 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)
+ if b, ok := test.expect.(bool); ok && !b {
+ if err == nil {
+ t.Errorf("[%d] Where didn't return an expected error", i)
+ }
+ } else {
+ if err != nil {
+ t.Errorf("[%d] failed: %s", i, err)
+ }
+ 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