blob: 16501d5afe9c6d4c4d451b94fd9d9601e795d2e6 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package mergepatch
18
19import (
20 "errors"
21 "fmt"
22 "reflect"
23)
24
25var (
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
35func 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
39func 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
45func 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
60func ErrBadPatchType(t interface{}, m map[string]interface{}) error {
61 return fmt.Errorf("unknown patch type: %s in map: %v", t, m)
62}
63
64// IsPreconditionFailed returns true if the provided error indicates
65// a precondition failed.
66func IsPreconditionFailed(err error) bool {
67 _, ok := err.(ErrPreconditionFailed)
68 return ok
69}
70
71type ErrPreconditionFailed struct {
72 message string
73}
74
75func NewErrPreconditionFailed(target map[string]interface{}) ErrPreconditionFailed {
76 s := fmt.Sprintf("precondition failed for: %v", target)
77 return ErrPreconditionFailed{s}
78}
79
80func (err ErrPreconditionFailed) Error() string {
81 return err.message
82}
83
84type ErrConflict struct {
85 message string
86}
87
88func 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
93func (err ErrConflict) Error() string {
94 return err.message
95}
96
97// IsConflict returns true if the provided error indicates
98// a conflict between the patch and the current configuration.
99func IsConflict(err error) bool {
100 _, ok := err.(ErrConflict)
101 return ok
102}