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

Function get_mut_value

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

Get a mutable reference to a value at the specified path

(value: &'a mut Value, path: &str)

Source from the content-addressed store, hash-verified

568
569/// Get a mutable reference to a value at the specified path
570fn get_mut_value<'a>(value: &'a mut Value, path: &str) -> Result<&'a mut Value, PatchError> {
571 if path.is_empty() || path == "/" {
572 return Ok(value);
573 }
574
575 let parts = parse_path(path)?;
576 let mut current = value;
577
578 for part in parts {
579 if current.is_object() {
580 current = current
581 .as_object_mut()
582 .unwrap()
583 .entry(part.clone())
584 .or_insert(json!({}));
585 } else if current.is_array() {
586 let index: usize = part
587 .parse()
588 .map_err(|_| PatchError::InvalidPatch(format!("Invalid array index: {}", part)))?;
589 current = current
590 .as_array_mut()
591 .unwrap()
592 .get_mut(index)
593 .ok_or_else(|| {
594 PatchError::OperationFailed(format!("Array index out of bounds: {}", index))
595 })?;
596 } else {
597 return Err(PatchError::OperationFailed(format!(
598 "Cannot traverse non-object/array in path: {}",
599 path
600 )));
601 }
602 }
603
604 Ok(current)
605}
606
607/// Parse a JSON pointer path into parts
608fn parse_path(path: &str) -> Result<Vec<String>, PatchError> {

Callers 3

add_operationFunction · 0.85
remove_operationFunction · 0.85
replace_operationFunction · 0.85

Calls 3

parse_pathFunction · 0.85
is_emptyMethod · 0.45
parseMethod · 0.45

Tested by

no test coverage detected