@@ -3,6 +3,7 @@ package jsonpatch_test
33import (
44 "encoding/json"
55 "strconv"
6+ "strings"
67
78 . "github.com/onsi/ginkgo"
89 . "github.com/onsi/gomega"
@@ -325,6 +326,7 @@ var _ = Describe("JSONPatch", func() {
325326 testPatchWithExpected (F {B : & B {Bool : true , Str : "str" }}, F {}, F {B : & B {Bool : true , Str : "str" }}, jsonpatch .WithPrefix ([]string {"" }))
326327 })
327328 It ("pointer prefix" , func () {
329+ prefix := "/a/ptr"
328330 modified := F {A : & A {B : & B {Bool : true , Str : "str" }}}
329331 current := F {A : & A {}}
330332 expected := F {A : & A {B : & B {Bool : true , Str : "str" }}}
@@ -336,16 +338,18 @@ var _ = Describe("JSONPatch", func() {
336338 expectedJSON , err := json .Marshal (expected )
337339 Ω (err ).ShouldNot (HaveOccurred ())
338340
339- bytes , _ , err := jsonpatch .CreateJSONPatch (modified .A .B , current .A .B , jsonpatch .WithPrefix (jsonpatch .ParseJSONPointer ("/a/ptr" )))
341+ list , err := jsonpatch .CreateJSONPatch (modified .A .B , current .A .B , jsonpatch .WithPrefix (jsonpatch .ParseJSONPointer (prefix )))
340342 Ω (err ).ShouldNot (HaveOccurred ())
341- Ω (bytes .String ()).ShouldNot (Equal ("" ))
342- jsonPatch , err := jsonpatch2 .DecodePatch (bytes )
343+ Ω (list .String ()).ShouldNot (Equal ("" ))
344+ Ω (list .List ()).Should (ContainElement (WithTransform (func (p jsonpatch.JSONPatch ) string { return p .Path }, HavePrefix (prefix ))))
345+ jsonPatch , err := jsonpatch2 .DecodePatch (list .Raw ())
343346 Ω (err ).ShouldNot (HaveOccurred ())
344347 patchedJSON , err := jsonPatch .Apply (currentJSON )
345348 Ω (err ).ShouldNot (HaveOccurred ())
346349 Ω (patchedJSON ).Should (MatchJSON (expectedJSON ))
347350 })
348351 It ("string prefix" , func () {
352+ prefix := []string {"b" }
349353 modified := F {B : & B {Bool : true , Str : "str" }}
350354 current := F {}
351355 expected := F {B : & B {Bool : true , Str : "str" }}
@@ -357,10 +361,11 @@ var _ = Describe("JSONPatch", func() {
357361 expectedJSON , err := json .Marshal (expected )
358362 Ω (err ).ShouldNot (HaveOccurred ())
359363
360- bytes , _ , err := jsonpatch .CreateJSONPatch (modified .B , current .B , jsonpatch .WithPrefix ([] string { "b" } ))
364+ list , err := jsonpatch .CreateJSONPatch (modified .B , current .B , jsonpatch .WithPrefix (prefix ))
361365 Ω (err ).ShouldNot (HaveOccurred ())
362- Ω (bytes .String ()).ShouldNot (Equal ("" ))
363- jsonPatch , err := jsonpatch2 .DecodePatch (bytes )
366+ Ω (list .String ()).ShouldNot (Equal ("" ))
367+ Ω (list .List ()).Should (ContainElement (WithTransform (func (p jsonpatch.JSONPatch ) string { return p .Path }, HavePrefix ("/" + strings .Join (prefix , "/" )))))
368+ jsonPatch , err := jsonpatch2 .DecodePatch (list .Raw ())
364369 Ω (err ).ShouldNot (HaveOccurred ())
365370 patchedJSON , err := jsonPatch .Apply (currentJSON )
366371 Ω (err ).ShouldNot (HaveOccurred ())
@@ -369,25 +374,25 @@ var _ = Describe("JSONPatch", func() {
369374 })
370375 Context ("CreateJsonPatch_errors" , func () {
371376 It ("not matching types" , func () {
372- _ , _ , err := jsonpatch .CreateJSONPatch (A {}, B {})
377+ _ , err := jsonpatch .CreateJSONPatch (A {}, B {})
373378 Ω (err ).Should (HaveOccurred ())
374379 })
375380 It ("not matching interface types" , func () {
376- _ , _ , err := jsonpatch .CreateJSONPatch (G {1 }, G {"str" })
381+ _ , err := jsonpatch .CreateJSONPatch (G {1 }, G {"str" })
377382 Ω (err ).Should (HaveOccurred ())
378383 })
379384 It ("invalid map (map[string]int)" , func () {
380- _ , _ , err := jsonpatch .CreateJSONPatch (G {map [string ]int {"key" : 2 }}, G {map [string ]int {"key" : 3 }})
385+ _ , err := jsonpatch .CreateJSONPatch (G {map [string ]int {"key" : 2 }}, G {map [string ]int {"key" : 3 }})
381386 Ω (err ).Should (HaveOccurred ())
382387 })
383388 It ("invalid map (map[int]string)" , func () {
384- _ , _ , err := jsonpatch .CreateJSONPatch (G {map [int ]string {1 : "value" }}, G {map [int ]string {2 : "value" }})
389+ _ , err := jsonpatch .CreateJSONPatch (G {map [int ]string {1 : "value" }}, G {map [int ]string {2 : "value" }})
385390 Ω (err ).Should (HaveOccurred ())
386391 })
387392 It ("ignore slice order failed (duplicated key)" , func () {
388- _ , _ , err := jsonpatch .CreateJSONPatch ([]int {1 , 1 , 1 , 1 }, []int {1 , 2 , 3 }, jsonpatch .IgnoreSliceOrder ())
393+ _ , err := jsonpatch .CreateJSONPatch ([]int {1 , 1 , 1 , 1 }, []int {1 , 2 , 3 }, jsonpatch .IgnoreSliceOrder ())
389394 Ω (err ).Should (HaveOccurred ())
390- _ , _ , err = jsonpatch .CreateJSONPatch ([]string {"1" , "2" , "3" }, []string {"1" , "1" }, jsonpatch .IgnoreSliceOrder ())
395+ _ , err = jsonpatch .CreateJSONPatch ([]string {"1" , "2" , "3" }, []string {"1" , "1" }, jsonpatch .IgnoreSliceOrder ())
391396 Ω (err ).Should (HaveOccurred ())
392397 })
393398 })
@@ -418,19 +423,19 @@ var _ = Describe("JSONPatch", func() {
418423 modifiedJSON , err := json .Marshal (modified )
419424 Ω (err ).ShouldNot (HaveOccurred ())
420425
421- var bytes jsonpatch.Patch
422- var changes int
426+ var list jsonpatch.JSONPatchList
423427 _ = b .Time ("runtime" , func () {
424- bytes , changes , err = jsonpatch .CreateJSONPatch (modified , current )
428+ list , err = jsonpatch .CreateJSONPatch (modified , current )
425429 })
426430 Ω (err ).ShouldNot (HaveOccurred ())
427- if bytes .Empty () {
431+ if list .Empty () {
428432 Ω (currentJSON ).Should (MatchJSON (modifiedJSON ))
429- Ω (changes ).Should (Equal (0 ))
433+ Ω (list .Len ()).Should (Equal (0 ))
434+
430435 return
431436 }
432437
433- jsonPatch , err := jsonpatch2 .DecodePatch (bytes )
438+ jsonPatch , err := jsonpatch2 .DecodePatch (list . Raw () )
434439 Ω (err ).ShouldNot (HaveOccurred ())
435440 patchedJSON , err := jsonPatch .Apply (currentJSON )
436441 Ω (err ).ShouldNot (HaveOccurred ())
@@ -445,18 +450,18 @@ func testPatch(modified, current interface{}) {
445450 modifiedJSON , err := json .Marshal (modified )
446451 Ω (err ).ShouldNot (HaveOccurred ())
447452
448- bytes , changes , err := jsonpatch .CreateJSONPatch (modified , current )
453+ list , err := jsonpatch .CreateJSONPatch (modified , current )
449454 Ω (err ).ShouldNot (HaveOccurred ())
450- if bytes .Empty () {
455+ if list .Empty () {
451456 Ω (currentJSON ).Should (MatchJSON (modifiedJSON ))
452- Ω (changes ).Should (Equal (0 ))
453- Ω (bytes .String ()).Should (Equal ("" ))
457+ Ω (list . Len () ).Should (Equal (0 ))
458+ Ω (list .String ()).Should (Equal ("" ))
454459
455460 return
456461 }
457462
458- Ω (bytes .String ()).ShouldNot (Equal ("" ))
459- jsonPatch , err := jsonpatch2 .DecodePatch (bytes )
463+ Ω (list .String ()).ShouldNot (Equal ("" ))
464+ jsonPatch , err := jsonpatch2 .DecodePatch (list . Raw () )
460465 Ω (err ).ShouldNot (HaveOccurred ())
461466 patchedJSON , err := jsonPatch .Apply (currentJSON )
462467 Ω (err ).ShouldNot (HaveOccurred ())
@@ -471,18 +476,18 @@ func testPatchWithExpected(modified, current, expected interface{}, options ...j
471476 expectedJSON , err := json .Marshal (expected )
472477 Ω (err ).ShouldNot (HaveOccurred ())
473478
474- bytes , changes , err := jsonpatch .CreateJSONPatch (modified , current , options ... )
479+ list , err := jsonpatch .CreateJSONPatch (modified , current , options ... )
475480 Ω (err ).ShouldNot (HaveOccurred ())
476- if bytes .Empty () {
481+ if list .Empty () {
477482 Ω (currentJSON ).Should (MatchJSON (expectedJSON ))
478- Ω (changes ).Should (Equal (0 ))
479- Ω (bytes .String ()).Should (Equal ("" ))
483+ Ω (list . Len () ).Should (Equal (0 ))
484+ Ω (list .String ()).Should (Equal ("" ))
480485
481486 return
482487 }
483488
484- Ω (bytes .String ()).ShouldNot (Equal ("" ))
485- jsonPatch , err := jsonpatch2 .DecodePatch (bytes )
489+ Ω (list .String ()).ShouldNot (Equal ("" ))
490+ jsonPatch , err := jsonpatch2 .DecodePatch (list . Raw () )
486491 Ω (err ).ShouldNot (HaveOccurred ())
487492 patchedJSON , err := jsonPatch .Apply (currentJSON )
488493 Ω (err ).ShouldNot (HaveOccurred ())
0 commit comments