Skip to content

Commit 15833b9

Browse files
tom-tanmr-c
authored andcommitted
Fix runtime and code-generated handling of nested typeDSL
1 parent dad9bea commit 15833b9

File tree

8 files changed

+36
-39
lines changed

8 files changed

+36
-39
lines changed

schema_salad/codegen.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def codegen(
5757
else ".".join(list(reversed(sp.netloc.split("."))) + sp.path.strip("/").split("/"))
5858
)
5959
info = parser_info or pkg
60+
salad_version = schema_metadata.get("saladVersion", "v1.1")
61+
6062
if lang in set(["python", "cpp", "dlang"]):
6163
if target:
6264
dest: Union[TextIOWrapper, TextIO] = open(target, mode="w", encoding="utf-8")
@@ -83,7 +85,9 @@ def codegen(
8385
)
8486
gen.parse(j)
8587
return
86-
gen = PythonCodeGen(dest, copyright=copyright, parser_info=info)
88+
gen = PythonCodeGen(
89+
dest, copyright=copyright, parser_info=info, salad_version=salad_version
90+
)
8791

8892
elif lang == "java":
8993
gen = JavaCodeGen(

schema_salad/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,10 @@ def main(argsl: Optional[List[str]] = None) -> int:
299299
raise ValidationException(f"Expected a CommentedSeq, got {type(schema_doc)}: {schema_doc}.")
300300

301301
# Create the loader that will be used to load the target document.
302-
document_loader = Loader(schema_ctx, skip_schemas=args.skip_schemas)
302+
schema_version = schema_metadata.get("saladVersion", None)
303+
document_loader = Loader(
304+
schema_ctx, skip_schemas=args.skip_schemas, salad_version=schema_version
305+
)
303306

304307
if args.codegen:
305308
codegen.codegen(

schema_salad/metaschema.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class LoadingOptions:
5959
cache: CacheType
6060
imports: List[str]
6161
includes: List[str]
62-
salad_version: str
6362

6463
def __init__(
6564
self,
@@ -74,7 +73,6 @@ def __init__(
7473
idx: Optional[IdxType] = None,
7574
imports: Optional[List[str]] = None,
7675
includes: Optional[List[str]] = None,
77-
salad_version: Optional[str] = None,
7876
) -> None:
7977
"""Create a LoadingOptions object."""
8078
self.original_doc = original_doc
@@ -140,11 +138,6 @@ def __init__(
140138
self.vocab = _vocab
141139
self.rvocab = _rvocab
142140

143-
if salad_version:
144-
self.salad_version = salad_version
145-
else:
146-
self.salad_version = "v1.0"
147-
148141
if namespaces is not None:
149142
self.vocab = self.vocab.copy()
150143
self.rvocab = self.rvocab.copy()
@@ -580,10 +573,11 @@ def load(self, doc, baseuri, loadingOptions, docRoot=None):
580573

581574

582575
class _TypeDSLLoader(_Loader):
583-
def __init__(self, inner, refScope):
584-
# type: (_Loader, Union[int, None]) -> None
576+
def __init__(self, inner, refScope, salad_version):
577+
# type: (_Loader, Union[int, None], str) -> None
585578
self.inner = inner
586579
self.refScope = refScope
580+
self.salad_version = salad_version
587581

588582
def resolve(
589583
self,
@@ -599,7 +593,7 @@ def resolve(
599593
doc_ = doc_[0:-1]
600594

601595
if doc_.endswith("[]"):
602-
salad_versions = [int(v) for v in loadingOptions.salad_version[1:].split(".")]
596+
salad_versions = [int(v) for v in self.salad_version[1:].split(".")]
603597
items = "" # type: Union[List[Union[Dict[str, Any], str]], Dict[str, Any], str]
604598
if salad_versions < [1, 3]:
605599
items = expand_url(doc_[0:-2], baseuri, loadingOptions, False, True, self.refScope)
@@ -720,9 +714,6 @@ def _document_load(
720714
loadingOptions,
721715
)
722716

723-
if "saladVersion" in doc:
724-
loadingOptions.salad_version = doc["saladVersion"]
725-
726717
if docuri != baseuri:
727718
loadingOptions.idx[docuri] = loadingOptions.idx[baseuri]
728719

@@ -3585,6 +3576,7 @@ def save(
35853576
typedsl_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_or_array_of_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_2 = _TypeDSLLoader(
35863577
union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_or_array_of_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype,
35873578
2,
3579+
"v1.1",
35883580
)
35893581
array_of_RecordFieldLoader = _ArrayLoader(RecordFieldLoader)
35903582
union_of_None_type_or_array_of_RecordFieldLoader = _UnionLoader(
@@ -3597,7 +3589,7 @@ def save(
35973589
union_of_None_type_or_array_of_RecordFieldLoader, "name", "type"
35983590
)
35993591
Record_nameLoader = _EnumLoader(("record",), "Record_name")
3600-
typedsl_Record_nameLoader_2 = _TypeDSLLoader(Record_nameLoader, 2)
3592+
typedsl_Record_nameLoader_2 = _TypeDSLLoader(Record_nameLoader, 2, "v1.1")
36013593
union_of_None_type_or_strtype = _UnionLoader(
36023594
(
36033595
None_type,
@@ -3609,15 +3601,15 @@ def save(
36093601
)
36103602
uri_array_of_strtype_True_False_None = _URILoader(array_of_strtype, True, False, None)
36113603
Enum_nameLoader = _EnumLoader(("enum",), "Enum_name")
3612-
typedsl_Enum_nameLoader_2 = _TypeDSLLoader(Enum_nameLoader, 2)
3604+
typedsl_Enum_nameLoader_2 = _TypeDSLLoader(Enum_nameLoader, 2, "v1.1")
36133605
uri_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_or_array_of_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_False_True_2 = _URILoader(
36143606
union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_or_array_of_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype,
36153607
False,
36163608
True,
36173609
2,
36183610
)
36193611
Array_nameLoader = _EnumLoader(("array",), "Array_name")
3620-
typedsl_Array_nameLoader_2 = _TypeDSLLoader(Array_nameLoader, 2)
3612+
typedsl_Array_nameLoader_2 = _TypeDSLLoader(Array_nameLoader, 2, "v1.1")
36213613
union_of_None_type_or_booltype = _UnionLoader(
36223614
(
36233615
None_type,
@@ -3674,7 +3666,7 @@ def save(
36743666
union_of_None_type_or_array_of_SpecializeDefLoader, "specializeFrom", "specializeTo"
36753667
)
36763668
Documentation_nameLoader = _EnumLoader(("documentation",), "Documentation_name")
3677-
typedsl_Documentation_nameLoader_2 = _TypeDSLLoader(Documentation_nameLoader, 2)
3669+
typedsl_Documentation_nameLoader_2 = _TypeDSLLoader(Documentation_nameLoader, 2, "v1.1")
36783670
union_of_SaladRecordSchemaLoader_or_SaladEnumSchemaLoader_or_DocumentationLoader = (
36793671
_UnionLoader(
36803672
(

schema_salad/python_codegen.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def __init__(
8282
out: IO[str],
8383
copyright: Optional[str],
8484
parser_info: str,
85+
salad_version: str,
8586
) -> None:
8687
super().__init__()
8788
self.out = out
@@ -90,6 +91,7 @@ def __init__(
9091
self.idfield = ""
9192
self.copyright = copyright
9293
self.parser_info = parser_info
94+
self.salad_version = salad_version
9395

9496
@staticmethod
9597
def safe_name(name: str) -> str:
@@ -629,7 +631,7 @@ def typedsl_loader(self, inner: TypeDef, ref_scope: Optional[int]) -> TypeDef:
629631
return self.declare_type(
630632
TypeDef(
631633
f"typedsl_{self.safe_name(inner.name)}_{ref_scope}",
632-
f"_TypeDSLLoader({self.safe_name(inner.name)}, {ref_scope})",
634+
f"_TypeDSLLoader({self.safe_name(inner.name)}, {ref_scope}, '{self.salad_version}')",
633635
)
634636
)
635637

schema_salad/python_codegen_support.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ class LoadingOptions:
5656
cache: CacheType
5757
imports: List[str]
5858
includes: List[str]
59-
salad_version: str
6059

6160
def __init__(
6261
self,
@@ -71,7 +70,6 @@ def __init__(
7170
idx: Optional[IdxType] = None,
7271
imports: Optional[List[str]] = None,
7372
includes: Optional[List[str]] = None,
74-
salad_version: Optional[str] = None,
7573
) -> None:
7674
"""Create a LoadingOptions object."""
7775
self.original_doc = original_doc
@@ -137,11 +135,6 @@ def __init__(
137135
self.vocab = _vocab
138136
self.rvocab = _rvocab
139137

140-
if salad_version:
141-
self.salad_version = salad_version
142-
else:
143-
self.salad_version = "v1.0"
144-
145138
if namespaces is not None:
146139
self.vocab = self.vocab.copy()
147140
self.rvocab = self.rvocab.copy()
@@ -577,10 +570,11 @@ def load(self, doc, baseuri, loadingOptions, docRoot=None):
577570

578571

579572
class _TypeDSLLoader(_Loader):
580-
def __init__(self, inner, refScope):
581-
# type: (_Loader, Union[int, None]) -> None
573+
def __init__(self, inner, refScope, salad_version):
574+
# type: (_Loader, Union[int, None], str) -> None
582575
self.inner = inner
583576
self.refScope = refScope
577+
self.salad_version = salad_version
584578

585579
def resolve(
586580
self,
@@ -596,7 +590,7 @@ def resolve(
596590
doc_ = doc_[0:-1]
597591

598592
if doc_.endswith("[]"):
599-
salad_versions = [int(v) for v in loadingOptions.salad_version[1:].split(".")]
593+
salad_versions = [int(v) for v in self.salad_version[1:].split(".")]
600594
items = "" # type: Union[List[Union[Dict[str, Any], str]], Dict[str, Any], str]
601595
if salad_versions < [1, 3]:
602596
items = expand_url(doc_[0:-2], baseuri, loadingOptions, False, True, self.refScope)
@@ -717,9 +711,6 @@ def _document_load(
717711
loadingOptions,
718712
)
719713

720-
if "saladVersion" in doc:
721-
loadingOptions.salad_version = doc["saladVersion"]
722-
723714
if docuri != baseuri:
724715
loadingOptions.idx[docuri] = loadingOptions.idx[baseuri]
725716

schema_salad/ref_resolver.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def SubLoader(loader: "Loader") -> "Loader":
140140
url_fields=loader.url_fields,
141141
allow_attachments=loader.allow_attachments,
142142
session=loader.session,
143+
salad_version=loader.salad_version,
143144
)
144145

145146

@@ -157,6 +158,7 @@ def __init__(
157158
url_fields: Optional[Set[str]] = None,
158159
allow_attachments: Optional[AttachmentsType] = None,
159160
doc_cache: Union[str, bool] = True,
161+
salad_version: Optional[str] = None,
160162
) -> None:
161163
self.idx: IdxType = (
162164
NormDict(lambda url: urllib.parse.urlsplit(url).geturl()) if idx is None else idx
@@ -205,7 +207,11 @@ def __init__(
205207
self.subscopes: Dict[str, str] = {}
206208
self.secondaryFile_dsl_fields: Set[str] = set()
207209
self.allow_attachments = allow_attachments
208-
self.salad_version = "v1.0"
210+
211+
if salad_version:
212+
self.salad_version = salad_version
213+
else:
214+
self.salad_version = "v1.1"
209215

210216
self.add_context(ctx)
211217

schema_salad/schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ def get_metaschema() -> Tuple[Names, List[Dict[str, str]], Loader]:
177177
},
178178
"typeDSL": saladp + "JsonldPredicate/typeDSL",
179179
"xsd": "http://www.w3.org/2001/XMLSchema#",
180-
}
180+
},
181+
salad_version="v1.3",
181182
)
182183

183184
for salad in SALAD_FILES:

schema_salad/tests/test_examples.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def test_yaml_float_test() -> None:
333333

334334

335335
def test_typedsl_ref() -> None:
336-
ldr = Loader({})
336+
ldr = Loader({}, salad_version="v1.1")
337337
ldr.add_context(
338338
{
339339
"File": "http://example.com/File",
@@ -355,13 +355,12 @@ def test_typedsl_ref() -> None:
355355
ra, _ = ldr.resolve_all(cmap({"type": "File[]?"}), "")
356356
assert {"type": ["null", {"items": "File", "type": "array"}]} == ra
357357

358-
assert ldr.salad_version == "v1.0"
359358
ra, _ = ldr.resolve_all(cmap({"type": "File[][]"}), "")
360359
assert {"type": {"items": "File[]", "type": "array"}} == ra
361360

362361

363362
def test_nested_typedsl_ref() -> None:
364-
ldr = Loader({})
363+
ldr = Loader({}, salad_version="v1.3")
365364
ldr.add_context(
366365
{
367366
"File": "http://example.com/File",
@@ -370,7 +369,6 @@ def test_nested_typedsl_ref() -> None:
370369
"type": {"@type": "@vocab", "typeDSL": True},
371370
}
372371
)
373-
ldr.salad_version = "v1.3"
374372

375373
ra, _ = ldr.resolve_all(cmap({"type": "File[][]"}), "")
376374
assert {"type": {"items": {"items": "File", "type": "array"}, "type": "array"}} == ra

0 commit comments

Comments
 (0)