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

Function remove_operation

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

Remove operation - removes a value at the specified path

(value: &Value, path: &str)

Source from the content-addressed store, hash-verified

224
225/// Remove operation - removes a value at the specified path
226fn remove_operation(value: &Value, path: &str) -> Result<Value, PatchError> {
227 let mut result = value.clone();
228 let (parent_path, key) = split_path(path)?;
229
230 let parent = get_mut_value(&mut result, parent_path)?;
231
232 if let Some(obj) = parent.as_object_mut() {
233 obj.remove(key);
234 } else if let Some(arr) = parent.as_array_mut() {
235 let index: usize = key
236 .parse()
237 .map_err(|_| PatchError::InvalidPatch(format!("Invalid array index: {}", key)))?;
238 if index >= arr.len() {
239 return Err(PatchError::OperationFailed(format!(
240 "Array index out of bounds: {}",
241 index
242 )));
243 }
244 arr.remove(index);
245 } else {
246 return Err(PatchError::OperationFailed(format!(
247 "Cannot remove from non-object/array at path: {}",
248 parent_path
249 )));
250 }
251
252 Ok(result)
253}
254
255/// Replace operation - replaces a value at the specified path
256fn replace_operation(value: &Value, path: &str, new_value: &Value) -> Result<Value, PatchError> {

Callers 2

move_operationFunction · 0.85

Calls 4

split_pathFunction · 0.85
get_mut_valueFunction · 0.85
parseMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected