From aa785419d8a8e500641b0346f1a2324a63ce70c5 Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Thu, 29 Jun 2023 10:08:49 -0400 Subject: [PATCH] Update backend_api to account for possibly null resource vector --- crates/rdsys-backend-api/Cargo.toml | 2 +- crates/rdsys-backend-api/src/proto.rs | 29 +++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/crates/rdsys-backend-api/Cargo.toml b/crates/rdsys-backend-api/Cargo.toml index eaf2b3f..d096167 100644 --- a/crates/rdsys-backend-api/Cargo.toml +++ b/crates/rdsys-backend-api/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rdsys_backend" authors = ["Cecylia Bocovich "] -version = "0.1.0" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/crates/rdsys-backend-api/src/proto.rs b/crates/rdsys-backend-api/src/proto.rs index eb21b15..20dfd93 100644 --- a/crates/rdsys-backend-api/src/proto.rs +++ b/crates/rdsys-backend-api/src/proto.rs @@ -54,9 +54,9 @@ impl Resource { /// A ResourceDiff holds information about new, changed, or pruned resources #[derive(Deserialize, PartialEq, Eq, Debug)] pub struct ResourceDiff { - pub new: Option>>, - pub changed: Option>>, - pub gone: Option>>, + pub new: Option>>>, + pub changed: Option>>>, + pub gone: Option>>>, pub full_update: bool, } @@ -201,7 +201,9 @@ mod tests { assert_eq!(diff.gone, None); assert_eq!(diff.full_update, true); if let Some(new) = diff.new { - assert_eq!(new["obfs2"][0].r#type, "obfs2"); + if let Some(obfs2) = &new["obfs2"] { + assert_eq!(obfs2[0].r#type, "obfs2"); + } } } @@ -236,4 +238,23 @@ mod tests { }; assert_eq!(diff, empty_diff); } + + #[test] + fn deserialize_empty_hashmap() { + let data = r#" + { + "new": { + "obfs4": null, + "scramblesuit": [] + }, + "changed": null, + "gone": null, + "full_update": true + }"#; + let diff: ResourceDiff = serde_json::from_str(data).unwrap(); + assert_ne!(diff.new, None); + assert_eq!(diff.changed, None); + assert_eq!(diff.gone, None); + assert_eq!(diff.full_update, true); + } }