Skip to content

Commit

Permalink
Rename to diff
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Aug 20, 2024
1 parent b34f075 commit d803c2e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Command line options

.. option:: command

- diff: generate a JSON patch from two JSON files.
- diff: make a JSON patch from two JSON files.
- format: re-format a JSON file.
- patch: apply a JSON patch to the input file.

Expand Down
4 changes: 2 additions & 2 deletions src/jsonyx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
"Manipulator",
"apply_patch",
"detect_encoding",
"diff",
"dump",
"dumps",
"format_syntax_error",
"load",
"load_query_value",
"loads",
"make_patch",
"read",
"run_filter_query",
"run_select_query",
Expand All @@ -36,7 +36,7 @@
from typing import TYPE_CHECKING, Any, Literal

from jsonyx._decoder import DuplicateKey, JSONSyntaxError, make_scanner
from jsonyx._differ import make_patch
from jsonyx._differ import diff
from jsonyx._encoder import make_encoder
from jsonyx._manipulator import Manipulator
from jsonyx.allow import NOTHING
Expand Down
5 changes: 2 additions & 3 deletions src/jsonyx/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from typing import Any, Literal, cast

from jsonyx import (
Decoder, Encoder, JSONSyntaxError, Manipulator, format_syntax_error,
make_patch,
Decoder, Encoder, JSONSyntaxError, Manipulator, diff, format_syntax_error,
)
from jsonyx.allow import EVERYTHING, NOTHING

Expand Down Expand Up @@ -199,7 +198,7 @@ def _run(args: _Namespace) -> None:
if args.command == "diff":
args = cast(_DiffNameSpace, args)
old_input_obj: object = decoder.read(args.old_input_filename)
output_obj: object = make_patch(old_input_obj, input_obj)
output_obj: object = diff(old_input_obj, input_obj)
elif args.command == "format":
output_obj = input_obj
else:
Expand Down
14 changes: 7 additions & 7 deletions src/jsonyx/_differ.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""JSON differ."""
from __future__ import annotations

__all__: list[str] = ["make_patch"]
__all__: list[str] = ["diff"]

import re
from itertools import starmap
Expand Down Expand Up @@ -67,7 +67,7 @@ def _get_lcs(old: list[Any], new: list[Any]) -> list[Any]:
return lcs[::-1]


def _make_patch( # noqa: C901
def _diff( # noqa: C901
old: Any, new: Any, patch: list[dict[str, Any]], path: str = "$",
) -> None:
if _eq(old, new):
Expand All @@ -86,7 +86,7 @@ def _make_patch( # noqa: C901

for key in old_keys & new_keys:
new_path = f"{path}.{_escape(_replace, key)}"
_make_patch(old[key], new[key], patch, new_path)
_diff(old[key], new[key], patch, new_path)
elif isinstance(old, list) and isinstance(new, list):
lcs: list[Any] = _get_lcs(old, new) # type: ignore
old_idx = new_idx = lcs_idx = 0
Expand All @@ -99,7 +99,7 @@ def _make_patch( # noqa: C901
lcs_idx >= len(lcs) or not _eq(new[new_idx], lcs[lcs_idx])
)
if removed and inserted:
_make_patch(old[old_idx], new[new_idx], patch, new_path)
_diff(old[old_idx], new[new_idx], patch, new_path)
old_idx += 1
new_idx += 1
elif removed and not inserted:
Expand All @@ -118,7 +118,7 @@ def _make_patch( # noqa: C901
patch.append({"op": "set", "path": path, "value": new})


def make_patch(old: Any, new: Any) -> list[dict[str, Any]]:
def diff(old: Any, new: Any) -> list[dict[str, Any]]:
"""Make a JSON patch from two Python objects.
:param old: the old Python object
Expand All @@ -135,8 +135,8 @@ def make_patch(old: Any, new: Any) -> list[dict[str, Any]]:
.. versionadded:: 2.0
"""
patch: list[dict[str, Any]] = []
_make_patch(old, new, patch)
_diff(old, new, patch)
return patch


make_patch.__module__ = "jsonyx"
diff.__module__ = "jsonyx"

0 comments on commit d803c2e

Please sign in to comment.