refactor(triggers): clarify stored integer coercion

This commit is contained in:
chengyongru 2026-07-19 15:35:34 +08:00 committed by chengyongru
parent c2071594cf
commit ffb7ddfa1e

View File

@ -11,11 +11,9 @@ from nanobot.utils.dict_keys import get_camel_snake as _get
TriggerStatus = Literal["ok", "error"] TriggerStatus = Literal["ok", "error"]
def _store_int(value: Any, default: int = 0) -> int: def _int_or_zero(value: Any) -> int:
"""Coerce JSON numerics to int; treat null/blank like a missing key.""" """Coerce a stored JSON numeric, using zero for null or empty values."""
if value is None or value == "": return 0 if value is None or value == "" else int(value)
return default
return int(value)
@dataclass @dataclass
@ -29,7 +27,7 @@ class TriggerRunRecord:
@classmethod @classmethod
def from_dict(cls, data: dict[str, Any]) -> "TriggerRunRecord": def from_dict(cls, data: dict[str, Any]) -> "TriggerRunRecord":
return cls( return cls(
run_at_ms=_store_int(_get(data, "runAtMs", "run_at_ms", 0)), run_at_ms=_int_or_zero(_get(data, "runAtMs", "run_at_ms", 0)),
status=str(data.get("status") or "error"), # type: ignore[arg-type] status=str(data.get("status") or "error"), # type: ignore[arg-type]
error=data.get("error"), error=data.get("error"),
) )
@ -77,8 +75,8 @@ class LocalTrigger:
session_key=str(_get(data, "sessionKey", "session_key", "")), session_key=str(_get(data, "sessionKey", "session_key", "")),
sender_id=str(_get(data, "senderId", "sender_id", "trigger") or "trigger"), sender_id=str(_get(data, "senderId", "sender_id", "trigger") or "trigger"),
origin_metadata=dict(_get(data, "originMetadata", "origin_metadata", {}) or {}), origin_metadata=dict(_get(data, "originMetadata", "origin_metadata", {}) or {}),
created_at_ms=_store_int(_get(data, "createdAtMs", "created_at_ms", 0)), created_at_ms=_int_or_zero(_get(data, "createdAtMs", "created_at_ms", 0)),
updated_at_ms=_store_int(_get(data, "updatedAtMs", "updated_at_ms", 0)), updated_at_ms=_int_or_zero(_get(data, "updatedAtMs", "updated_at_ms", 0)),
last_run_at_ms=_get(data, "lastRunAtMs", "last_run_at_ms"), last_run_at_ms=_get(data, "lastRunAtMs", "last_run_at_ms"),
last_status=_get(data, "lastStatus", "last_status"), # type: ignore[arg-type] last_status=_get(data, "lastStatus", "last_status"), # type: ignore[arg-type]
last_error=_get(data, "lastError", "last_error"), last_error=_get(data, "lastError", "last_error"),
@ -127,8 +125,8 @@ class TriggerDelivery:
id=str(data["id"]), id=str(data["id"]),
trigger_id=str(_get(data, "triggerId", "trigger_id", "")), trigger_id=str(_get(data, "triggerId", "trigger_id", "")),
content=str(data.get("content") or ""), content=str(data.get("content") or ""),
created_at_ms=_store_int(_get(data, "createdAtMs", "created_at_ms", 0)), created_at_ms=_int_or_zero(_get(data, "createdAtMs", "created_at_ms", 0)),
attempts=_store_int(data.get("attempts", 0)), attempts=_int_or_zero(data.get("attempts", 0)),
last_error=data.get("lastError") or data.get("last_error"), last_error=data.get("lastError") or data.get("last_error"),
path=path, path=path,
) )