Fix bug when deleting range queries

When performing a range query on an array when deleting, the items which were supposed to be removed, ended up being the ones which were kept.

Not the ramining array items are kept instead when deleting.
This commit is contained in:
Tobie Morgan Hitchcock 2017-02-23 15:42:58 +00:00
parent d27e00e737
commit ca6d0d86fd
2 changed files with 48 additions and 4 deletions

View file

@ -111,6 +111,20 @@ func (d *Doc) path(path ...string) (paths []string) {
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func trim(s string) string {
if s[0] == '[' && s[len(s)-1] == ']' {
return s[1 : len(s)-1]
@ -158,7 +172,7 @@ func (d *Doc) what(p string, a []interface{}, t int8) (o []interface{}, i []int,
}
// Split the specified array index
// by commas, so that we can get
// by colons, so that we can get
// the specified array items.
c := strings.Count(p, ":")
@ -216,6 +230,7 @@ func (d *Doc) what(p string, a []interface{}, t int8) (o []interface{}, i []int,
if c == 1 {
var e error
var s, f int
b := []int{0, len(a)}
x := strings.Split(p, ":")
@ -234,9 +249,24 @@ func (d *Doc) what(p string, a []interface{}, t int8) (o []interface{}, i []int,
}
}
for k := b[0]; k < b[1] && k < len(a); k++ {
i = append(i, k)
o = append(o, a[k])
s = b[0]
s = max(s, 0)
s = min(s, len(a))
f = b[1]
f = max(f, 0)
f = min(f, len(a))
if t == choose {
for k, v := range a[s:f] {
i = append(i, k)
o = append(o, v)
}
} else {
for k, v := range append(a[:s], a[f+1:]...) {
i = append(i, k)
o = append(o, v)
}
}
return o, i, many

View file

@ -760,6 +760,20 @@ func TestOperations(t *testing.T) {
So(doc.Get("the.item.tags.length").Data(), ShouldResemble, 0)
})
Convey("Can del single from array", t, func() {
_, err := doc.Inc([]interface{}{"Hot", "Humid", "Sticky", "Warm", "Snowy", "Icy"}, "the.item.tags")
So(err, ShouldBeNil)
So(doc.Get("the.item.tags").Data(), ShouldResemble, []interface{}{"Hot", "Humid", "Sticky", "Warm", "Snowy", "Icy"})
So(doc.Get("the.item.tags.length").Data(), ShouldResemble, 6)
})
Convey("Can del single from array", t, func() {
err := doc.Del("the.item.tags[0:3]")
So(err, ShouldBeNil)
So(doc.Get("the.item.tags").Data(), ShouldResemble, []interface{}{"Snowy", "Icy"})
So(doc.Get("the.item.tags.length").Data(), ShouldResemble, 2)
})
Convey("Can del array", t, func() {
err := doc.Del("the.item.tags")
So(err, ShouldBeNil)