Function chewdata::updater::tera_helpers::function::object::replace_key

source ·
pub fn replace_key(args: &HashMap<String, Value>) -> Result<Value>
Expand description

Replace object key name by another.

§Arguments

  • target - the object on which the transformation will be applied.
  • from - The key to replace. Can be a regular expression.
  • to - The new key.
  • level - The depth level to apply the replacement.

§Examples

use std::collections::HashMap;
use serde_json::value::Value;
use chewdata::updater::tera_helpers::function::object::replace_key;

let target = serde_json::from_str::<Value>(r#"{"field_1":"value_1","field_2":"value_1"}"#).unwrap();
let mut args = HashMap::new();
args.insert("target".to_string(), target);
args.insert("from".to_string(), Value::String("^(field_1)$".to_string()));
args.insert("to".to_string(), Value::String("@$1".to_string()));

let result = replace_key(&args);
assert!(result.is_ok());
assert_eq!(
    serde_json::from_str::<Value>(r#"{"@field_1":"value_1","field_2":"value_1"}"#).unwrap(),
    result.unwrap()
);