Skip to content

Commit 7ac0741

Browse files
committed
feat(schema): add can:// durable-id builders (v2)
1 parent b41f66a commit 7ac0741

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

codeanalyzer/schema/ids.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Canonical `can://` id construction for schema v2 (durable ids, ≥ callable).
2+
Ordinal ids (< callable) are `ordinal_id(callable_id, tag)`. Pure functions;
3+
ids are opaque handles (the <file> segment itself contains '/')."""
4+
from __future__ import annotations
5+
from typing import List
6+
7+
_SCHEME = "can://python"
8+
9+
def application_id(app_name: str) -> str:
10+
return f"{_SCHEME}/{app_name}"
11+
12+
def module_id(app_name: str, file_key: str) -> str:
13+
rel = file_key.replace("\\", "/").lstrip("./")
14+
return f"{application_id(app_name)}/{rel}"
15+
16+
def child_id(parent_id: str, segment: str) -> str:
17+
return f"{parent_id}/{segment}"
18+
19+
def callable_sig_segment(name: str, param_names: List[str]) -> str:
20+
return f"{name}({','.join(param_names)})"
21+
22+
def ordinal_id(callable_id: str, tag: str) -> str:
23+
return f"{callable_id}@{tag}"

test/test_v2_ids.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from codeanalyzer.schema import ids
2+
3+
def test_application_and_module_ids():
4+
assert ids.application_id("myapp") == "can://python/myapp"
5+
assert ids.module_id("myapp", "pkg/mod.py") == "can://python/myapp/pkg/mod.py"
6+
7+
def test_callable_signature_segment_uses_param_names():
8+
assert ids.callable_sig_segment("hash", ["self", "s"]) == "hash(self,s)"
9+
assert ids.callable_sig_segment("noargs", []) == "noargs()"
10+
11+
def test_child_and_ordinal_ids_compose():
12+
mod = ids.module_id("myapp", "pkg/mod.py")
13+
cls = ids.child_id(mod, "Hasher")
14+
fn = ids.child_id(cls, ids.callable_sig_segment("hash", ["self", "s"]))
15+
assert fn == "can://python/myapp/pkg/mod.py/Hasher/hash(self,s)"
16+
assert ids.ordinal_id(fn, "15:4") == fn + "@15:4"
17+
assert ids.ordinal_id(fn, "entry") == fn + "@entry"

0 commit comments

Comments
 (0)