use serde_json::Value;
use crate::delete;
use crate::spec::Spec;
pub(crate) fn remove(mut input: Value, spec: &Spec) -> Value {
for (path, _) in spec.iter() {
if input.pointer(&path.join_rfc6901()).is_some() {
let _ = delete(&mut input, &path);
}
}
input
}
#[cfg(test)]
mod test {
use serde_json::json;
use super::*;
#[test]
fn test_remove_if_absent() {
let spec: Spec = serde_json::from_value(json!({
"a" : "a",
"d" : {
"e" : "e"
}
}))
.expect("parsed spec");
let input: Value = serde_json::from_value(json!({
"b" : "b",
"c" : "c"
}))
.expect("parsed spec");
let output = remove(input, &spec);
assert_eq!(
output,
json!({
"b" : "b",
"c" : "c"
})
)
}
#[test]
fn test_remove_if_present() {
let spec: Spec = serde_json::from_value(json!({
"a" : ""
}))
.expect("parsed spec");
let input: Value = serde_json::from_value(json!({
"a" : "a",
"b" : "b"
}))
.expect("parsed spec");
let output = remove(input, &spec);
assert_eq!(
output,
json!({
"b" : "b"
})
)
}
}