...
1
16
17 package mergepatch
18
19 import (
20 "errors"
21 "fmt"
22 "reflect"
23 )
24
25 var (
26 ErrBadJSONDoc = errors.New("invalid JSON document")
27 ErrNoListOfLists = errors.New("lists of lists are not supported")
28 ErrBadPatchFormatForPrimitiveList = errors.New("invalid patch format of primitive list")
29 ErrBadPatchFormatForRetainKeys = errors.New("invalid patch format of retainKeys")
30 ErrBadPatchFormatForSetElementOrderList = errors.New("invalid patch format of setElementOrder list")
31 ErrPatchContentNotMatchRetainKeys = errors.New("patch content doesn't match retainKeys list")
32 ErrUnsupportedStrategicMergePatchFormat = errors.New("strategic merge patch format is not supported")
33 )
34
35 func ErrNoMergeKey(m map[string]interface{}, k string) error {
36 return fmt.Errorf("map: %v does not contain declared merge key: %s", m, k)
37 }
38
39 func ErrBadArgType(expected, actual interface{}) error {
40 return fmt.Errorf("expected a %s, but received a %s",
41 reflect.TypeOf(expected),
42 reflect.TypeOf(actual))
43 }
44
45 func ErrBadArgKind(expected, actual interface{}) error {
46 var expectedKindString, actualKindString string
47 if expected == nil {
48 expectedKindString = "nil"
49 } else {
50 expectedKindString = reflect.TypeOf(expected).Kind().String()
51 }
52 if actual == nil {
53 actualKindString = "nil"
54 } else {
55 actualKindString = reflect.TypeOf(actual).Kind().String()
56 }
57 return fmt.Errorf("expected a %s, but received a %s", expectedKindString, actualKindString)
58 }
59
60 func ErrBadPatchType(t interface{}, m map[string]interface{}) error {
61 return fmt.Errorf("unknown patch type: %s in map: %v", t, m)
62 }
63
64
65
66 func IsPreconditionFailed(err error) bool {
67 _, ok := err.(ErrPreconditionFailed)
68 return ok
69 }
70
71 type ErrPreconditionFailed struct {
72 message string
73 }
74
75 func NewErrPreconditionFailed(target map[string]interface{}) ErrPreconditionFailed {
76 s := fmt.Sprintf("precondition failed for: %v", target)
77 return ErrPreconditionFailed{s}
78 }
79
80 func (err ErrPreconditionFailed) Error() string {
81 return err.message
82 }
83
84 type ErrConflict struct {
85 message string
86 }
87
88 func NewErrConflict(patch, current string) ErrConflict {
89 s := fmt.Sprintf("patch:\n%s\nconflicts with changes made from original to current:\n%s\n", patch, current)
90 return ErrConflict{s}
91 }
92
93 func (err ErrConflict) Error() string {
94 return err.message
95 }
96
97
98
99 func IsConflict(err error) bool {
100 _, ok := err.(ErrConflict)
101 return ok
102 }
103
View as plain text