Merge remote-tracking branch 'origin/dev' into dev
This commit is contained in:
commit
b8fbc86084
3 changed files with 110 additions and 11 deletions
43
Cargo.lock
generated
43
Cargo.lock
generated
|
|
@ -185,6 +185,7 @@ name = "base"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"json-patch",
|
||||
"log",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
|
@ -542,6 +543,18 @@ dependencies = [
|
|||
"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]]
|
||||
name = "libc"
|
||||
version = "0.2.153"
|
||||
|
|
@ -949,6 +962,7 @@ name = "storage-sled"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"base",
|
||||
"chrono",
|
||||
"sled",
|
||||
]
|
||||
|
||||
|
|
@ -985,6 +999,26 @@ version = "1.0.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "tokio"
|
||||
version = "1.37.0"
|
||||
|
|
@ -1063,6 +1097,15 @@ dependencies = [
|
|||
"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]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
|
|
|
|||
|
|
@ -11,3 +11,4 @@ chrono = { workspace = true, features = ["serde"] }
|
|||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_json = { workspace = true }
|
||||
log = { workspace = true }
|
||||
json-patch = "*"
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use log::*;
|
||||
#[test]
|
||||
fn print_serialized_test() {
|
||||
let temp_secret = TempSecret {
|
||||
|
|
@ -16,6 +15,46 @@ mod tests {
|
|||
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 {
|
||||
|
|
@ -58,7 +97,7 @@ pub fn deserialize_secret_struct(raw: &String) -> Result<TempSecret, serde_json:
|
|||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct VersionMeta {
|
||||
pub created_time: DateTime<Utc>,
|
||||
pub deletion_time: DateTime<Utc>,
|
||||
pub deletion_time: Option<DateTime<Utc>>, // optional deletion time
|
||||
pub destroyed: bool,
|
||||
}
|
||||
|
||||
|
|
@ -86,14 +125,30 @@ pub fn deserialize_metadata_struct(raw: &String) -> Result<SecretMeta, serde_jso
|
|||
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()){
|
||||
Ok(val ) => {
|
||||
return val},
|
||||
pub fn patch_metadata(
|
||||
old: &mut SecretMeta,
|
||||
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) => {
|
||||
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