Skip to content

Commit 9bd6086

Browse files
authored
Fix support for 'subscope' in python codegen (#594)
The way subscopes are supposed to work is to amend the base URI used for loading that subtree of the document. The implementation just changed the name of the variable (?) This is fixed for Python. The TypesSript, java and dotnet codegens are only fixed so that they aren't broken and MyPy won't complain, but I did not actually add the equivalent codegen.
1 parent 120e991 commit 9bd6086

File tree

7 files changed

+37
-25
lines changed

7 files changed

+37
-25
lines changed

schema_salad/codegen.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ def codegen(
144144
gen.type_loader(field["type"]), True, False, None
145145
)
146146
gen.declare_id_field(
147-
fieldpred, uri_loader, field.get("doc"), optional, subscope
147+
fieldpred,
148+
uri_loader,
149+
field.get("doc"),
150+
optional,
148151
)
149152
break
150153

@@ -186,7 +189,9 @@ def codegen(
186189
if jld == "@id":
187190
continue
188191

189-
gen.declare_field(fieldpred, type_loader, field.get("doc"), optional)
192+
gen.declare_field(
193+
fieldpred, type_loader, field.get("doc"), optional, subscope
194+
)
190195

191196
gen.end_class(rec["name"], field_names)
192197

schema_salad/codegen_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def declare_field(
9696
fieldtype: TypeDef,
9797
doc: Optional[str],
9898
optional: bool,
99+
subscope: str,
99100
) -> None:
100101
"""Output the code to load the given field."""
101102
raise NotImplementedError()
@@ -106,7 +107,6 @@ def declare_id_field(
106107
fieldtype: TypeDef,
107108
doc: str,
108109
optional: bool,
109-
subscope: Optional[str],
110110
) -> None:
111111
"""Output the code to handle the given ID field."""
112112
raise NotImplementedError()

schema_salad/dotnet_codegen.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ def declare_field(
569569
fieldtype: TypeDef,
570570
doc: Optional[str],
571571
optional: bool,
572+
subscope: str,
572573
) -> None:
573574
"""Output the code to load the given field."""
574575
if self.current_class_is_abstract:
@@ -753,13 +754,12 @@ def declare_id_field(
753754
fieldtype: TypeDef,
754755
doc: str,
755756
optional: bool,
756-
subscope: Optional[str],
757757
) -> None:
758758
"""Output the code to handle the given ID field."""
759759
self.id_field_type = fieldtype
760760
if self.current_class_is_abstract:
761761
return
762-
self.declare_field(name, fieldtype, doc, True)
762+
self.declare_field(name, fieldtype, doc, True, "")
763763
if optional:
764764
opt = """{safename} = "_" + Guid.NewGuid();""".format(
765765
safename=self.safe_name(name)
@@ -769,9 +769,6 @@ def declare_id_field(
769769
fieldname=shortname(name)
770770
)
771771

772-
if subscope is not None:
773-
name = name + subscope
774-
775772
self.current_loader.write(
776773
"""
777774
if ({safename} == null)

schema_salad/java_codegen.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ def declare_field(
591591
fieldtype: TypeDef,
592592
doc: Optional[str],
593593
optional: bool,
594+
subscope: str,
594595
) -> None:
595596
fieldname = name
596597
property_name = self.property_name(fieldname)
@@ -692,12 +693,11 @@ def declare_id_field(
692693
fieldtype: TypeDef,
693694
doc: str,
694695
optional: bool,
695-
subscope: Optional[str],
696696
) -> None:
697697
if self.current_class_is_abstract:
698698
return
699699

700-
self.declare_field(name, fieldtype, doc, True)
700+
self.declare_field(name, fieldtype, doc, True, "")
701701
if optional:
702702
set_uri = """
703703
Boolean __original_is_null = {safename} == null;
@@ -725,8 +725,6 @@ def declare_id_field(
725725
}}
726726
__baseUri = (String) {safename};
727727
"""
728-
if subscope is not None:
729-
name = name + subscope
730728

731729
self.current_loader.write(
732730
set_uri.format(safename=self.safe_name(name), fieldname=shortname(name))

schema_salad/python_codegen.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,11 @@ def declare_id_field(
457457
fieldtype: TypeDef,
458458
doc: str,
459459
optional: bool,
460-
subscope: Optional[str],
461460
) -> None:
462461
if self.current_class_is_abstract:
463462
return
464463

465-
self.declare_field(name, fieldtype, doc, True)
464+
self.declare_field(name, fieldtype, doc, True, "")
466465

467466
if optional:
468467
opt = """{safename} = "_:" + str(_uuid__.uuid4())""".format(
@@ -473,9 +472,6 @@ def declare_id_field(
473472
fieldname=shortname(name)
474473
)
475474

476-
if subscope is not None:
477-
name = name + subscope
478-
479475
self.out.write(
480476
"""
481477
__original_{safename}_is_none = {safename} is None
@@ -492,7 +488,12 @@ def declare_id_field(
492488
)
493489

494490
def declare_field(
495-
self, name: str, fieldtype: TypeDef, doc: Optional[str], optional: bool
491+
self,
492+
name: str,
493+
fieldtype: TypeDef,
494+
doc: Optional[str],
495+
optional: bool,
496+
subscope: str,
496497
) -> None:
497498

498499
if self.current_class_is_abstract:
@@ -506,12 +507,25 @@ def declare_field(
506507
spc = " "
507508
else:
508509
spc = ""
510+
511+
if subscope:
512+
self.out.write(
513+
"""
514+
{spc} subscope_baseuri = expand_url('{subscope}', baseuri, loadingOptions, True)
515+
""".format(
516+
subscope=subscope, spc=spc
517+
)
518+
)
519+
baseurivar = "subscope_baseuri"
520+
else:
521+
baseurivar = "baseuri"
522+
509523
self.out.write(
510524
"""{spc} try:
511525
{spc} {safename} = load_field(
512526
{spc} _doc.get("{fieldname}"),
513527
{spc} {fieldtype},
514-
{spc} baseuri,
528+
{spc} {baseurivar},
515529
{spc} loadingOptions,
516530
{spc} )
517531
{spc} except ValidationException as e:
@@ -526,6 +540,7 @@ def declare_field(
526540
safename=self.safe_name(name),
527541
fieldname=shortname(name),
528542
fieldtype=fieldtype.name,
543+
baseurivar=baseurivar,
529544
spc=spc,
530545
)
531546
)

schema_salad/sourceline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def strip_dup_lineno(text: str, maxline: Optional[int] = None) -> str:
173173

174174

175175
def cmap(
176-
d: Union[int, float, str, Dict[str, Any], List[Any], None],
176+
d: Union[int, float, str, MutableMapping[str, Any], MutableSequence[Any], None],
177177
lc: Optional[List[int]] = None,
178178
fn: Optional[str] = None,
179179
) -> Union[int, float, str, CommentedMap, CommentedSeq, None]:

schema_salad/typescript_codegen.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ def declare_field(
471471
fieldtype: TypeDef,
472472
doc: Optional[str],
473473
optional: bool,
474+
subscope: str,
474475
) -> None:
475476
"""Output the code to load the given field."""
476477
safename = self.safe_name(name)
@@ -642,10 +643,9 @@ def declare_id_field(
642643
fieldtype: TypeDef,
643644
doc: str,
644645
optional: bool,
645-
subscope: Optional[str],
646646
) -> None:
647647
"""Output the code to handle the given ID field."""
648-
self.declare_field(name, fieldtype, doc, True)
648+
self.declare_field(name, fieldtype, doc, True, "")
649649
if optional:
650650
opt = """{safename} = "_" + uuidv4()""".format(
651651
safename=self.safe_name(name)
@@ -655,9 +655,6 @@ def declare_id_field(
655655
fieldname=shortname(name)
656656
)
657657

658-
if subscope is not None:
659-
name = name + subscope
660-
661658
self.current_loader.write(
662659
"""
663660
const original{safename}IsUndefined = ({safename} === undefined)

0 commit comments

Comments
 (0)