Skip to content

Commit

Permalink
Add tests for WithPrefix option
Browse files Browse the repository at this point in the history
  • Loading branch information
snorwin committed Feb 7, 2021
1 parent d6faa23 commit 9b5c3af
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
20 changes: 10 additions & 10 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down
47 changes: 47 additions & 0 deletions patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down

0 comments on commit 9b5c3af

Please sign in to comment.