Skip to content

Commit 9b5c3af

Browse files
committed
Add tests for WithPrefix option
1 parent d6faa23 commit 9b5c3af

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

handler.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,49 @@ package jsonpatch
33
// Handler is the interfaces used by the walker to create patches
44
type Handler interface {
55
// Add creates a JSONPatch with an 'add' operation and appends it to the patch list
6-
Add(path JSONPointer, modified interface{}) []JSONPatch
6+
Add(pointer JSONPointer, modified interface{}) []JSONPatch
77

88
// Remove creates a JSONPatch with an 'remove' operation and appends it to the patch list
9-
Remove(path JSONPointer, current interface{}) []JSONPatch
9+
Remove(pointer JSONPointer, current interface{}) []JSONPatch
1010

1111
// Replace creates a JSONPatch with an 'replace' operation and appends it to the patch list
12-
Replace(path JSONPointer, modified, current interface{}) []JSONPatch
12+
Replace(pointer JSONPointer, modified, current interface{}) []JSONPatch
1313
}
1414

1515
// DefaultHandler implements the Handler
1616
type DefaultHandler struct{}
1717

1818
// Add implements Handler
19-
func (h *DefaultHandler) Add(path JSONPointer, value interface{}) []JSONPatch {
19+
func (h *DefaultHandler) Add(pointer JSONPointer, value interface{}) []JSONPatch {
2020
// The 'add' operation either inserts a value into the array at the specified index or adds a new member to the object
2121
// NOTE: If the target location specifies an object member that does exist, that member's value is replaced
2222
return []JSONPatch{
2323
{
2424
Operation: "add",
25-
Path: path.String(),
25+
Path: pointer.String(),
2626
Value: value,
2727
},
2828
}
2929
}
3030

3131
// Remove implements Handler
32-
func (h *DefaultHandler) Remove(path JSONPointer, _ interface{}) []JSONPatch {
33-
// The 'remove' operation removes the value at the target location (specified by the path)
32+
func (h *DefaultHandler) Remove(pointer JSONPointer, _ interface{}) []JSONPatch {
33+
// The 'remove' operation removes the value at the target location (specified by the pointer)
3434
return []JSONPatch{
3535
{
3636
Operation: "remove",
37-
Path: path.String(),
37+
Path: pointer.String(),
3838
},
3939
}
4040
}
4141

4242
// Replace implements Handler
43-
func (h *DefaultHandler) Replace(path JSONPointer, value, _ interface{}) []JSONPatch {
43+
func (h *DefaultHandler) Replace(pointer JSONPointer, value, _ interface{}) []JSONPatch {
4444
// The 'replace' operation replaces the value at the target location with a new value
4545
return []JSONPatch{
4646
{
4747
Operation: "replace",
48-
Path: path.String(),
48+
Path: pointer.String(),
4949
Value: value,
5050
},
5151
}

patch_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,53 @@ var _ = Describe("JSONPatch", func() {
320320
testPatchWithExpected(F{}, F{B: &B{Str: "don't remove me"}}, F{B: &B{Str: "don't remove me"}}, jsonpatch.WithPredicate(predicate))
321321
})
322322
})
323+
Context("CreateJsonPatch_with_prefix", func() {
324+
It("empty prefix", func() {
325+
testPatchWithExpected(F{B: &B{Bool: true, Str: "str"}}, F{}, F{B: &B{Bool: true, Str: "str"}}, jsonpatch.WithPrefix([]string{""}))
326+
})
327+
It("pointer prefix", func() {
328+
modified := F{A: &A{B: &B{Bool: true, Str: "str"}}}
329+
current := F{A: &A{}}
330+
expected := F{A: &A{B: &B{Bool: true, Str: "str"}}}
331+
332+
currentJSON, err := json.Marshal(current)
333+
Ω(err).ShouldNot(HaveOccurred())
334+
_, err = json.Marshal(modified)
335+
Ω(err).ShouldNot(HaveOccurred())
336+
expectedJSON, err := json.Marshal(expected)
337+
Ω(err).ShouldNot(HaveOccurred())
338+
339+
bytes, _, err := jsonpatch.CreateJSONPatch(modified.A.B, current.A.B, jsonpatch.WithPrefix(jsonpatch.ParseJSONPointer("/a/ptr")))
340+
Ω(err).ShouldNot(HaveOccurred())
341+
Ω(bytes.String()).ShouldNot(Equal(""))
342+
jsonPatch, err := jsonpatch2.DecodePatch(bytes)
343+
Ω(err).ShouldNot(HaveOccurred())
344+
patchedJSON, err := jsonPatch.Apply(currentJSON)
345+
Ω(err).ShouldNot(HaveOccurred())
346+
Ω(patchedJSON).Should(MatchJSON(expectedJSON))
347+
})
348+
It("string prefix", func() {
349+
modified := F{B: &B{Bool: true, Str: "str"}}
350+
current := F{}
351+
expected := F{B: &B{Bool: true, Str: "str"}}
352+
353+
currentJSON, err := json.Marshal(current)
354+
Ω(err).ShouldNot(HaveOccurred())
355+
_, err = json.Marshal(modified)
356+
Ω(err).ShouldNot(HaveOccurred())
357+
expectedJSON, err := json.Marshal(expected)
358+
Ω(err).ShouldNot(HaveOccurred())
359+
360+
bytes, _, err := jsonpatch.CreateJSONPatch(modified.B, current.B, jsonpatch.WithPrefix([]string{"b"}))
361+
Ω(err).ShouldNot(HaveOccurred())
362+
Ω(bytes.String()).ShouldNot(Equal(""))
363+
jsonPatch, err := jsonpatch2.DecodePatch(bytes)
364+
Ω(err).ShouldNot(HaveOccurred())
365+
patchedJSON, err := jsonPatch.Apply(currentJSON)
366+
Ω(err).ShouldNot(HaveOccurred())
367+
Ω(patchedJSON).Should(MatchJSON(expectedJSON))
368+
})
369+
})
323370
Context("CreateJsonPatch_errors", func() {
324371
It("not matching types", func() {
325372
_, _, err := jsonpatch.CreateJSONPatch(A{}, B{})

0 commit comments

Comments
 (0)