diff --git a/handler.go b/handler.go index 02061d9..41ffc9e 100644 --- a/handler.go +++ b/handler.go @@ -3,49 +3,49 @@ package jsonpatch // Handler is the interfaces used by the walker to create patches type Handler interface { // Add creates a JSONPatch with an 'add' operation and appends it to the patch list - Add(path JSONPointer, modified interface{}) []JSONPatch + Add(pointer JSONPointer, modified interface{}) []JSONPatch // Remove creates a JSONPatch with an 'remove' operation and appends it to the patch list - Remove(path JSONPointer, current interface{}) []JSONPatch + Remove(pointer JSONPointer, current interface{}) []JSONPatch // Replace creates a JSONPatch with an 'replace' operation and appends it to the patch list - Replace(path JSONPointer, modified, current interface{}) []JSONPatch + Replace(pointer JSONPointer, modified, current interface{}) []JSONPatch } // DefaultHandler implements the Handler type DefaultHandler struct{} // Add implements Handler -func (h *DefaultHandler) Add(path JSONPointer, value interface{}) []JSONPatch { +func (h *DefaultHandler) Add(pointer JSONPointer, value interface{}) []JSONPatch { // The 'add' operation either inserts a value into the array at the specified index or adds a new member to the object // NOTE: If the target location specifies an object member that does exist, that member's value is replaced return []JSONPatch{ { Operation: "add", - Path: path.String(), + Path: pointer.String(), Value: value, }, } } // Remove implements Handler -func (h *DefaultHandler) Remove(path JSONPointer, _ interface{}) []JSONPatch { - // The 'remove' operation removes the value at the target location (specified by the path) +func (h *DefaultHandler) Remove(pointer JSONPointer, _ interface{}) []JSONPatch { + // The 'remove' operation removes the value at the target location (specified by the pointer) return []JSONPatch{ { Operation: "remove", - Path: path.String(), + Path: pointer.String(), }, } } // Replace implements Handler -func (h *DefaultHandler) Replace(path JSONPointer, value, _ interface{}) []JSONPatch { +func (h *DefaultHandler) Replace(pointer JSONPointer, value, _ interface{}) []JSONPatch { // The 'replace' operation replaces the value at the target location with a new value return []JSONPatch{ { Operation: "replace", - Path: path.String(), + Path: pointer.String(), Value: value, }, } diff --git a/patch_test.go b/patch_test.go index 2f28205..2a55597 100644 --- a/patch_test.go +++ b/patch_test.go @@ -320,6 +320,53 @@ var _ = Describe("JSONPatch", func() { testPatchWithExpected(F{}, F{B: &B{Str: "don't remove me"}}, F{B: &B{Str: "don't remove me"}}, jsonpatch.WithPredicate(predicate)) }) }) + Context("CreateJsonPatch_with_prefix", func() { + It("empty prefix", func() { + testPatchWithExpected(F{B: &B{Bool: true, Str: "str"}}, F{}, F{B: &B{Bool: true, Str: "str"}}, jsonpatch.WithPrefix([]string{""})) + }) + It("pointer prefix", func() { + modified := F{A: &A{B: &B{Bool: true, Str: "str"}}} + current := F{A: &A{}} + expected := F{A: &A{B: &B{Bool: true, Str: "str"}}} + + currentJSON, err := json.Marshal(current) + Ω(err).ShouldNot(HaveOccurred()) + _, err = json.Marshal(modified) + Ω(err).ShouldNot(HaveOccurred()) + expectedJSON, err := json.Marshal(expected) + Ω(err).ShouldNot(HaveOccurred()) + + bytes, _, err := jsonpatch.CreateJSONPatch(modified.A.B, current.A.B, jsonpatch.WithPrefix(jsonpatch.ParseJSONPointer("/a/ptr"))) + Ω(err).ShouldNot(HaveOccurred()) + Ω(bytes.String()).ShouldNot(Equal("")) + jsonPatch, err := jsonpatch2.DecodePatch(bytes) + Ω(err).ShouldNot(HaveOccurred()) + patchedJSON, err := jsonPatch.Apply(currentJSON) + Ω(err).ShouldNot(HaveOccurred()) + Ω(patchedJSON).Should(MatchJSON(expectedJSON)) + }) + It("string prefix", func() { + modified := F{B: &B{Bool: true, Str: "str"}} + current := F{} + expected := F{B: &B{Bool: true, Str: "str"}} + + currentJSON, err := json.Marshal(current) + Ω(err).ShouldNot(HaveOccurred()) + _, err = json.Marshal(modified) + Ω(err).ShouldNot(HaveOccurred()) + expectedJSON, err := json.Marshal(expected) + Ω(err).ShouldNot(HaveOccurred()) + + bytes, _, err := jsonpatch.CreateJSONPatch(modified.B, current.B, jsonpatch.WithPrefix([]string{"b"})) + Ω(err).ShouldNot(HaveOccurred()) + Ω(bytes.String()).ShouldNot(Equal("")) + jsonPatch, err := jsonpatch2.DecodePatch(bytes) + Ω(err).ShouldNot(HaveOccurred()) + patchedJSON, err := jsonPatch.Apply(currentJSON) + Ω(err).ShouldNot(HaveOccurred()) + Ω(patchedJSON).Should(MatchJSON(expectedJSON)) + }) + }) Context("CreateJsonPatch_errors", func() { It("not matching types", func() { _, _, err := jsonpatch.CreateJSONPatch(A{}, B{})