@@ -3,11 +3,10 @@ package jsonpatch
3
3
import (
4
4
"bytes"
5
5
"encoding/json"
6
+ "errors"
6
7
"fmt"
7
8
"strconv"
8
9
"strings"
9
-
10
- "github.com/pkg/errors"
11
10
)
12
11
13
12
const (
@@ -277,7 +276,7 @@ func (o Operation) Path() (string, error) {
277
276
return op , nil
278
277
}
279
278
280
- return "unknown" , errors . Wrapf ( ErrMissing , "operation missing path field" )
279
+ return "unknown" , fmt . Errorf ( "operation missing path field: %w" , ErrMissing )
281
280
}
282
281
283
282
// From reads the "from" field of the Operation.
@@ -294,7 +293,7 @@ func (o Operation) From() (string, error) {
294
293
return op , nil
295
294
}
296
295
297
- return "unknown" , errors . Wrapf ( ErrMissing , "operation, missing from field" )
296
+ return "unknown" , fmt . Errorf ( "operation, missing from field: %w" , ErrMissing )
298
297
}
299
298
300
299
func (o Operation ) value () * lazyNode {
@@ -319,7 +318,7 @@ func (o Operation) ValueInterface() (interface{}, error) {
319
318
return v , nil
320
319
}
321
320
322
- return nil , errors . Wrapf ( ErrMissing , "operation, missing value field" )
321
+ return nil , fmt . Errorf ( "operation, missing value field: %w" , ErrMissing )
323
322
}
324
323
325
324
func isArray (buf []byte ) bool {
@@ -398,7 +397,7 @@ func (d *partialDoc) get(key string) (*lazyNode, error) {
398
397
func (d * partialDoc ) remove (key string ) error {
399
398
_ , ok := (* d )[key ]
400
399
if ! ok {
401
- return errors . Wrapf ( ErrMissing , "Unable to remove nonexistent key: %s" , key )
400
+ return fmt . Errorf ( "Unable to remove nonexistent key: %s: %w " , key , ErrMissing )
402
401
}
403
402
404
403
delete (* d , key )
@@ -415,10 +414,10 @@ func (d *partialArray) set(key string, val *lazyNode) error {
415
414
416
415
if idx < 0 {
417
416
if ! SupportNegativeIndices {
418
- return errors . Wrapf ( ErrInvalidIndex , "Unable to access invalid index: %d" , idx )
417
+ return fmt . Errorf ( "Unable to access invalid index: %d : %w " , idx , ErrInvalidIndex )
419
418
}
420
419
if idx < - len (* d ) {
421
- return errors . Wrapf ( ErrInvalidIndex , "Unable to access invalid index: %d" , idx )
420
+ return fmt . Errorf ( "Unable to access invalid index: %d: %w " , idx , ErrInvalidIndex )
422
421
}
423
422
idx += len (* d )
424
423
}
@@ -435,7 +434,7 @@ func (d *partialArray) add(key string, val *lazyNode) error {
435
434
436
435
idx , err := strconv .Atoi (key )
437
436
if err != nil {
438
- return errors . Wrapf ( err , "value was not a proper array index: '%s'" , key )
437
+ return fmt . Errorf ( "value was not a proper array index: '%s': %w " , key , err )
439
438
}
440
439
441
440
sz := len (* d ) + 1
@@ -445,15 +444,15 @@ func (d *partialArray) add(key string, val *lazyNode) error {
445
444
cur := * d
446
445
447
446
if idx >= len (ary ) {
448
- return errors . Wrapf ( ErrInvalidIndex , "Unable to access invalid index: %d" , idx )
447
+ return fmt . Errorf ( "Unable to access invalid index: %d: %w " , idx , ErrInvalidIndex )
449
448
}
450
449
451
450
if idx < 0 {
452
451
if ! SupportNegativeIndices {
453
- return errors . Wrapf ( ErrInvalidIndex , "Unable to access invalid index: %d" , idx )
452
+ return fmt . Errorf ( "Unable to access invalid index: %d: %w " , idx , ErrInvalidIndex )
454
453
}
455
454
if idx < - len (ary ) {
456
- return errors . Wrapf ( ErrInvalidIndex , "Unable to access invalid index: %d" , idx )
455
+ return fmt . Errorf ( "Unable to access invalid index: %d: %w " , idx , ErrInvalidIndex )
457
456
}
458
457
idx += len (ary )
459
458
}
@@ -475,16 +474,16 @@ func (d *partialArray) get(key string) (*lazyNode, error) {
475
474
476
475
if idx < 0 {
477
476
if ! SupportNegativeIndices {
478
- return nil , errors . Wrapf ( ErrInvalidIndex , "Unable to access invalid index: %d" , idx )
477
+ return nil , fmt . Errorf ( "Unable to access invalid index: %d: %w " , idx , ErrInvalidIndex )
479
478
}
480
479
if idx < - len (* d ) {
481
- return nil , errors . Wrapf ( ErrInvalidIndex , "Unable to access invalid index: %d" , idx )
480
+ return nil , fmt . Errorf ( "Unable to access invalid index: %d: %w " , idx , ErrInvalidIndex )
482
481
}
483
482
idx += len (* d )
484
483
}
485
484
486
485
if idx >= len (* d ) {
487
- return nil , errors . Wrapf ( ErrInvalidIndex , "Unable to access invalid index: %d" , idx )
486
+ return nil , fmt . Errorf ( "Unable to access invalid index: %d: %w " , idx , ErrInvalidIndex )
488
487
}
489
488
490
489
return (* d )[idx ], nil
@@ -499,15 +498,15 @@ func (d *partialArray) remove(key string) error {
499
498
cur := * d
500
499
501
500
if idx >= len (cur ) {
502
- return errors . Wrapf ( ErrInvalidIndex , "Unable to access invalid index: %d" , idx )
501
+ return fmt . Errorf ( "Unable to access invalid index: %d: %w " , idx , ErrInvalidIndex )
503
502
}
504
503
505
504
if idx < 0 {
506
505
if ! SupportNegativeIndices {
507
- return errors . Wrapf ( ErrInvalidIndex , "Unable to access invalid index: %d" , idx )
506
+ return fmt . Errorf ( "Unable to access invalid index: %d: %w " , idx , ErrInvalidIndex )
508
507
}
509
508
if idx < - len (cur ) {
510
- return errors . Wrapf ( ErrInvalidIndex , "Unable to access invalid index: %d" , idx )
509
+ return fmt . Errorf ( "Unable to access invalid index: %d: %w " , idx , ErrInvalidIndex )
511
510
}
512
511
idx += len (cur )
513
512
}
@@ -525,18 +524,18 @@ func (d *partialArray) remove(key string) error {
525
524
func (p Patch ) add (doc * container , op Operation ) error {
526
525
path , err := op .Path ()
527
526
if err != nil {
528
- return errors . Wrapf ( ErrMissing , "add operation failed to decode path" )
527
+ return fmt . Errorf ( "add operation failed to decode path: %w" , ErrMissing )
529
528
}
530
529
531
530
con , key := findObject (doc , path )
532
531
533
532
if con == nil {
534
- return errors . Wrapf ( ErrMissing , "add operation does not apply: doc is missing path: \" %s\" " , path )
533
+ return fmt . Errorf ( "add operation does not apply: doc is missing path: \" %s\" : %w " , path , ErrMissing )
535
534
}
536
535
537
536
err = con .add (key , op .value ())
538
537
if err != nil {
539
- return errors . Wrapf ( err , "error in add for path: '%s'" , path )
538
+ return fmt . Errorf ( "error in add for path: '%s': %w " , path , err )
540
539
}
541
540
542
541
return nil
@@ -545,18 +544,18 @@ func (p Patch) add(doc *container, op Operation) error {
545
544
func (p Patch ) remove (doc * container , op Operation ) error {
546
545
path , err := op .Path ()
547
546
if err != nil {
548
- return errors . Wrapf ( ErrMissing , "remove operation failed to decode path" )
547
+ return fmt . Errorf ( "remove operation failed to decode path: %w" , ErrMissing )
549
548
}
550
549
551
550
con , key := findObject (doc , path )
552
551
553
552
if con == nil {
554
- return errors . Wrapf ( ErrMissing , "remove operation does not apply: doc is missing path: \" %s\" " , path )
553
+ return fmt . Errorf ( "remove operation does not apply: doc is missing path: \" %s\" : %w " , path , ErrMissing )
555
554
}
556
555
557
556
err = con .remove (key )
558
557
if err != nil {
559
- return errors . Wrapf ( err , "error in remove for path: '%s'" , path )
558
+ return fmt . Errorf ( "error in remove for path: '%s': %w " , path , err )
560
559
}
561
560
562
561
return nil
@@ -565,7 +564,7 @@ func (p Patch) remove(doc *container, op Operation) error {
565
564
func (p Patch ) replace (doc * container , op Operation ) error {
566
565
path , err := op .Path ()
567
566
if err != nil {
568
- return errors . Wrapf ( err , "replace operation failed to decode path" )
567
+ return fmt . Errorf ( "replace operation failed to decode path: %w" , err )
569
568
}
570
569
571
570
if path == "" {
@@ -574,7 +573,7 @@ func (p Patch) replace(doc *container, op Operation) error {
574
573
if val .which == eRaw {
575
574
if ! val .tryDoc () {
576
575
if ! val .tryAry () {
577
- return errors . Wrapf ( err , "replace operation value must be object or array" )
576
+ return fmt . Errorf ( "replace operation value must be object or array: %w" , err )
578
577
}
579
578
}
580
579
}
@@ -585,7 +584,7 @@ func (p Patch) replace(doc *container, op Operation) error {
585
584
case eDoc :
586
585
* doc = & val .doc
587
586
case eRaw :
588
- return errors . Wrapf ( err , "replace operation hit impossible case" )
587
+ return fmt . Errorf ( "replace operation hit impossible case: %w" , err )
589
588
}
590
589
591
590
return nil
@@ -594,17 +593,17 @@ func (p Patch) replace(doc *container, op Operation) error {
594
593
con , key := findObject (doc , path )
595
594
596
595
if con == nil {
597
- return errors . Wrapf ( ErrMissing , "replace operation does not apply: doc is missing path: %s" , path )
596
+ return fmt . Errorf ( "replace operation does not apply: doc is missing path: %s: %w " , path , ErrMissing )
598
597
}
599
598
600
599
_ , ok := con .get (key )
601
600
if ok != nil {
602
- return errors . Wrapf ( ErrMissing , "replace operation does not apply: doc is missing key: %s" , path )
601
+ return fmt . Errorf ( "replace operation does not apply: doc is missing key: %s: %w " , path , ErrMissing )
603
602
}
604
603
605
604
err = con .set (key , op .value ())
606
605
if err != nil {
607
- return errors . Wrapf ( err , "error in remove for path: '%s'" , path )
606
+ return fmt . Errorf ( "error in remove for path: '%s': %w " , path , err )
608
607
}
609
608
610
609
return nil
@@ -613,39 +612,39 @@ func (p Patch) replace(doc *container, op Operation) error {
613
612
func (p Patch ) move (doc * container , op Operation ) error {
614
613
from , err := op .From ()
615
614
if err != nil {
616
- return errors . Wrapf ( err , "move operation failed to decode from" )
615
+ return fmt . Errorf ( "move operation failed to decode from: %w" , err )
617
616
}
618
617
619
618
con , key := findObject (doc , from )
620
619
621
620
if con == nil {
622
- return errors . Wrapf ( ErrMissing , "move operation does not apply: doc is missing from path: %s" , from )
621
+ return fmt . Errorf ( "move operation does not apply: doc is missing from path: %s: %w " , from , ErrMissing )
623
622
}
624
623
625
624
val , err := con .get (key )
626
625
if err != nil {
627
- return errors . Wrapf ( err , "error in move for path: '%s'" , key )
626
+ return fmt . Errorf ( "error in move for path: '%s': %w " , key , err )
628
627
}
629
628
630
629
err = con .remove (key )
631
630
if err != nil {
632
- return errors . Wrapf ( err , "error in move for path: '%s'" , key )
631
+ return fmt . Errorf ( "error in move for path: '%s': %w " , key , err )
633
632
}
634
633
635
634
path , err := op .Path ()
636
635
if err != nil {
637
- return errors . Wrapf ( err , "move operation failed to decode path" )
636
+ return fmt . Errorf ( "move operation failed to decode path: %w" , err )
638
637
}
639
638
640
639
con , key = findObject (doc , path )
641
640
642
641
if con == nil {
643
- return errors . Wrapf ( ErrMissing , "move operation does not apply: doc is missing destination path: %s" , path )
642
+ return fmt . Errorf ( "move operation does not apply: doc is missing destination path: %s: %w " , path , ErrMissing )
644
643
}
645
644
646
645
err = con .add (key , val )
647
646
if err != nil {
648
- return errors . Wrapf ( err , "error in move for path: '%s'" , path )
647
+ return fmt . Errorf ( "error in move for path: '%s': %w " , path , err )
649
648
}
650
649
651
650
return nil
@@ -654,7 +653,7 @@ func (p Patch) move(doc *container, op Operation) error {
654
653
func (p Patch ) test (doc * container , op Operation ) error {
655
654
path , err := op .Path ()
656
655
if err != nil {
657
- return errors . Wrapf ( err , "test operation failed to decode path" )
656
+ return fmt . Errorf ( "test operation failed to decode path: %w" , err )
658
657
}
659
658
660
659
if path == "" {
@@ -673,67 +672,67 @@ func (p Patch) test(doc *container, op Operation) error {
673
672
return nil
674
673
}
675
674
676
- return errors . Wrapf ( ErrTestFailed , "testing value %s failed" , path )
675
+ return fmt . Errorf ( "testing value %s failed: %w " , path , ErrTestFailed )
677
676
}
678
677
679
678
con , key := findObject (doc , path )
680
679
681
680
if con == nil {
682
- return errors . Wrapf ( ErrMissing , "test operation does not apply: is missing path: %s" , path )
681
+ return fmt . Errorf ( "test operation does not apply: is missing path: %s: %w " , path , ErrMissing )
683
682
}
684
683
685
684
val , err := con .get (key )
686
685
if err != nil {
687
- return errors . Wrapf ( err , "error in test for path: '%s'" , path )
686
+ return fmt . Errorf ( "error in test for path: '%s': %w " , path , err )
688
687
}
689
688
690
689
if val == nil {
691
690
if op .value () == nil || op .value ().raw == nil {
692
691
return nil
693
692
}
694
- return errors . Wrapf ( ErrTestFailed , "testing value %s failed" , path )
693
+ return fmt . Errorf ( "testing value %s failed: %w " , path , ErrTestFailed )
695
694
} else if op .value () == nil {
696
- return errors . Wrapf ( ErrTestFailed , "testing value %s failed" , path )
695
+ return fmt . Errorf ( "testing value %s failed: %w " , path , ErrTestFailed )
697
696
}
698
697
699
698
if val .equal (op .value ()) {
700
699
return nil
701
700
}
702
701
703
- return errors . Wrapf ( ErrTestFailed , "testing value %s failed" , path )
702
+ return fmt . Errorf ( "testing value %s failed: %w " , path , ErrTestFailed )
704
703
}
705
704
706
705
func (p Patch ) copy (doc * container , op Operation , accumulatedCopySize * int64 ) error {
707
706
from , err := op .From ()
708
707
if err != nil {
709
- return errors . Wrapf ( err , "copy operation failed to decode from" )
708
+ return fmt . Errorf ( "copy operation failed to decode from: %w" , err )
710
709
}
711
710
712
711
con , key := findObject (doc , from )
713
712
714
713
if con == nil {
715
- return errors . Wrapf ( ErrMissing , "copy operation does not apply: doc is missing from path: %s" , from )
714
+ return fmt . Errorf ( "copy operation does not apply: doc is missing from path: %s: %w " , from , ErrMissing )
716
715
}
717
716
718
717
val , err := con .get (key )
719
718
if err != nil {
720
- return errors . Wrapf ( err , "error in copy for from: '%s'" , from )
719
+ return fmt . Errorf ( "error in copy for from: '%s': %w " , from , err )
721
720
}
722
721
723
722
path , err := op .Path ()
724
723
if err != nil {
725
- return errors . Wrapf ( ErrMissing , "copy operation failed to decode path" )
724
+ return fmt . Errorf ( "copy operation failed to decode path: %w" , ErrMissing )
726
725
}
727
726
728
727
con , key = findObject (doc , path )
729
728
730
729
if con == nil {
731
- return errors . Wrapf ( ErrMissing , "copy operation does not apply: doc is missing destination path: %s" , path )
730
+ return fmt . Errorf ( "copy operation does not apply: doc is missing destination path: %s: %w " , path , ErrMissing )
732
731
}
733
732
734
733
valCopy , sz , err := deepCopy (val )
735
734
if err != nil {
736
- return errors . Wrapf ( err , "error while performing deep copy" )
735
+ return fmt . Errorf ( "error while performing deep copy: %w" , err )
737
736
}
738
737
739
738
(* accumulatedCopySize ) += int64 (sz )
@@ -743,7 +742,7 @@ func (p Patch) copy(doc *container, op Operation, accumulatedCopySize *int64) er
743
742
744
743
err = con .add (key , valCopy )
745
744
if err != nil {
746
- return errors . Wrapf ( err , "error while adding value during copy" )
745
+ return fmt . Errorf ( "error while adding value during copy: %w" , err )
747
746
}
748
747
749
748
return nil
0 commit comments