+ metadata patching (with test)
This commit is contained in:
parent
bc82e576f3
commit
7ffe071b32
3 changed files with 110 additions and 11 deletions
43
Cargo.lock
generated
43
Cargo.lock
generated
|
|
@ -192,6 +192,7 @@ name = "base"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
|
"json-patch",
|
||||||
"log",
|
"log",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
|
@ -543,6 +544,18 @@ dependencies = [
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "json-patch"
|
||||||
|
version = "1.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "55ff1e1486799e3f64129f8ccad108b38290df9cd7015cd31bed17239f0789d6"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"thiserror",
|
||||||
|
"treediff",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.153"
|
version = "0.2.153"
|
||||||
|
|
@ -950,6 +963,7 @@ name = "storage-sled"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base",
|
"base",
|
||||||
|
"chrono",
|
||||||
"sled",
|
"sled",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -986,6 +1000,26 @@ version = "1.0.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394"
|
checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror"
|
||||||
|
version = "1.0.59"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f0126ad08bff79f29fc3ae6a55cc72352056dfff61e3ff8bb7129476d44b23aa"
|
||||||
|
dependencies = [
|
||||||
|
"thiserror-impl",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror-impl"
|
||||||
|
version = "1.0.59"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d1cd413b5d558b4c5bf3680e324a6fa5014e7b7c067a51e69dbdf47eb7148b66"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.60",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.37.0"
|
version = "1.37.0"
|
||||||
|
|
@ -1064,6 +1098,15 @@ dependencies = [
|
||||||
"once_cell",
|
"once_cell",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "treediff"
|
||||||
|
version = "4.0.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4d127780145176e2b5d16611cc25a900150e86e9fd79d3bde6ff3a37359c9cb5"
|
||||||
|
dependencies = [
|
||||||
|
"serde_json",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-ident"
|
name = "unicode-ident"
|
||||||
version = "1.0.12"
|
version = "1.0.12"
|
||||||
|
|
|
||||||
|
|
@ -11,3 +11,4 @@ chrono = { version = "0.4.38", features = ["serde"] }
|
||||||
serde = { version = "1.0.198", features = ["derive"] }
|
serde = { version = "1.0.198", features = ["derive"] }
|
||||||
serde_json = "1.0.116"
|
serde_json = "1.0.116"
|
||||||
log = { workspace = true }
|
log = { workspace = true }
|
||||||
|
json-patch = "*"
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use log::*;
|
|
||||||
#[test]
|
#[test]
|
||||||
fn print_serialized_test() {
|
fn print_serialized_test() {
|
||||||
let temp_secret = TempSecret {
|
let temp_secret = TempSecret {
|
||||||
|
|
@ -16,6 +15,46 @@ mod tests {
|
||||||
deserialized.unwrap().content
|
deserialized.unwrap().content
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_patching() {
|
||||||
|
// TODO add more assertions
|
||||||
|
let mut base = create_mock_meta();
|
||||||
|
println!("OLD metadata: {:?}", base);
|
||||||
|
let overwrite: SecretMeta = SecretMeta {
|
||||||
|
max_versions: 10,
|
||||||
|
versions: vec![VersionMeta {
|
||||||
|
created_time: Utc::now(),
|
||||||
|
deletion_time: Some(Utc::now()),
|
||||||
|
destroyed: true,
|
||||||
|
}],
|
||||||
|
cas_required: true,
|
||||||
|
delete_version_after: "10m".to_string(),
|
||||||
|
current_version: 4,
|
||||||
|
oldest_version: 2,
|
||||||
|
updated_time: Utc::now(),
|
||||||
|
created_time: Utc::now(),
|
||||||
|
custom_metadata: HashMap::new(),
|
||||||
|
};
|
||||||
|
let mut patched: Option<SecretMeta> = None; // Laurenz here
|
||||||
|
match patch_metadata(&mut base, &overwrite) {
|
||||||
|
Ok(meta) => {
|
||||||
|
println!("NEW metadata: {:?}", meta);
|
||||||
|
println!("patched successfully");
|
||||||
|
patched = Some(meta);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("error patching metadata: {}", e);
|
||||||
|
panic!("Patching failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(patched_meta) = patched {
|
||||||
|
assert!(patched_meta.current_version == 4);
|
||||||
|
assert!(patched_meta.versions[0].destroyed == true);
|
||||||
|
} else {
|
||||||
|
panic!("patched was not initialized");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_mock_meta() -> SecretMeta {
|
pub fn create_mock_meta() -> SecretMeta {
|
||||||
|
|
@ -58,7 +97,7 @@ pub fn deserialize_secret_struct(raw: &String) -> Result<TempSecret, serde_json:
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct VersionMeta {
|
pub struct VersionMeta {
|
||||||
pub created_time: DateTime<Utc>,
|
pub created_time: DateTime<Utc>,
|
||||||
pub deletion_time: DateTime<Utc>,
|
pub deletion_time: Option<DateTime<Utc>>, // optional deletion time
|
||||||
pub destroyed: bool,
|
pub destroyed: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -86,14 +125,30 @@ pub fn deserialize_metadata_struct(raw: &String) -> Result<SecretMeta, serde_jso
|
||||||
serde_json::from_str(&raw)
|
serde_json::from_str(&raw)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn body_to_json(body: String) -> Value{
|
// /// atrocity that currently uses unwrap
|
||||||
|
// fn patch_metadata(old: &mut SecretMeta, new: &SecretMeta) -> Result<SecretMeta, serde_json::Error>{
|
||||||
|
// let mut patch = serde_json::to_value(serialize_metadata_json(&old).unwrap()).unwrap();
|
||||||
|
// let new_json = serde_json::to_value(serialize_metadata_json(&new).unwrap()).unwrap();
|
||||||
|
// json_patch::merge(&mut patch, &new_json);
|
||||||
|
// return deserialize_metadata_struct(&serde_json::to_string(&patch).unwrap());
|
||||||
|
// }
|
||||||
|
|
||||||
match serde_json::from_str::<serde_json::Value>(body.as_str()){
|
pub fn patch_metadata(
|
||||||
Ok(val ) => {
|
old: &mut SecretMeta,
|
||||||
return val},
|
new: &SecretMeta,
|
||||||
|
) -> Result<SecretMeta, serde_json::Error> {
|
||||||
|
let mut patch = serde_json::to_value(old)?; // ? operator is cool: returns early if error was detected
|
||||||
|
let new_json = serde_json::to_value(new)?;
|
||||||
|
json_patch::merge(&mut patch, &new_json);
|
||||||
|
serde_json::from_value(patch)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn body_to_json(body: String) -> Value {
|
||||||
|
match serde_json::from_str::<serde_json::Value>(body.as_str()) {
|
||||||
|
Ok(val) => return val,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::debug!("Faulty result from conversion: {:?}", e);
|
log::debug!("Faulty result from conversion: {:?}", e);
|
||||||
return Into::into("Error converting body") }
|
return Into::into("Error converting body");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue