Skip to content

Commit d2922c2

Browse files
authored
refactor(codegen): separate v1 schema semantics (#140)
1 parent adc8488 commit d2922c2

3 files changed

Lines changed: 311 additions & 254 deletions

File tree

scripts/_schema_semantics.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from __future__ import annotations
2+
3+
from dataclasses import dataclass
4+
from importlib import import_module
5+
from pathlib import Path
6+
from typing import Protocol, cast
7+
8+
ROOT = Path(__file__).resolve().parents[1]
9+
10+
DEFAULT_SEMANTICS_MODULE = "scripts.gen_schema_v1"
11+
12+
13+
@dataclass(frozen=True, slots=True)
14+
class SchemaSemantics:
15+
schema_json: Path
16+
version_file: Path
17+
schema_out: Path
18+
model_name_map: dict[str, str]
19+
compatibility_aliases: str = ""
20+
21+
22+
class _SemanticsModule(Protocol):
23+
SEMANTICS: SchemaSemantics
24+
25+
26+
def get_default_schema_semantics() -> SchemaSemantics:
27+
module = cast(_SemanticsModule, import_module(DEFAULT_SEMANTICS_MODULE))
28+
return module.SEMANTICS
29+
30+
31+
def inline_model_ref(definition: str, *steps: tuple[str, int | None]) -> str:
32+
ref = f"#/$defs/{definition}"
33+
for keyword, index in steps:
34+
ref += f"#-datamodel-code-generator-#-{keyword}-#-special-#"
35+
if index is not None:
36+
ref += f"/{index}"
37+
return ref
38+
39+
40+
def variant_model_map(
41+
definition: str,
42+
keyword: str,
43+
branch: str,
44+
names: tuple[str, ...],
45+
) -> dict[str, str]:
46+
return {inline_model_ref(definition, (keyword, index), (branch, None)): name for index, name in enumerate(names)}

0 commit comments

Comments
 (0)