use serde_json::Value;
use crate::insert;
use crate::spec::Spec;
pub(crate) fn default(mut input: Value, spec: &Spec) -> Value {
for (path, leaf) in spec.iter() {
if input.pointer(&path.join_rfc6901()).is_none() {
insert(&mut input, path, leaf.clone());
}
}
input
}
#[cfg(test)]
mod test {
use serde_json::json;
use super::*;
#[test]
fn test_insert_if_absent() {
let spec: Spec = serde_json::from_value(json!({
"a" : "default_value",
"d" : {
"e" : "default_value"
}
}))
.expect("parsed spec");
let input: Value = serde_json::from_value(json!({
"b" : "b",
"c" : "c"
}))
.expect("parsed spec");
let output = default(input, &spec);
assert_eq!(
output,
json!({
"a" : "default_value",
"b" : "b",
"c" : "c",
"d" : {
"e" : "default_value"
}
})
)
}
#[test]
fn test_skip_insert_if_present() {
let spec: Spec = serde_json::from_value(json!({
"a" : "default_value"
}))
.expect("parsed spec");
let input: Value = serde_json::from_value(json!({
"a" : "a",
"b" : "b"
}))
.expect("parsed spec");
let output = default(input, &spec);
assert_eq!(
output,
json!({
"a" : "a",
"b" : "b"
})
)
}
}