Return empty array if requested array range

If an array range was requested, but no values matched, then a nil value was returned, instead of an empty array.

Now an empty array is returned if regardless of whether there are any matching array values or not.
This commit is contained in:
Tobie Morgan Hitchcock 2018-05-13 22:15:54 +01:00
parent 74785bd70d
commit b886a5576a
2 changed files with 16 additions and 6 deletions

View file

@ -505,7 +505,12 @@ func (d *Doc) Fetch(call Fetcher, path ...string) *Doc {
c, _, r := d.what(p, a, choose)
if len(c) == 0 {
switch r {
case one:
return &Doc{data: nil}
case many:
return &Doc{data: []interface{}{}}
}
}
if r == one {
@ -594,7 +599,12 @@ func (d *Doc) Set(value interface{}, path ...string) (*Doc, error) {
c, i, r := d.what(p, a, choose)
if len(c) == 0 {
switch r {
case one:
return &Doc{data: nil}, nil
case many:
return &Doc{data: []interface{}{}}, nil
}
}
if r == one {

View file

@ -633,10 +633,10 @@ func TestOperations(t *testing.T) {
So(doc.Get("the.item.tags[2:4]").Data(), ShouldResemble, []interface{}{"Sticky", "Warm"})
So(doc.Get("the.item.tags[2:5]").Data(), ShouldResemble, []interface{}{"Sticky", "Warm"})
So(doc.Get("the.item.tags[2:9]").Data(), ShouldResemble, []interface{}{"Sticky", "Warm"})
So(doc.Get("the.item.tags[4:5]").Data(), ShouldResemble, nil)
So(doc.Get("the.item.tags[8:9]").Data(), ShouldResemble, nil)
So(doc.Get("the.item.tags[0:none]").Data(), ShouldResemble, nil)
So(doc.Get("the.item.tags[0:none:some]").Data(), ShouldResemble, nil)
So(doc.Get("the.item.tags[4:5]").Data(), ShouldResemble, []interface{}{})
So(doc.Get("the.item.tags[8:9]").Data(), ShouldResemble, []interface{}{})
So(doc.Get("the.item.tags[0:none]").Data(), ShouldResemble, []interface{}{})
So(doc.Get("the.item.tags[0:none:some]").Data(), ShouldResemble, []interface{}{})
})
Convey("Can add single to array", t, func() {