From f131f727494113b573e6faf2ff496df0ffdc94de Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Fri, 7 Aug 2026 05:31:48 +0000 Subject: [PATCH] Demo: simplify resolve_user to a plain previous-state lookup --- demo/main.py | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/demo/main.py b/demo/main.py index ec87ddc..09ec190 100644 --- a/demo/main.py +++ b/demo/main.py @@ -7,7 +7,7 @@ from pathlib import Path import msgspec from kanta import Kanta -from kanta.callbacks import DictPost, DictPre +from kanta.callbacks import DictPre from kanta.logging import configure_logging @@ -47,18 +47,11 @@ kanta_v1 = Kanta(filename, Data(), migrations=sys.modules[__name__]) @kanta_v1.logfmt -def resolve_user( - value: str, path: str, previous: DictPre, current: DictPost -) -> str | None: +def resolve_user(value: str, path: str, previous: DictPre) -> str | None: """Resolve user ids to names from the database state itself.""" if path != "$user" and not path.startswith("users."): return None - # Post-change state first, then pre-change (deleted users are only there). - for state in (current, previous): - name = state.get("users", {}).get(value, {}).get("name") - if name: - return name - return None + return previous.get("users", {}).get(value, {}).get("name") async def main() -> None: @@ -92,18 +85,13 @@ async def main() -> None: ) async with kanta_v1 as kanta: with kanta.transaction( - action="import", user="userid001", logdiff=False + action="update", user="userid002", extra=filename.name ) as data: - data.total = 3 + data.total += 1 with kanta.transaction(action="delete", user="userid002") as data: del data.users["userid001"] - with kanta.transaction( - action="update", user="userid002", extra=filename.name - ) as data: - data.total = 4 - # Fake clock for deterministic timestamps _now = datetime(2027, 1, 1, tzinfo=UTC)