Add an Append function which appends to a slice
This commit is contained in:
parent
4640839231
commit
428ad4312c
2 changed files with 68 additions and 0 deletions
|
@ -688,6 +688,26 @@ func (d *Doc) Del(path ...string) error {
|
||||||
|
|
||||||
// --------------------------------------------------------------------------------
|
// --------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Append appends an item or an array of items to an array at the specified path.
|
||||||
|
func (d *Doc) Append(value interface{}, path ...string) (*Doc, error) {
|
||||||
|
|
||||||
|
a, ok := d.Get(path...).Data().([]interface{})
|
||||||
|
if !ok {
|
||||||
|
return &Doc{data: nil}, fmt.Errorf("Not an array")
|
||||||
|
}
|
||||||
|
|
||||||
|
if values, ok := value.([]interface{}); ok {
|
||||||
|
for _, value := range values {
|
||||||
|
a = append(a, value)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
a = append(a, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return d.Set(a, path...)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// ArrayAdd appends an item or an array of items to an array at the specified path.
|
// ArrayAdd appends an item or an array of items to an array at the specified path.
|
||||||
func (d *Doc) ArrayAdd(value interface{}, path ...string) (*Doc, error) {
|
func (d *Doc) ArrayAdd(value interface{}, path ...string) (*Doc, error) {
|
||||||
|
|
||||||
|
|
|
@ -774,6 +774,54 @@ func TestOperations(t *testing.T) {
|
||||||
So(doc.Get("the.item.tags.length").Data(), ShouldResemble, 2)
|
So(doc.Get("the.item.tags.length").Data(), ShouldResemble, 2)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("Can add to array", t, func() {
|
||||||
|
set, err := doc.ArrayAdd("None", "the.item.tags")
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(set.Data(), ShouldHaveLength, 3)
|
||||||
|
So(doc.Get("the.item.tags").Data(), ShouldResemble, []interface{}{"Snowy", "Icy", "None"})
|
||||||
|
So(doc.Get("the.item.tags.length").Data(), ShouldResemble, 3)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Can add to array", t, func() {
|
||||||
|
set, err := doc.ArrayAdd("None", "the.item.tags")
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(set.Data(), ShouldHaveLength, 3)
|
||||||
|
So(doc.Get("the.item.tags").Data(), ShouldResemble, []interface{}{"Snowy", "Icy", "None"})
|
||||||
|
So(doc.Get("the.item.tags.length").Data(), ShouldResemble, 3)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Can del from array", t, func() {
|
||||||
|
set, err := doc.ArrayDel("None", "the.item.tags")
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(set.Data(), ShouldHaveLength, 2)
|
||||||
|
So(doc.Get("the.item.tags").Data(), ShouldResemble, []interface{}{"Snowy", "Icy"})
|
||||||
|
So(doc.Get("the.item.tags.length").Data(), ShouldResemble, 2)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Can append to array", t, func() {
|
||||||
|
set, err := doc.Append("None", "the.item.tags")
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(set.Data(), ShouldHaveLength, 3)
|
||||||
|
So(doc.Get("the.item.tags").Data(), ShouldResemble, []interface{}{"Snowy", "Icy", "None"})
|
||||||
|
So(doc.Get("the.item.tags.length").Data(), ShouldResemble, 3)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Can append to array", t, func() {
|
||||||
|
set, err := doc.Append("None", "the.item.tags")
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(set.Data(), ShouldHaveLength, 4)
|
||||||
|
So(doc.Get("the.item.tags").Data(), ShouldResemble, []interface{}{"Snowy", "Icy", "None", "None"})
|
||||||
|
So(doc.Get("the.item.tags.length").Data(), ShouldResemble, 4)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Can append to array", t, func() {
|
||||||
|
set, err := doc.Append("None", "the.item.tags")
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(set.Data(), ShouldHaveLength, 5)
|
||||||
|
So(doc.Get("the.item.tags").Data(), ShouldResemble, []interface{}{"Snowy", "Icy", "None", "None", "None"})
|
||||||
|
So(doc.Get("the.item.tags.length").Data(), ShouldResemble, 5)
|
||||||
|
})
|
||||||
|
|
||||||
Convey("Can del array", t, func() {
|
Convey("Can del array", t, func() {
|
||||||
err := doc.Del("the.item.tags")
|
err := doc.Del("the.item.tags")
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
Loading…
Reference in a new issue