|
| 1 | +package kubeapi |
| 2 | + |
| 3 | +/* |
| 4 | + Copyright 2020 - 2021 Crunchy Data Solutions, Inc. |
| 5 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + you may not use this file except in compliance with the License. |
| 7 | + You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | + Unless required by applicable law or agreed to in writing, software |
| 12 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + See the License for the specific language governing permissions and |
| 15 | + limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "strings" |
| 21 | +) |
| 22 | + |
| 23 | +// escapeJSONPointer encodes '~' and '/' according to RFC 6901. |
| 24 | +var escapeJSONPointer = strings.NewReplacer( |
| 25 | + "~", "~0", |
| 26 | + "/", "~1", |
| 27 | +).Replace |
| 28 | + |
| 29 | +// JSON6902 represents a JSON Patch according to RFC 6902; the same as |
| 30 | +// k8s.io/apimachinery/pkg/types.JSONPatchType. |
| 31 | +type JSON6902 []interface{} |
| 32 | + |
| 33 | +// NewJSONPatch creates a new JSON Patch according to RFC 6902; the same as |
| 34 | +// k8s.io/apimachinery/pkg/types.JSONPatchType. |
| 35 | +func NewJSONPatch() *JSON6902 { return &JSON6902{} } |
| 36 | + |
| 37 | +func (*JSON6902) pointer(tokens ...string) string { |
| 38 | + var b strings.Builder |
| 39 | + |
| 40 | + for _, t := range tokens { |
| 41 | + _ = b.WriteByte('/') |
| 42 | + _, _ = b.WriteString(escapeJSONPointer(t)) |
| 43 | + } |
| 44 | + |
| 45 | + return b.String() |
| 46 | +} |
| 47 | + |
| 48 | +// Add appends an "add" operation to patch. |
| 49 | +// |
| 50 | +// > The "add" operation performs one of the following functions, |
| 51 | +// > depending upon what the target location references: |
| 52 | +// > |
| 53 | +// > o If the target location specifies an array index, a new value is |
| 54 | +// > inserted into the array at the specified index. |
| 55 | +// > |
| 56 | +// > o If the target location specifies an object member that does not |
| 57 | +// > already exist, a new member is added to the object. |
| 58 | +// > |
| 59 | +// > o If the target location specifies an object member that does exist, |
| 60 | +// > that member's value is replaced. |
| 61 | +// |
| 62 | +func (patch *JSON6902) Add(path ...string) func(value interface{}) *JSON6902 { |
| 63 | + i := len(*patch) |
| 64 | + f := func(value interface{}) *JSON6902 { |
| 65 | + (*patch)[i] = map[string]interface{}{ |
| 66 | + "op": "add", |
| 67 | + "path": patch.pointer(path...), |
| 68 | + "value": value, |
| 69 | + } |
| 70 | + return patch |
| 71 | + } |
| 72 | + |
| 73 | + *patch = append(*patch, f) |
| 74 | + |
| 75 | + return f |
| 76 | +} |
| 77 | + |
| 78 | +// Remove appends a "remove" operation to patch. |
| 79 | +// |
| 80 | +// > The "remove" operation removes the value at the target location. |
| 81 | +// > |
| 82 | +// > The target location MUST exist for the operation to be successful. |
| 83 | +// |
| 84 | +func (patch *JSON6902) Remove(path ...string) *JSON6902 { |
| 85 | + *patch = append(*patch, map[string]interface{}{ |
| 86 | + "op": "remove", |
| 87 | + "path": patch.pointer(path...), |
| 88 | + }) |
| 89 | + |
| 90 | + return patch |
| 91 | +} |
| 92 | + |
| 93 | +// Replace appends a "replace" operation to patch. |
| 94 | +// |
| 95 | +// > The "replace" operation replaces the value at the target location |
| 96 | +// > with a new value. |
| 97 | +// > |
| 98 | +// > The target location MUST exist for the operation to be successful. |
| 99 | +// |
| 100 | +func (patch *JSON6902) Replace(path ...string) func(value interface{}) *JSON6902 { |
| 101 | + i := len(*patch) |
| 102 | + f := func(value interface{}) *JSON6902 { |
| 103 | + (*patch)[i] = map[string]interface{}{ |
| 104 | + "op": "replace", |
| 105 | + "path": patch.pointer(path...), |
| 106 | + "value": value, |
| 107 | + } |
| 108 | + return patch |
| 109 | + } |
| 110 | + |
| 111 | + *patch = append(*patch, f) |
| 112 | + |
| 113 | + return f |
| 114 | +} |
| 115 | + |
| 116 | +// Bytes returns the JSON representation of patch. |
| 117 | +func (patch JSON6902) Bytes() ([]byte, error) { return json.Marshal(patch) } |
| 118 | + |
| 119 | +// Merge7386 represents a JSON Merge Patch according to RFC 7386; the same as |
| 120 | +// k8s.io/apimachinery/pkg/types.MergePatchType. |
| 121 | +type Merge7386 map[string]interface{} |
| 122 | + |
| 123 | +// NewMergePatch creates a new JSON Merge Patch according to RFC 7386; the same |
| 124 | +// as k8s.io/apimachinery/pkg/types.MergePatchType. |
| 125 | +func NewMergePatch() *Merge7386 { return &Merge7386{} } |
| 126 | + |
| 127 | +// Add modifies patch to indicate that the member at path should be added or |
| 128 | +// replaced with value. |
| 129 | +// |
| 130 | +// > If the provided merge patch contains members that do not appear |
| 131 | +// > within the target, those members are added. If the target does |
| 132 | +// > contain the member, the value is replaced. Null values in the merge |
| 133 | +// > patch are given special meaning to indicate the removal of existing |
| 134 | +// > values in the target. |
| 135 | +// |
| 136 | +func (patch *Merge7386) Add(path ...string) func(value interface{}) *Merge7386 { |
| 137 | + position := *patch |
| 138 | + |
| 139 | + for len(path) > 1 { |
| 140 | + p, ok := position[path[0]].(Merge7386) |
| 141 | + if !ok { |
| 142 | + p = Merge7386{} |
| 143 | + position[path[0]] = p |
| 144 | + } |
| 145 | + |
| 146 | + position = p |
| 147 | + path = path[1:] |
| 148 | + } |
| 149 | + |
| 150 | + if len(path) < 1 { |
| 151 | + return func(interface{}) *Merge7386 { return patch } |
| 152 | + } |
| 153 | + |
| 154 | + f := func(value interface{}) *Merge7386 { |
| 155 | + position[path[0]] = value |
| 156 | + return patch |
| 157 | + } |
| 158 | + |
| 159 | + position[path[0]] = f |
| 160 | + |
| 161 | + return f |
| 162 | +} |
| 163 | + |
| 164 | +// Remove modifies patch to indicate that the member at path should be removed |
| 165 | +// if it exists. |
| 166 | +func (patch *Merge7386) Remove(path ...string) *Merge7386 { |
| 167 | + return patch.Add(path...)(nil) |
| 168 | +} |
| 169 | + |
| 170 | +// Bytes returns the JSON representation of patch. |
| 171 | +func (patch Merge7386) Bytes() ([]byte, error) { return json.Marshal(patch) } |
0 commit comments