feat: catalog-level validate_catalog + index_by_id (unique ids)
Per sub-project-2 plan Task 2 / spec §3. Purely additive to hef.catalog; no existing symbol changes. validate_catalog runs per-record validate() then asserts id uniqueness; index_by_id gives by-id lookup the player also wants. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -126,3 +126,20 @@ def append_record(record, path) -> None:
|
||||
path = Path(path)
|
||||
with path.open("a", encoding="utf-8") as fh:
|
||||
fh.write(json.dumps(record_to_dict(record), ensure_ascii=False) + "\n")
|
||||
|
||||
|
||||
def index_by_id(records) -> dict:
|
||||
"""Map id -> Record, raising CatalogError on a duplicate id."""
|
||||
idx: dict = {}
|
||||
for r in records:
|
||||
if r.id in idx:
|
||||
raise CatalogError(f"duplicate record id: {r.id!r}")
|
||||
idx[r.id] = r
|
||||
return idx
|
||||
|
||||
|
||||
def validate_catalog(records) -> None:
|
||||
"""Validate every record AND cross-record invariants (currently: unique ids)."""
|
||||
for r in records:
|
||||
validate(r)
|
||||
index_by_id(records) # raises on duplicate id
|
||||
|
||||
Reference in New Issue
Block a user