|
2 | 2 | package jsonpatch |
3 | 3 |
|
4 | 4 | import ( |
5 | | - "encoding/json" |
6 | | - |
7 | | - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 5 | + "github.com/fluxcd/pkg/apis/kustomize" |
8 | 6 | ) |
9 | 7 |
|
10 | 8 | // JSONPatch represents a JSON patch operation. |
11 | 9 | // Technically, a single JSON patch (as defined by RFC 6902) is a list of patch operations. |
12 | 10 | // Opposed to that, this type represents a single operation. Use the JSONPatches type for a list of operations instead. |
13 | | -// +kubebuilder:validation:Schemaless |
14 | | -// +kubebuilder:validation:Type=object |
15 | | -type JSONPatch struct { |
16 | | - // Operation is the operation to perform. |
17 | | - // Valid values are: add, remove, replace, move, copy, test |
18 | | - // +kubebuilder:validation:Enum=add;remove;replace;move;copy;test |
19 | | - // +kubebuilder:validation:Required |
20 | | - Operation Operation `json:"op"` |
21 | | - |
22 | | - // Path is the path to the target location in the JSON document. |
23 | | - // +kubebuilder:validation:Required |
24 | | - Path string `json:"path"` |
25 | | - |
26 | | - // Value is the value to set at the target location. |
27 | | - // Required for add, replace, and test operations. |
28 | | - // +kubebuilder:validation:Schemaless |
29 | | - // +kubebuilder:pruning:PreserveUnknownFields |
30 | | - // +optional |
31 | | - Value *apiextensionsv1.JSON `json:"value,omitempty"` |
32 | | - |
33 | | - // From is the source location for move and copy operations. |
34 | | - // +optional |
35 | | - From *string `json:"from,omitempty"` |
36 | | -} |
| 11 | +type JSONPatch = kustomize.JSON6902 |
37 | 12 |
|
38 | 13 | // JSONPatches is a list of JSON patch operations. |
39 | 14 | // This is technically a 'JSON patch' as defined in RFC 6902. |
40 | 15 | type JSONPatches []JSONPatch |
41 | 16 |
|
42 | | -type Operation string |
43 | | - |
44 | 17 | const ( |
45 | 18 | // ADD is the constant for the JSONPatch 'add' operation. |
46 | | - ADD Operation = "add" |
| 19 | + ADD = "add" |
47 | 20 | // REMOVE is the constant for the JSONPatch 'remove' operation. |
48 | | - REMOVE Operation = "remove" |
| 21 | + REMOVE = "remove" |
49 | 22 | // REPLACE is the constant for the JSONPatch 'replace' operation. |
50 | | - REPLACE Operation = "replace" |
| 23 | + REPLACE = "replace" |
51 | 24 | // MOVE is the constant for the JSONPatch 'move' operation. |
52 | | - MOVE Operation = "move" |
| 25 | + MOVE = "move" |
53 | 26 | // COPY is the constant for the JSONPatch 'copy' operation. |
54 | | - COPY Operation = "copy" |
| 27 | + COPY = "copy" |
55 | 28 | // TEST is the constant for the JSONPatch 'test' operation. |
56 | | - TEST Operation = "test" |
| 29 | + TEST = "test" |
57 | 30 | ) |
58 | | - |
59 | | -// NewJSONPatch creates a new JSONPatch with the given values. |
60 | | -// If value is non-nil, it is marshaled to JSON. Returns an error if the value cannot be marshaled. |
61 | | -func NewJSONPatch(op Operation, path string, value any, from *string) (JSONPatch, error) { |
62 | | - res := JSONPatch{ |
63 | | - Operation: op, |
64 | | - Path: path, |
65 | | - From: from, |
66 | | - } |
67 | | - var err error |
68 | | - if value != nil { |
69 | | - var valueJSON []byte |
70 | | - valueJSON, err = json.Marshal(value) |
71 | | - res.Value = &apiextensionsv1.JSON{ |
72 | | - Raw: valueJSON, |
73 | | - } |
74 | | - } |
75 | | - return res, err |
76 | | -} |
77 | | - |
78 | | -// NewJSONPatchOrPanic works like NewJSONPatch, but instead of returning an error, it panics if the patch cannot be created. |
79 | | -func NewJSONPatchOrPanic(op Operation, path string, value any, from *string) JSONPatch { |
80 | | - patch, err := NewJSONPatch(op, path, value, from) |
81 | | - if err != nil { |
82 | | - panic(err) |
83 | | - } |
84 | | - return patch |
85 | | -} |
86 | | - |
87 | | -// NewJSONPatches combines multiple JSONPatch instances into a single JSONPatches instance. |
88 | | -// This is a convenience function to create a JSONPatches instance from multiple JSONPatch instances. |
89 | | -func NewJSONPatches(patches ...JSONPatch) JSONPatches { |
90 | | - result := make(JSONPatches, 0, len(patches)) |
91 | | - for _, patch := range patches { |
92 | | - result = append(result, patch) |
93 | | - } |
94 | | - return result |
95 | | -} |
0 commit comments