clan_lib: typecast return of get_value_by_path

This commit is contained in:
Johannes Kirschbauer
2025-09-18 16:29:04 +02:00
parent 0fef161391
commit 0f71ffd3c7
4 changed files with 29 additions and 10 deletions

View File

@@ -9,6 +9,7 @@ from clan_lib.machines.machines import Machine
from clan_lib.nix_models.clan import (
InventoryInstance,
InventoryMachine,
InventoryMachineTagsType,
)
from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.persist.util import (
@@ -181,11 +182,11 @@ def get_machine_fields_schema(machine: Machine) -> dict[str, FieldSchema]:
# TODO: handle this more generically. I.e via json schema
persisted_data = inventory_store._get_persisted() # noqa: SLF001
inventory = inventory_store.read()
all_tags = get_value_by_path(inventory, f"machines.{machine.name}.tags", [])
all_tags = get_value_by_path(
inventory, f"machines.{machine.name}.tags", [], InventoryMachineTagsType
)
persisted_tags = get_value_by_path(
persisted_data,
f"machines.{machine.name}.tags",
[],
persisted_data, f"machines.{machine.name}.tags", [], InventoryMachineTagsType
)
nix_tags = list_difference(all_tags, persisted_tags)

View File

@@ -9,7 +9,12 @@ from clan_lib.errors import ClanError
from clan_lib.flake import Flake
from clan_lib.machines import actions as actions_module
from clan_lib.machines.machines import Machine
from clan_lib.nix_models.clan import Clan, InventoryMachine, Unknown
from clan_lib.nix_models.clan import (
Clan,
InventoryMachine,
InventoryMachineTagsType,
Unknown,
)
from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.persist.util import get_value_by_path, set_value_by_path
@@ -233,7 +238,9 @@ def test_get_machine_writeability(clan_flake: Callable[..., Flake]) -> None:
# TODO: Move this into the api
inventory_store = InventoryStore(flake=flake)
inventory = inventory_store.read()
curr_tags = get_value_by_path(inventory, "machines.jon.tags", [])
curr_tags = get_value_by_path(
inventory, "machines.jon.tags", [], InventoryMachineTagsType
)
new_tags = ["managed1", "managed2"]
set_value_by_path(inventory, "machines.jon.tags", [*curr_tags, *new_tags])
inventory_store.write(inventory, message="Test writeability")