implement deleting for array members
diff --git a/README.md b/README.md
index a31ce94..0005924 100644
--- a/README.md
+++ b/README.md
@@ -25,7 +25,7 @@
title, _, _ := pointer.Get(jsonDocument)
fmt.Println(title) //outputs "Supreme Leader of Westeros"
- //DELETE the "heir" from the document (only works on maps for now, not arrays)
+ //DELETE the "heir" from the document
deletePointer := NewJsonPointer("/occupation/heir")
deletePointer.Delete(jsonDocument)
diff --git a/pointer.go b/pointer.go
index 74e0116..386bcec 100644
--- a/pointer.go
+++ b/pointer.go
@@ -90,7 +90,7 @@
}
-// Uses the pointer to delete a value from a JSON document (only works on maps/objects right now, not arrays)
+// Uses the pointer to delete a value from a JSON document
func (p *JsonPointer) Delete(document interface{}) (interface{}, error) {
is := &implStruct{mode: "DEL", inDocument: document}
p.implementation(is)
@@ -113,9 +113,14 @@
node := i.inDocument
+ previousNodes := make([]interface{}, len(p.referenceTokens))
+ previousTokens := make([]string, len(p.referenceTokens))
+
for ti, token := range p.referenceTokens {
isLastToken := ti == len(p.referenceTokens)-1
+ previousNodes[ti] = node
+ previousTokens[ti] = token
switch v := node.(type) {
@@ -153,6 +158,11 @@
node = v[tokenIndex]
if isLastToken && i.mode == "SET" {
v[tokenIndex] = i.setInValue
+ } else if isLastToken && i.mode =="DEL" {
+ v[tokenIndex] = v[len(v)-1]
+ v[len(v)-1] = nil
+ v = v[:len(v)-1]
+ previousNodes[ti-1].(map[string]interface{})[previousTokens[ti-1]] = v
}
default:
diff --git a/pointer_test.go b/pointer_test.go
index 171396f..ce5b47e 100644
--- a/pointer_test.go
+++ b/pointer_test.go
@@ -246,9 +246,9 @@
}
-func TestDelMapNode(t *testing.T) {
+func TestDelObject(t *testing.T) {
jsonText := `{
- "a":[{"b": 1, "c": 2}],
+ "a":["apple sauce", "ketchup", "soy sauce"],
"d": {
"z" : {
"v" : {
@@ -262,6 +262,7 @@
var jsonDocument map[string]interface{}
json.Unmarshal([]byte(jsonText), &jsonDocument)
+ //Deleting an object key
in := "/d/z/v/occupation"
p, err := NewJsonPointer(in)
if err != nil {
@@ -277,11 +278,44 @@
var z map[string]interface{} = d["z"].(map[string]interface{})
var v map[string]interface{} = z["v"].(map[string]interface{})
- if v["name"] != "donald mcbobble" {
- t.Errorf("Delete (%s) failed: caused an unexpected side effect")
- }
-
if _, present := v["occupation"]; present {
t.Errorf("Delete (%s) failed: key is still present in the map", in)
}
}
+
+
+func TestDelArray(t *testing.T) {
+ jsonText := `{
+ "a":["applesauce", "ketchup", "soysauce", "oliveoil"],
+ "d": {
+ "z" : {
+ "v" : {
+ "name" : "donald mcbobble",
+ "occupation" : "corporate overlord",
+ "responsibilities" : ["managing", "hiring"]
+ }
+ }
+ }
+ }`
+
+ var jsonDocument map[string]interface{}
+ json.Unmarshal([]byte(jsonText), &jsonDocument)
+
+ //Deleting an array member
+ in := "/a/2"
+ p, err := NewJsonPointer(in)
+ if err != nil {
+ t.Errorf("NewJsonPointer(%v) error %v", in, err.Error())
+ }
+
+ _, err = p.Delete(jsonDocument)
+ if err != nil {
+ t.Errorf("Delete(%v) error %v", in, err.Error())
+ }
+
+ a := jsonDocument["a"].([]interface{})
+ if len(a) != 3 || a[2] == "soysauce" {
+ t.Errorf("Delete(%v) error (%s)", in, a)
+ }
+
+}