SCCMQCSX6BA6DQKARGW25PVWQPU3MZNZS4RNZ53FXYFCKMI24RGQC
for (k, v) in add.iter() {
let updated = pointer_mut(&mut result, k.as_str().unwrap()).unwrap();
*updated = v.to_owned();
for (ptr, v) in add.iter() {
let mut p = ptr
.as_str()
.unwrap();
if p.ends_with('-') {
// When the pointer ends with '-' append the value to the end
// of the sequence. If the sequence doesn't exist, create it.
p = p.trim_end_matches("/-");
let updated = json_pointer::insert(&mut result, p).unwrap();
if let Some(s) = updated.as_sequence_mut() {
s.push(v.to_owned());
} else {
let mut value = Sequence::new();
value.push(v.to_owned());
*updated = serde_yaml::Value::Sequence(value);
}
} else {
let updated = json_pointer::insert(&mut result, p).unwrap();
*updated = v.to_owned();
}
if let Some(prefix) = t.name_prefix {
if let Value::String(current) = &result["metadata"]["name"] {
let new = format!("{prefix}{current}");
result["metadata"]["name"] = Value::String(new.into());
if let Some(Value::String(current)) = json_pointer::get(&result, &p) {
let new = format!("{v}{current}");
*json_pointer::update(&mut result, &p).unwrap() = Value::String(new.into());
}
if let Some(suffix) = t.name_suffix {
if let Value::String(current) = &result["metadata"]["name"] {
let new = format!("{current}{suffix}");
result["metadata"]["name"] = Value::String(new.into());
if let Some(suffix) = t.suffix {
for (ptr, value) in suffix.iter() {
let p: &str = ptr.as_str().unwrap();
let v: &str = value.as_str().unwrap();
if let Some(Value::String(current)) = json_pointer::get(&result, &p) {
let new = format!("{current}{v}");
*json_pointer::update(&mut result, &p).unwrap() = Value::String(new.into());
}
fn pointer<'a>(doc: &'a Value, pointer: &'a str) -> Option<&'a Value> {
// Taken from serde JSON
if pointer.is_empty() {
return Some(doc);
}
if !pointer.starts_with('/') {
return None;
}
pointer
.split('/')
.skip(1)
.map(|x| x.replace("~1", "/").replace("~0", "~"))
.try_fold(doc, |target, token| {
match target {
Value::Mapping(map) => {
let t: Value = serde_yaml::from_str(&token).unwrap();
map.get(&t)
}
Value::Sequence(seq) => {
let t: usize = token.parse().unwrap();
seq.get(t)
}
_ => None,
}
})
}
fn pointer_mut<'a>(doc: &'a mut Value, pointer: &'a str) -> Option<&'a mut Value> {
// Taken from serde JSON
if pointer.is_empty() {
return Some(doc);
}
if !pointer.starts_with('/') {
return None;
}
pointer
.split('/')
.skip(1)
.map(|x| x.replace("~1", "/").replace("~0", "~"))
.try_fold(doc, |target, token| {
match target {
Value::Mapping(map) => {
let t: Value = serde_yaml::from_str(&token).unwrap();
map.get_mut(&t)
}
Value::Sequence(seq) => {
let t: usize = token.parse().unwrap();
seq.get_mut(t)
}
_ => None,
}
})
}
use serde_yaml::{self, Value, Mapping};
pub fn get<'a>(doc: &'a Value, pointer: &'a str) -> Option<&'a Value> {
// Taken from serde JSON
if pointer.is_empty() {
return Some(doc);
}
if !pointer.starts_with('/') {
return None;
}
pointer
.split('/')
.skip(1)
.map(|x| x.replace("~1", "/").replace("~0", "~"))
.try_fold(doc, |target, token| {
match target {
Value::Mapping(map) => {
let t: Value = serde_yaml::from_str(&token).unwrap();
map.get(&t)
}
Value::Sequence(seq) => {
let t: usize = token.parse().unwrap();
seq.get(t)
}
_ => None,
}
})
}
pub fn insert<'a>(doc: &'a mut Value, pointer: &'a str) -> Option<&'a mut Value> {
// Taken from serde JSON
if pointer.is_empty() {
return Some(doc);
}
if !pointer.starts_with('/') {
return None;
}
pointer
.split('/')
.skip(1)
.map(|x| x.replace("~1", "/").replace("~0", "~"))
.try_fold(doc, |target, token| {
match target {
Value::Mapping(map) => {
let t: Value = serde_yaml::from_str(&token).unwrap();
if !map.contains_key(&t) {
let next = Mapping::new();
map.insert(t.to_owned(), Value::Mapping(next));
}
map.get_mut(&t)
}
Value::Sequence(seq) => {
let t = token.parse::<usize>().unwrap();
seq.get_mut(t)
}
_ => None,
}
})
}
pub fn update<'a>(doc: &'a mut Value, pointer: &'a str) -> Option<&'a mut Value> {
// Taken from serde JSON
if pointer.is_empty() {
return Some(doc);
}
if !pointer.starts_with('/') {
return None;
}
pointer
.split('/')
.skip(1)
.map(|x| x.replace("~1", "/").replace("~0", "~"))
.try_fold(doc, |target, token| {
match target {
Value::Mapping(map) => {
let t: Value = serde_yaml::from_str(&token).unwrap();
map.get_mut(&t)
}
Value::Sequence(seq) => {
let t: usize = token.parse().unwrap();
seq.get_mut(t)
}
_ => None,
}
})
}
pub fn upsert<'a>(doc: &'a mut Value, pointer: &'a str) -> Option<&'a mut Value> {
// Taken from serde JSON
if pointer.is_empty() {
return Some(doc);
}
if !pointer.starts_with('/') {
return None;
}
pointer
.split('/')
.skip(1)
.map(|x| x.replace("~1", "/").replace("~0", "~"))
.try_fold(doc, |target, token| {
match target {
Value::Mapping(map) => {
let t: Value = serde_yaml::from_str(&token).unwrap();
if !map.contains_key(&t) {
let next = Mapping::new();
map.insert(t.to_owned(), Value::Mapping(next));
}
map.get_mut(&t)
}
Value::Sequence(seq) => {
let t: usize = token.parse().unwrap();
seq.get_mut(t)
}
_ => None,
}
})
}