Skip to content

Commit d07a281

Browse files
authored
dlang: support saladVersion (#715)
* dlang: support saladVersion * Replace `Either` with `Union`
1 parent ff30991 commit d07a281

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

schema_salad/codegen.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def codegen(
8282
pkg,
8383
copyright,
8484
info,
85+
salad_version,
8586
)
8687
gen.parse(j)
8788
return

schema_salad/dlang_codegen.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(
2222
package: str,
2323
copyright_: Optional[str],
2424
parser_info: Optional[str],
25+
salad_version: str,
2526
) -> None:
2627
"""Initialize the D codegen."""
2728
super().__init__()
@@ -31,6 +32,7 @@ def __init__(
3132
self.package = package
3233
self.copyright = copyright_
3334
self.parser_info = parser_info
35+
self.salad_version = salad_version
3436
self.doc_root_types: List[str] = []
3537

3638
def prologue(self) -> None:
@@ -60,11 +62,11 @@ def prologue(self) -> None:
6062
f"""module {self.package};
6163
6264
import salad.meta.dumper : genDumper;
63-
import salad.meta.impl : genCtor, genIdentifier, genOpEq;
65+
import salad.meta.impl : genCtor_, genIdentifier, genOpEq;
6466
import salad.meta.parser : import_ = importFromURI;
6567
import salad.meta.uda : documentRoot, id, idMap, link, LinkResolver, secondaryFilesDSL, typeDSL;
6668
import salad.primitives : SchemaBase;
67-
import salad.type : None, Either;
69+
import salad.type : None, Union;
6870
6971
"""
7072
)
@@ -75,10 +77,21 @@ def prologue(self) -> None:
7577
""" # noqa: B907
7678
)
7779

80+
self.target.write(
81+
f"""
82+
enum saladVersion = "{self.salad_version}";
83+
84+
mixin template genCtor()
85+
{{
86+
mixin genCtor_!saladVersion;
87+
}}
88+
"""
89+
)
90+
7891
def epilogue(self, root_loader: TypeDef) -> None:
7992
"""Trigger to generate the epilouge code."""
8093
doc_root_type_str = ", ".join(self.doc_root_types)
81-
doc_root_type = f"Either!({doc_root_type_str})"
94+
doc_root_type = f"Union!({doc_root_type_str})"
8295
self.target.write(
8396
f"""
8497
///
@@ -185,7 +198,7 @@ def parse_record_field_type(
185198
elif isinstance(type_, list):
186199
t_str = [self.parse_record_field_type(t, None)[1] for t in type_]
187200
union_types = ", ".join(t_str)
188-
type_str = f"Either!({union_types})"
201+
type_str = f"Union!({union_types})"
189202
elif shortname(type_["type"]) == "array":
190203
item_type = self.parse_record_field_type(type_["items"], None)[1]
191204
type_str = f"{item_type}[]"

0 commit comments

Comments
 (0)