From 428ad4312cb6302ea3ae75df529f1d77d0ee8b37 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Thu, 16 Nov 2017 19:09:27 +0000 Subject: [PATCH] Add an Append function which appends to a slice --- util/data/data.go | 20 ++++++++++++++++++ util/data/data_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/util/data/data.go b/util/data/data.go index 1e1ce594..9702e21d 100644 --- a/util/data/data.go +++ b/util/data/data.go @@ -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. func (d *Doc) ArrayAdd(value interface{}, path ...string) (*Doc, error) { diff --git a/util/data/data_test.go b/util/data/data_test.go index 5fbae888..fbea3fc6 100644 --- a/util/data/data_test.go +++ b/util/data/data_test.go @@ -774,6 +774,54 @@ func TestOperations(t *testing.T) { 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() { err := doc.Del("the.item.tags") So(err, ShouldBeNil)