MCPcopy Create free account
hub / github.com/calfonso/rusternetes / apply_json_patch_operation

Function apply_json_patch_operation

crates/api-server/src/patch.rs:162–191  ·  view source on GitHub ↗

Apply a single JSON Patch operation

(value: &Value, op: &JsonPatchOperation)

Source from the content-addressed store, hash-verified

160
161/// Apply a single JSON Patch operation
162fn apply_json_patch_operation(value: &Value, op: &JsonPatchOperation) -> Result<Value, PatchError> {
163 let require_value = |what: &str| -> Result<&Value, PatchError> {
164 op.value
165 .as_ref()
166 .ok_or_else(|| PatchError::InvalidPatch(format!("'value' required for {}", what)))
167 };
168 match op.op {
169 JsonPatchOp::Add => add_operation(value, &op.path, require_value("add")?),
170 JsonPatchOp::Remove => remove_operation(value, &op.path),
171 JsonPatchOp::Replace => replace_operation(value, &op.path, require_value("replace")?),
172 JsonPatchOp::Move => {
173 let from = op
174 .from
175 .as_ref()
176 .ok_or_else(|| PatchError::InvalidPatch("'from' required for move".to_string()))?;
177 move_operation(value, from, &op.path)
178 }
179 JsonPatchOp::Copy => {
180 let from = op
181 .from
182 .as_ref()
183 .ok_or_else(|| PatchError::InvalidPatch("'from' required for copy".to_string()))?;
184 copy_operation(value, from, &op.path)
185 }
186 JsonPatchOp::Test => {
187 test_operation(value, &op.path, require_value("test")?)?;
188 Ok(value.clone())
189 }
190 }
191}
192
193/// Add operation - adds a value at the specified path
194fn add_operation(value: &Value, path: &str, new_value: &Value) -> Result<Value, PatchError> {

Callers 1

apply_json_patchFunction · 0.85

Calls 6

add_operationFunction · 0.85
remove_operationFunction · 0.85
replace_operationFunction · 0.85
move_operationFunction · 0.85
copy_operationFunction · 0.85
test_operationFunction · 0.85

Tested by

no test coverage detected