Skip to content

Commit 9789ecc

Browse files
committed
Fix the issue with matching objects
1 parent 00c102b commit 9789ecc

File tree

2 files changed

+47
-45
lines changed

2 files changed

+47
-45
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "json_diff"
33
version = "0.1.0"
4-
authors = ["Kshitij Tyagi <tkshitij@thoughtworks.com>"]
4+
authors = ["ksceriath"]
55
edition = "2018"
66

77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

src/main.rs

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,22 @@ fn main() {
3535
let value: Value = serde_json::from_str(data).unwrap();
3636
let value2: Value = serde_json::from_str(data2).unwrap();
3737

38-
if value == value2 {
39-
println!("JSONs match.");
40-
} else {
41-
match match_json(&value, &value2) {
42-
Mismatch::NoMismatch => println!("No mismatch was found."),
43-
Mismatch::ValueMismatch => println!("Mismatch at root."),
44-
Mismatch::ObjectMismatch(a,b,c) => {
45-
if let Some(left_keys) = a {
46-
println!("Following keys were not found in second object: {:?}", left_keys);
47-
}
48-
if let Some(right_keys) = b {
49-
println!("Following keys were not found in first object: {:?}", right_keys);
50-
}
51-
if let Some(unequal_keys) = c {
52-
println!("Following keys were not found to be equal: {:?}", unequal_keys);
53-
}
38+
match match_json(&value, &value2) {
39+
Mismatch::NoMismatch => println!("No mismatch was found."),
40+
Mismatch::ValueMismatch => println!("Mismatch at root."),
41+
Mismatch::ObjectMismatch(None, None, None) => println!("No mismatch was found."),
42+
Mismatch::ObjectMismatch(a,b,c) => {
43+
if let Some(left_keys) = a {
44+
println!("Following keys were not found in second object: {:?}", left_keys);
5445
}
55-
};
56-
}
46+
if let Some(right_keys) = b {
47+
println!("Following keys were not found in first object: {:?}", right_keys);
48+
}
49+
if let Some(unequal_keys) = c {
50+
println!("Following keys were not found to be equal: {:?}", unequal_keys);
51+
}
52+
}
53+
};
5754
}
5855

5956
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug)]
@@ -62,6 +59,7 @@ struct KeyMap {
6259
children: Option<BTreeSet<KeyMap>>,
6360
}
6461

62+
#[derive(Debug)]
6563
enum Mismatch {
6664
NoMismatch,
6765
ValueMismatch,
@@ -72,34 +70,35 @@ fn match_json(value: &Value, value1: &Value) -> Mismatch {
7270
match (value, value1) {
7371
(Value::Object(a), Value::Object(b)) => {
7472
let (left, right, intersection) = intersect_maps(&a, &b);
75-
let mut unequal_keys = BTreeSet::new();
73+
let mut unequal_keys = None;
7674

77-
let mut left = left.iter().map(|x| KeyMap{ key: String::from(x), children: None }).collect::<BTreeSet<KeyMap>>();
78-
let mut right = right.iter().map(|x| KeyMap{ key: String::from(x), children: None }).collect::<BTreeSet<KeyMap>>();
75+
let mut left = left.map(|l| l.iter().map(|x| KeyMap{ key: String::from(x), children: None }).collect::<BTreeSet<KeyMap>>());
76+
let mut right = right.map(|r| r.iter().map(|x| KeyMap{ key: String::from(x), children: None }).collect::<BTreeSet<KeyMap>>());
7977

80-
for key in intersection {
81-
let x = match_json(&a.get(&key).unwrap(), &b.get(&key).unwrap());
82-
if let Some(keys) = match x {
83-
Mismatch::NoMismatch => None,
84-
Mismatch::ValueMismatch => Some(KeyMap{ key, children: None }),
85-
Mismatch::ObjectMismatch(left_keys, right_keys, mismatch_keys) => {
86-
if let Some(left_keys) = left_keys {
87-
left.insert(KeyMap{ key: String::from(&key), children: Some(left_keys) });
88-
}
89-
if let Some(right_keys) = right_keys {
90-
right.insert(KeyMap{ key: String::from(&key), children: Some(right_keys) });
91-
}
92-
if let Some(mismatch_keys) = mismatch_keys {
93-
Some(KeyMap{ key: String::from(&key), children: Some(mismatch_keys) })
94-
} else {
95-
None
96-
}
97-
},
98-
} {
99-
unequal_keys.insert(keys);
78+
if let Some(intersection) = intersection {
79+
for key in intersection {
80+
if let Some(keys) = match match_json(&a.get(&key).unwrap(), &b.get(&key).unwrap()) {
81+
Mismatch::NoMismatch => None,
82+
Mismatch::ValueMismatch => Some(KeyMap{ key, children: None }),
83+
Mismatch::ObjectMismatch(left_keys, right_keys, mismatch_keys) => {
84+
if let Some(left_keys) = left_keys {
85+
left.get_or_insert(BTreeSet::new()).insert(KeyMap{ key: String::from(&key), children: Some(left_keys) });
86+
}
87+
if let Some(right_keys) = right_keys {
88+
right.get_or_insert(BTreeSet::new()).insert(KeyMap{ key: String::from(&key), children: Some(right_keys) });
89+
}
90+
if let Some(mismatch_keys) = mismatch_keys {
91+
Some(KeyMap{ key: String::from(&key), children: Some(mismatch_keys) })
92+
} else {
93+
None
94+
}
95+
},
96+
} {
97+
unequal_keys.get_or_insert(BTreeSet::new()).insert(keys);
98+
}
10099
}
101100
}
102-
Mismatch::ObjectMismatch(Some(left), Some(right), Some(unequal_keys))
101+
Mismatch::ObjectMismatch(left, right, unequal_keys)
103102
},
104103
(a, b) => {
105104
if a == b {
@@ -112,8 +111,8 @@ fn match_json(value: &Value, value1: &Value) -> Mismatch {
112111
}
113112

114113
fn intersect_maps<'a>(a: &Map<String, Value>,
115-
b: &Map<String, Value>) -> (BTreeSet<String>,
116-
BTreeSet<String>, BTreeSet<String>) {
114+
b: &Map<String, Value>) -> (Option<BTreeSet<String>>,
115+
Option<BTreeSet<String>>, Option<BTreeSet<String>>) {
117116
let mut intersection = BTreeSet::new();
118117
let mut left = BTreeSet::new();
119118
let mut right = BTreeSet::new();
@@ -129,5 +128,8 @@ fn intersect_maps<'a>(a: &Map<String, Value>,
129128
right.insert(String::from(b_key));
130129
}
131130
}
131+
let left = if left.len() == 0 { None } else { Some(left) };
132+
let right = if right.len() == 0 { None } else { Some(right) };
133+
let intersection = if intersection.len() == 0 { None } else { Some(intersection) };
132134
(left, right, intersection)
133135
}

0 commit comments

Comments
 (0)