Skip to content

Commit 949d59b

Browse files
tom-tanmr-c
authored andcommitted
Tentative support for switching nested TypeDSL with saladVersion
1 parent 510ebce commit 949d59b

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
lines changed

schema_salad/metaschema.py

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

6364
def __init__(
6465
self,
@@ -73,6 +74,7 @@ def __init__(
7374
idx: Optional[IdxType] = None,
7475
imports: Optional[List[str]] = None,
7576
includes: Optional[List[str]] = None,
77+
salad_version: Optional[str] = None,
7678
) -> None:
7779
"""Create a LoadingOptions object."""
7880
self.original_doc = original_doc
@@ -138,6 +140,11 @@ def __init__(
138140
self.vocab = _vocab
139141
self.rvocab = _rvocab
140142

143+
if salad_version:
144+
self.salad_version = salad_version
145+
else:
146+
self.salad_version = "v1.0"
147+
141148
if namespaces is not None:
142149
self.vocab = self.vocab.copy()
143150
self.rvocab = self.rvocab.copy()
@@ -592,9 +599,14 @@ def resolve(
592599
doc_ = doc_[0:-1]
593600

594601
if doc_.endswith("[]"):
595-
items = self.resolve(doc_[0:-2], baseuri, loadingOptions)
596-
if isinstance(items, str):
597-
items = expand_url(items, baseuri, loadingOptions, False, True, self.refScope)
602+
salad_versions = [int(v) for v in loadingOptions.salad_version[1:].split(".")]
603+
items = "" # type: Union[list[Union[dict[str, Any], str]], dict[str, Any], str]
604+
if salad_versions < [1, 3]:
605+
items = expand_url(doc_[0:-2], baseuri, loadingOptions, False, True, self.refScope)
606+
else:
607+
items = self.resolve(doc_[0:-2], baseuri, loadingOptions)
608+
if isinstance(items, str):
609+
items = expand_url(items, baseuri, loadingOptions, False, True, self.refScope)
598610
expanded = {"type": "array", "items": items} # type: Union[Dict[str, Any], str]
599611
else:
600612
expanded = expand_url(doc_, baseuri, loadingOptions, False, True, self.refScope)
@@ -708,6 +720,9 @@ def _document_load(
708720
loadingOptions,
709721
)
710722

723+
if "saladVersion" in doc:
724+
loadingOptions.salad_version = doc["saladVersion"]
725+
711726
if docuri != baseuri:
712727
loadingOptions.idx[docuri] = loadingOptions.idx[baseuri]
713728

schema_salad/python_codegen_support.py

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

6061
def __init__(
6162
self,
@@ -70,6 +71,7 @@ def __init__(
7071
idx: Optional[IdxType] = None,
7172
imports: Optional[List[str]] = None,
7273
includes: Optional[List[str]] = None,
74+
salad_version: Optional[str] = None,
7375
) -> None:
7476
"""Create a LoadingOptions object."""
7577
self.original_doc = original_doc
@@ -135,6 +137,11 @@ def __init__(
135137
self.vocab = _vocab
136138
self.rvocab = _rvocab
137139

140+
if salad_version:
141+
self.salad_version = salad_version
142+
else:
143+
self.salad_version = "v1.0"
144+
138145
if namespaces is not None:
139146
self.vocab = self.vocab.copy()
140147
self.rvocab = self.rvocab.copy()
@@ -589,9 +596,14 @@ def resolve(
589596
doc_ = doc_[0:-1]
590597

591598
if doc_.endswith("[]"):
592-
items = self.resolve(doc_[0:-2], baseuri, loadingOptions)
593-
if isinstance(items, str):
594-
items = expand_url(items, baseuri, loadingOptions, False, True, self.refScope)
599+
salad_versions = [int(v) for v in loadingOptions.salad_version[1:].split(".")]
600+
items = "" # type: Union[list[Union[dict[str, Any], str]], dict[str, Any], str]
601+
if salad_versions < [1, 3]:
602+
items = expand_url(doc_[0:-2], baseuri, loadingOptions, False, True, self.refScope)
603+
else:
604+
items = self.resolve(doc_[0:-2], baseuri, loadingOptions)
605+
if isinstance(items, str):
606+
items = expand_url(items, baseuri, loadingOptions, False, True, self.refScope)
595607
expanded = {"type": "array", "items": items} # type: Union[Dict[str, Any], str]
596608
else:
597609
expanded = expand_url(doc_, baseuri, loadingOptions, False, True, self.refScope)
@@ -705,6 +717,9 @@ def _document_load(
705717
loadingOptions,
706718
)
707719

720+
if "saladVersion" in doc:
721+
loadingOptions.salad_version = doc["saladVersion"]
722+
708723
if docuri != baseuri:
709724
loadingOptions.idx[docuri] = loadingOptions.idx[baseuri]
710725

schema_salad/ref_resolver.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ def __init__(
205205
self.subscopes: Dict[str, str] = {}
206206
self.secondaryFile_dsl_fields: Set[str] = set()
207207
self.allow_attachments = allow_attachments
208+
self.salad_version = "v1.0"
208209

209210
self.add_context(ctx)
210211

@@ -637,8 +638,12 @@ def _type_dsl(
637638
t_ = t_[0:-1]
638639

639640
if t_.endswith("[]"):
640-
items = self._type_dsl(t_[0:-2], lc, filename)
641-
cmap = CommentedMap((("type", "array"), ("items", items)))
641+
salad_versions = [int(v) for v in self.salad_version[1:].split(".")]
642+
if salad_versions < [1, 3]:
643+
cmap = CommentedMap((("type", "array"), ("items", t_[0:-2])))
644+
else:
645+
items = self._type_dsl(t_[0:-2], lc, filename)
646+
cmap = CommentedMap((("type", "array"), ("items", items)))
642647
cmap.lc.add_kv_line_col("type", lc)
643648
cmap.lc.add_kv_line_col("items", lc)
644649
cmap.lc.filename = filename

schema_salad/tests/test_examples.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,23 @@ 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"
359+
ra, _ = ldr.resolve_all(cmap({"type": "File[][]"}), "")
360+
assert {"type": {"items": "File[]", "type": "array"}} == ra
361+
362+
363+
def test_nested_typedsl_ref() -> None:
364+
ldr = Loader({})
365+
ldr.add_context(
366+
{
367+
"File": "http://example.com/File",
368+
"null": "http://example.com/null",
369+
"array": "http://example.com/array",
370+
"type": {"@type": "@vocab", "typeDSL": True},
371+
}
372+
)
373+
ldr.salad_version = "v1.3"
374+
358375
ra, _ = ldr.resolve_all(cmap({"type": "File[][]"}), "")
359376
assert {"type": {"items": {"items": "File", "type": "array"}, "type": "array"}} == ra
360377

0 commit comments

Comments
 (0)