Skip to content

Commit

Permalink
fix: generate list for multiple items
Browse files Browse the repository at this point in the history
  • Loading branch information
wusteven815 committed Dec 17, 2024
1 parent 7b8d59c commit f40d862
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pyjsonpatch/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ def _generate(source_: Any, target_: Any, path: str):

elif isinstance(source_, list) and isinstance(target_, list):
# Prioritize speed of comparison over the size of patch (do not check for remove/move in middle of list)
smaller = source_ if len(source_) < len(target_) else target_
if smaller is source_:
for i in range(len(target_) - 1, len(source_) - 1, -1):
if len(source_) < len(target_):
for i in range(len(source_)):
_generate(source_[i], target_[i], f"{path}/{i}")
for i in range(len(source_), len(target_)):
patch.append(
{
"op": "add",
Expand All @@ -57,10 +58,11 @@ def _generate(source_: Any, target_: Any, path: str):
}
)
else:
for i in range(len(target_)):
_generate(source_[i], target_[i], f"{path}/{i}")
# Start from end to avoid index shifting
for i in range(len(source_) - 1, len(target_) - 1, -1):
patch.append({"op": "remove", "path": f"{path}/{i}"})
for i in range(len(smaller) - 1, -1, -1):
_generate(source_[i], target_[i], f"{path}/{i}")

else:
patch.append({"op": "replace", "path": path, "value": target_})
Expand Down

0 comments on commit f40d862

Please sign in to comment.