clj-json-patch icon indicating copy to clipboard operation
clj-json-patch copied to clipboard

Improve vector diffing

Open pkoivisto opened this issue 2 years ago • 0 comments

This PR aims to primarily fix the following incorrect output reported by diff-vecs:

       (let [obj1 {"foo" []}
             obj2 {"foo" ["bar" "baz"]}
             patches [{"op" "add" "path" "/foo/0" "value" "bar"}
                      {"op" "add" "path" "/foo/1" "value" "baz"}]]
         (fact "Adding two Array Elements"
               (diff obj1 obj2) => patches))
=> 

FAIL JSON diff - Adding two Array Elements at (core_test.clj:33)
Expected:
[{"op" "add" "path" "/foo/0" "value" "bar"}
 {"op" "add" "path" "/foo/1" "value" "baz"}]
Actual:
[{"op" "replace" "path" "/foo/0" "value" "bar"}
 {"op" "add" "path" "/foo/1" "value" "baz"}]

Additionally, the diff-vecs implementation is slightly changed to improve performance for the likely common case of diff'ed vectors being the same or sharing common prefixes / suffixes.

If any changes to this PR are desired, please feel free to suggest them or just pick from this PR whatever changes you deem desirable.

pkoivisto avatar Nov 23 '23 13:11 pkoivisto