|
| 1 | +""" |
| 2 | +Complex example: schema discovery + taxonomy prefixes + robust handling. |
| 3 | +
|
| 4 | +Run: |
| 5 | + # (recommended) set schema location so discovery works |
| 6 | + export TYPEID_SCHEMA=examples/schemas/typeid.schema.json |
| 7 | +
|
| 8 | + python examples/explain_complex.py |
| 9 | +
|
| 10 | +Optional: |
| 11 | + pip install typeid-python[yaml] |
| 12 | + export TYPEID_SCHEMA=examples/schemas/typeid.schema.yaml |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | +from typing import Iterable |
| 17 | + |
| 18 | +from typeid import TypeID |
| 19 | +from typeid.explain.discovery import discover_schema_path |
| 20 | +from typeid.explain.registry import load_registry, make_lookup |
| 21 | +from typeid.explain.engine import explain as explain_engine |
| 22 | +from typeid.explain.formatters import format_explanation_pretty |
| 23 | + |
| 24 | + |
| 25 | +def _load_schema_lookup(): |
| 26 | + discovery = discover_schema_path() |
| 27 | + if discovery.path is None: |
| 28 | + print("No schema discovered. Proceeding without schema.") |
| 29 | + return None |
| 30 | + |
| 31 | + result = load_registry(discovery.path) |
| 32 | + if result.registry is None: |
| 33 | + print(f"Schema load failed: {result.error.message if result.error else 'unknown error'}") |
| 34 | + return None |
| 35 | + |
| 36 | + print(f"Schema loaded from: {discovery.path} ({discovery.source})") |
| 37 | + return make_lookup(result.registry) |
| 38 | + |
| 39 | + |
| 40 | +def _banner(title: str) -> None: |
| 41 | + print("\n" + "=" * 80) |
| 42 | + print(title) |
| 43 | + print("=" * 80) |
| 44 | + |
| 45 | + |
| 46 | +def _explain_many(ids: Iterable[str], lookup) -> None: |
| 47 | + for tid in ids: |
| 48 | + exp = explain_engine(tid, schema_lookup=lookup, enable_schema=True, enable_links=True) |
| 49 | + print(format_explanation_pretty(exp)) |
| 50 | + |
| 51 | + |
| 52 | +def main() -> None: |
| 53 | + _banner("TypeID explain — complex demo") |
| 54 | + |
| 55 | + # Use schema discovery (env/cwd/user-config) |
| 56 | + lookup = _load_schema_lookup() |
| 57 | + |
| 58 | + # Create a bunch of IDs: |
| 59 | + # - standard prefixes |
| 60 | + # - taxonomy prefix (env/region in prefix) |
| 61 | + # - unknown prefix |
| 62 | + # - invalid string |
| 63 | + user_id = str(TypeID(prefix="user")) |
| 64 | + order_id = str(TypeID(prefix="order")) |
| 65 | + evt_id = str(TypeID(prefix="evt_payment")) |
| 66 | + user_live_eu_id = str(TypeID(prefix="user_live_eu")) |
| 67 | + unknown_id = str(TypeID(prefix="something_new")) |
| 68 | + invalid_id = "user_NOT_A_SUFFIX" |
| 69 | + |
| 70 | + _banner("Explaining generated IDs") |
| 71 | + ids = [user_id, order_id, evt_id, user_live_eu_id, unknown_id, invalid_id] |
| 72 | + _explain_many(ids, lookup) |
| 73 | + |
| 74 | + _banner("Notes") |
| 75 | + print("- IDs still explain offline (derived facts always present).") |
| 76 | + print("- Schema adds meaning, ownership, policies, and links.") |
| 77 | + print("- Prefix taxonomy works because TypeID prefixes allow underscores.") |
| 78 | + print("- Invalid IDs never crash; they return valid=false and errors.") |
| 79 | + print("- Unknown prefixes still show derived facts, schema found=false.") |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + # Helpful hint for users |
| 84 | + if "TYPEID_SCHEMA" not in os.environ: |
| 85 | + print("Tip: set TYPEID_SCHEMA to enable schema discovery, e.g.:") |
| 86 | + print(" export TYPEID_SCHEMA=examples/schemas/typeid.schema.json\n") |
| 87 | + main() |
0 commit comments