refactor(lib/persist): rename 'patch' to 'apply_patch'

This commit is contained in:
Johannes Kirschbauer
2025-05-14 16:16:16 +02:00
parent 574ed11df4
commit 633e6ffeae
4 changed files with 14 additions and 11 deletions

View File

@@ -19,10 +19,10 @@ from clan_lib.api import API
from clan_lib.nix_models.inventory import Inventory
from clan_lib.persist.inventory_store import WriteInfo
from clan_lib.persist.util import (
apply_patch,
calc_patches,
delete_by_path,
determine_writeability,
patch,
)
from clan_cli.cmd import run
@@ -150,7 +150,7 @@ def patch_inventory_with(flake: Flake, section: str, content: dict[str, Any]) ->
with inventory_file.open("r") as f:
curr_inventory = json.load(f)
patch(curr_inventory, section, content)
apply_patch(curr_inventory, section, content)
with inventory_file.open("w") as f:
json.dump(curr_inventory, f, indent=2)
@@ -206,7 +206,7 @@ def set_inventory(
persisted = dict(write_info.data_disk)
for patch_path, data in patchset.items():
patch(persisted, patch_path, data)
apply_patch(persisted, patch_path, data)
for delete_path in delete_set:
delete_by_path(persisted, delete_path)

View File

@@ -8,10 +8,10 @@ from clan_cli.git import commit_file
from clan_lib.nix_models.inventory import Inventory
from .util import (
apply_patch,
calc_patches,
delete_by_path,
determine_writeability,
patch,
)
@@ -153,7 +153,7 @@ class InventoryStore:
persisted = dict(write_info.data_disk)
for patch_path, data in patchset.items():
patch(persisted, patch_path, data)
apply_patch(persisted, patch_path, data)
for delete_path in delete_set:
delete_by_path(persisted, delete_path)

View File

@@ -307,7 +307,10 @@ def delete_by_path(d: dict[str, Any], path: str) -> Any:
return {last_key: value}
def patch(d: dict[str, Any], path: str, content: Any) -> None:
type DictLike = dict[str, Any] | Any
def apply_patch(d: DictLike, path: str, content: Any) -> None:
"""
Update the value at a specific dot-separated path in a nested dictionary.

View File

@@ -5,10 +5,10 @@ import pytest
from clan_cli.errors import ClanError
from clan_lib.persist.util import (
apply_patch,
calc_patches,
delete_by_path,
determine_writeability,
patch,
unmerge_lists,
)
@@ -17,7 +17,7 @@ from clan_lib.persist.util import (
def test_patch_nested() -> None:
orig = {"a": 1, "b": {"a": 2.1, "b": 2.2}, "c": 3}
patch(orig, "b.b", "foo")
apply_patch(orig, "b.b", "foo")
# Should only update the nested value
assert orig == {"a": 1, "b": {"a": 2.1, "b": "foo"}, "c": 3}
@@ -28,7 +28,7 @@ def test_patch_nested_dict() -> None:
# This should update the whole "b" dict
# Which also removes all other keys
patch(orig, "b", {"b": "foo"})
apply_patch(orig, "b", {"b": "foo"})
# Should only update the nested value
assert orig == {"a": 1, "b": {"b": "foo"}, "c": 3}
@@ -37,13 +37,13 @@ def test_patch_nested_dict() -> None:
def test_create_missing_paths() -> None:
orig = {"a": 1}
patch(orig, "b.c", "foo")
apply_patch(orig, "b.c", "foo")
# Should only update the nested value
assert orig == {"a": 1, "b": {"c": "foo"}}
orig = {}
patch(orig, "a.b.c", "foo")
apply_patch(orig, "a.b.c", "foo")
assert orig == {"a": {"b": {"c": "foo"}}}