From 057c0ca7e93440663ace945285437955aa09b609 Mon Sep 17 00:00:00 2001 From: Christopher Regali Date: Fri, 10 Nov 2023 22:32:09 +0100 Subject: [PATCH 1/3] Fix some problematic case for myers diff --- src/process.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/process.rs b/src/process.rs index a83b5d7..4730be6 100644 --- a/src/process.rs +++ b/src/process.rs @@ -99,8 +99,18 @@ pub fn match_json(value1: &Value, value2: &Value) -> Mismatch { let mismatch: Vec<_> = replaced .into_iter() - .flat_map(|(o, ol, n, _nl)| { - (0..ol).map(move |i| (o + i, match_json(&a[o + i], &b[n + i]).keys_in_both)) + .flat_map(|(o, ol, n, nl)| { + let max_length = ol.max(nl); + (0..max_length).map(move |i| { + ( + o + i, + match_json( + a.get(o + i).unwrap_or(&Value::Null), + b.get(n + i).unwrap_or(&Value::Null), + ) + .keys_in_both, + ) + }) }) .collect(); @@ -277,6 +287,22 @@ mod tests { assert_eq!(diff.left_only_keys, KeyNode::Nil); } + #[test] + fn long_insertion_modification() { + let data1 = r#"["a","b","a"]"#; + let data2 = r#"["a","c","c","c","a"]"#; + let diff = compare_jsons(data1, data2).unwrap(); + let diffs = diff.keys_in_both.absolute_keys_to_vec(None); + // 1. is b!=c, second is a!=c, third is missing in data1 but c in data2 + assert_eq!(diffs.len(), 3); + assert_eq!( + diffs.last().unwrap().to_string(), + r#"[l: 3] -> { null != "c" }"# + ); + assert_eq!(diff.right_only_keys, KeyNode::Nil); + assert_eq!(diff.left_only_keys, KeyNode::Nil); + } + #[test] fn test_arrays_object_extra() { let data1 = r#"["a","b"]"#; From 1d7ea797d6cc5039ded8f87f82de66ce9fffa9c4 Mon Sep 17 00:00:00 2001 From: Christopher Regali Date: Fri, 10 Nov 2023 22:32:37 +0100 Subject: [PATCH 2/3] Bump version to 0.3.1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 6f1200d..d76fcfc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "json_diff_ng" -version = "0.3.0" +version = "0.3.1" authors = ["ksceriath", "ChrisRega"] edition = "2021" license = "Unlicense" From 6ab8672fa44ef872dd211a0ac6baa1f686d50aa3 Mon Sep 17 00:00:00 2001 From: Christopher Regali Date: Fri, 10 Nov 2023 22:33:00 +0100 Subject: [PATCH 3/3] Fixup README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ab54ce8..ec10cbf 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ Usage Example: Option: -f : read input from json files -d : read input from command line +file : read input from json files +direct : read input from command line ### Installation