@@ -3,6 +3,7 @@ package jsonpatch_test
3
3
import (
4
4
"encoding/json"
5
5
"strconv"
6
+ "strings"
6
7
7
8
. "github.com/onsi/ginkgo"
8
9
. "github.com/onsi/gomega"
@@ -325,6 +326,7 @@ var _ = Describe("JSONPatch", func() {
325
326
testPatchWithExpected (F {B : & B {Bool : true , Str : "str" }}, F {}, F {B : & B {Bool : true , Str : "str" }}, jsonpatch .WithPrefix ([]string {"" }))
326
327
})
327
328
It ("pointer prefix" , func () {
329
+ prefix := "/a/ptr"
328
330
modified := F {A : & A {B : & B {Bool : true , Str : "str" }}}
329
331
current := F {A : & A {}}
330
332
expected := F {A : & A {B : & B {Bool : true , Str : "str" }}}
@@ -336,16 +338,18 @@ var _ = Describe("JSONPatch", func() {
336
338
expectedJSON , err := json .Marshal (expected )
337
339
Ω (err ).ShouldNot (HaveOccurred ())
338
340
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 )))
340
342
Ω (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 ())
343
346
Ω (err ).ShouldNot (HaveOccurred ())
344
347
patchedJSON , err := jsonPatch .Apply (currentJSON )
345
348
Ω (err ).ShouldNot (HaveOccurred ())
346
349
Ω (patchedJSON ).Should (MatchJSON (expectedJSON ))
347
350
})
348
351
It ("string prefix" , func () {
352
+ prefix := []string {"b" }
349
353
modified := F {B : & B {Bool : true , Str : "str" }}
350
354
current := F {}
351
355
expected := F {B : & B {Bool : true , Str : "str" }}
@@ -357,10 +361,11 @@ var _ = Describe("JSONPatch", func() {
357
361
expectedJSON , err := json .Marshal (expected )
358
362
Ω (err ).ShouldNot (HaveOccurred ())
359
363
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 ))
361
365
Ω (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 ())
364
369
Ω (err ).ShouldNot (HaveOccurred ())
365
370
patchedJSON , err := jsonPatch .Apply (currentJSON )
366
371
Ω (err ).ShouldNot (HaveOccurred ())
@@ -369,25 +374,25 @@ var _ = Describe("JSONPatch", func() {
369
374
})
370
375
Context ("CreateJsonPatch_errors" , func () {
371
376
It ("not matching types" , func () {
372
- _ , _ , err := jsonpatch .CreateJSONPatch (A {}, B {})
377
+ _ , err := jsonpatch .CreateJSONPatch (A {}, B {})
373
378
Ω (err ).Should (HaveOccurred ())
374
379
})
375
380
It ("not matching interface types" , func () {
376
- _ , _ , err := jsonpatch .CreateJSONPatch (G {1 }, G {"str" })
381
+ _ , err := jsonpatch .CreateJSONPatch (G {1 }, G {"str" })
377
382
Ω (err ).Should (HaveOccurred ())
378
383
})
379
384
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 }})
381
386
Ω (err ).Should (HaveOccurred ())
382
387
})
383
388
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" }})
385
390
Ω (err ).Should (HaveOccurred ())
386
391
})
387
392
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 ())
389
394
Ω (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 ())
391
396
Ω (err ).Should (HaveOccurred ())
392
397
})
393
398
})
@@ -418,19 +423,19 @@ var _ = Describe("JSONPatch", func() {
418
423
modifiedJSON , err := json .Marshal (modified )
419
424
Ω (err ).ShouldNot (HaveOccurred ())
420
425
421
- var bytes jsonpatch.Patch
422
- var changes int
426
+ var list jsonpatch.JSONPatchList
423
427
_ = b .Time ("runtime" , func () {
424
- bytes , changes , err = jsonpatch .CreateJSONPatch (modified , current )
428
+ list , err = jsonpatch .CreateJSONPatch (modified , current )
425
429
})
426
430
Ω (err ).ShouldNot (HaveOccurred ())
427
- if bytes .Empty () {
431
+ if list .Empty () {
428
432
Ω (currentJSON ).Should (MatchJSON (modifiedJSON ))
429
- Ω (changes ).Should (Equal (0 ))
433
+ Ω (list .Len ()).Should (Equal (0 ))
434
+
430
435
return
431
436
}
432
437
433
- jsonPatch , err := jsonpatch2 .DecodePatch (bytes )
438
+ jsonPatch , err := jsonpatch2 .DecodePatch (list . Raw () )
434
439
Ω (err ).ShouldNot (HaveOccurred ())
435
440
patchedJSON , err := jsonPatch .Apply (currentJSON )
436
441
Ω (err ).ShouldNot (HaveOccurred ())
@@ -445,18 +450,18 @@ func testPatch(modified, current interface{}) {
445
450
modifiedJSON , err := json .Marshal (modified )
446
451
Ω (err ).ShouldNot (HaveOccurred ())
447
452
448
- bytes , changes , err := jsonpatch .CreateJSONPatch (modified , current )
453
+ list , err := jsonpatch .CreateJSONPatch (modified , current )
449
454
Ω (err ).ShouldNot (HaveOccurred ())
450
- if bytes .Empty () {
455
+ if list .Empty () {
451
456
Ω (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 ("" ))
454
459
455
460
return
456
461
}
457
462
458
- Ω (bytes .String ()).ShouldNot (Equal ("" ))
459
- jsonPatch , err := jsonpatch2 .DecodePatch (bytes )
463
+ Ω (list .String ()).ShouldNot (Equal ("" ))
464
+ jsonPatch , err := jsonpatch2 .DecodePatch (list . Raw () )
460
465
Ω (err ).ShouldNot (HaveOccurred ())
461
466
patchedJSON , err := jsonPatch .Apply (currentJSON )
462
467
Ω (err ).ShouldNot (HaveOccurred ())
@@ -471,18 +476,18 @@ func testPatchWithExpected(modified, current, expected interface{}, options ...j
471
476
expectedJSON , err := json .Marshal (expected )
472
477
Ω (err ).ShouldNot (HaveOccurred ())
473
478
474
- bytes , changes , err := jsonpatch .CreateJSONPatch (modified , current , options ... )
479
+ list , err := jsonpatch .CreateJSONPatch (modified , current , options ... )
475
480
Ω (err ).ShouldNot (HaveOccurred ())
476
- if bytes .Empty () {
481
+ if list .Empty () {
477
482
Ω (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 ("" ))
480
485
481
486
return
482
487
}
483
488
484
- Ω (bytes .String ()).ShouldNot (Equal ("" ))
485
- jsonPatch , err := jsonpatch2 .DecodePatch (bytes )
489
+ Ω (list .String ()).ShouldNot (Equal ("" ))
490
+ jsonPatch , err := jsonpatch2 .DecodePatch (list . Raw () )
486
491
Ω (err ).ShouldNot (HaveOccurred ())
487
492
patchedJSON , err := jsonPatch .Apply (currentJSON )
488
493
Ω (err ).ShouldNot (HaveOccurred ())
0 commit comments