Skip to content

Commit

Permalink
Fix struct-in-map diffing
Browse files Browse the repository at this point in the history
  • Loading branch information
Diggsey committed Feb 17, 2022
1 parent f5c05cf commit b346787
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[package]
name = "serde-diff"
version = "0.4.1"
authors = ["Karl Bergström <[email protected]>", "Philip Degarmo <[email protected]>"]
authors = [
"Karl Bergström <[email protected]>",
"Philip Degarmo <[email protected]>",
]
readme = "README.md"
exclude = ["examples/*"]
license = "Apache-2.0 OR MIT"
Expand All @@ -17,10 +20,11 @@ maintenance = { status = "experimental" }

[dependencies]
serde-diff-derive = { version = "0.4.0", path = "serde-diff-derive" }
serde = { version = "1", features = [ "derive" ] }
serde_derive = { version = "1", features = ["deserialize_in_place"]}
serde = { version = "1", features = ["derive"] }
serde_derive = { version = "1", features = ["deserialize_in_place"] }

[dev-dependencies]
serde_json = "1.0"
bincode = "1.2"
rmp-serde = "0.15.0"
maplit = "1.0.2"
79 changes: 79 additions & 0 deletions examples/nested_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use maplit::hashmap;
use serde::{Deserialize, Serialize};
use serde_diff::{Apply, Diff, SerdeDiff};
use std::collections::HashMap;

#[derive(SerdeDiff, Serialize, Deserialize, Debug, Default, PartialEq, Clone)]
struct TestValue {
v: String,
}

#[derive(SerdeDiff, Serialize, Deserialize, Debug, Default, PartialEq, Clone)]
struct TestStruct {
test: bool,
map: HashMap<String, HashMap<i32, TestValue>>,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut empty = TestStruct::default();
empty.test = true;

let mut hello_world = TestStruct::default();
hello_world.map.insert(
"hello".to_string(),
hashmap![0 => TestValue { v: "world".to_string()}],
);

let mut hi_world = TestStruct::default();
hi_world.map.insert(
"hi".to_string(),
hashmap![0 => TestValue { v: "world".to_string()}],
);

let mut hi_world_and_planet = TestStruct::default();
hi_world_and_planet.map.insert(
"hi".to_string(),
hashmap![0 => TestValue { v: "world".to_string()}, 1 => TestValue { v: "planet".to_string()}],
);

let mut hi_planet = TestStruct::default();
hi_planet.map.insert(
"hi".to_string(),
hashmap![0 => TestValue { v: "planet".to_string()}],
);

let mut hi_planet_hello_world = TestStruct::default();
hi_planet_hello_world.map.insert(
"hi".to_string(),
hashmap![0 => TestValue { v: "planet".to_string()}],
);
hi_planet_hello_world.map.insert(
"hello".to_string(),
hashmap![0 => TestValue { v: "world".to_string()}],
);

let add_hello = serde_json::to_string(&Diff::serializable(&empty, &hello_world))?;
let hello_to_hi = serde_json::to_string(&Diff::serializable(&hello_world, &hi_world))?;
let add_planet = serde_json::to_string(&Diff::serializable(&hi_world, &hi_world_and_planet))?;
let del_world = serde_json::to_string(&Diff::serializable(&hi_world_and_planet, &hi_planet))?;
let no_change = serde_json::to_string(&Diff::serializable(&hi_planet, &hi_planet))?;
let add_world = serde_json::to_string(&Diff::serializable(&hi_planet, &hi_planet_hello_world))?;

let mut built = TestStruct::default();
for (diff, after) in &[
(add_hello, hello_world),
(hello_to_hi, hi_world),
(add_planet, hi_world_and_planet),
(del_world, hi_planet.clone()),
(no_change, hi_planet),
(add_world, hi_planet_hello_world),
] {
println!("{}", diff);

let mut deserializer = serde_json::Deserializer::from_str(&diff);
Apply::apply(&mut deserializer, &mut built)?;

assert_eq!(after, &built);
}
Ok(())
}
1 change: 1 addition & 0 deletions src/implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ macro_rules! map_serde_diff {
if <V as SerdeDiff>::diff(self_value, &mut subctx, other_value)? {
changed = true;
}
subctx.pop_path_element()?;
},
None => {
ctx.save_command(&DiffCommandRef::RemoveKey(key), true, true)?;
Expand Down

0 comments on commit b346787

Please sign in to comment.