diff --git a/util/data/data.go b/util/data/data.go index 0b156ca0..1f17cf89 100644 --- a/util/data/data.go +++ b/util/data/data.go @@ -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 diff --git a/util/data/data_test.go b/util/data/data_test.go index c519c862..280adea4 100644 --- a/util/data/data_test.go +++ b/util/data/data_test.go @@ -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)