Skip to content

Commit

Permalink
Fix support for 'subscope' in python codegen (#594)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tetron authored Sep 13, 2022
1 parent 120e991 commit 9bd6086
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 25 deletions.
9 changes: 7 additions & 2 deletions schema_salad/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ def codegen(
gen.type_loader(field["type"]), True, False, None
)
gen.declare_id_field(
fieldpred, uri_loader, field.get("doc"), optional, subscope
fieldpred,
uri_loader,
field.get("doc"),
optional,
)
break

Expand Down Expand Up @@ -186,7 +189,9 @@ def codegen(
if jld == "@id":
continue

gen.declare_field(fieldpred, type_loader, field.get("doc"), optional)
gen.declare_field(
fieldpred, type_loader, field.get("doc"), optional, subscope
)

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

Expand Down
2 changes: 1 addition & 1 deletion schema_salad/codegen_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def declare_field(
fieldtype: TypeDef,
doc: Optional[str],
optional: bool,
subscope: str,
) -> None:
"""Output the code to load the given field."""
raise NotImplementedError()
Expand All @@ -106,7 +107,6 @@ def declare_id_field(
fieldtype: TypeDef,
doc: str,
optional: bool,
subscope: Optional[str],
) -> None:
"""Output the code to handle the given ID field."""
raise NotImplementedError()
Expand Down
7 changes: 2 additions & 5 deletions schema_salad/dotnet_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ def declare_field(
fieldtype: TypeDef,
doc: Optional[str],
optional: bool,
subscope: str,
) -> None:
"""Output the code to load the given field."""
if self.current_class_is_abstract:
Expand Down Expand Up @@ -753,13 +754,12 @@ def declare_id_field(
fieldtype: TypeDef,
doc: str,
optional: bool,
subscope: Optional[str],
) -> None:
"""Output the code to handle the given ID field."""
self.id_field_type = fieldtype
if self.current_class_is_abstract:
return
self.declare_field(name, fieldtype, doc, True)
self.declare_field(name, fieldtype, doc, True, "")
if optional:
opt = """{safename} = "_" + Guid.NewGuid();""".format(
safename=self.safe_name(name)
Expand All @@ -769,9 +769,6 @@ def declare_id_field(
fieldname=shortname(name)
)

if subscope is not None:
name = name + subscope

self.current_loader.write(
"""
if ({safename} == null)
Expand Down
6 changes: 2 additions & 4 deletions schema_salad/java_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ def declare_field(
fieldtype: TypeDef,
doc: Optional[str],
optional: bool,
subscope: str,
) -> None:
fieldname = name
property_name = self.property_name(fieldname)
Expand Down Expand Up @@ -692,12 +693,11 @@ def declare_id_field(
fieldtype: TypeDef,
doc: str,
optional: bool,
subscope: Optional[str],
) -> None:
if self.current_class_is_abstract:
return

self.declare_field(name, fieldtype, doc, True)
self.declare_field(name, fieldtype, doc, True, "")
if optional:
set_uri = """
Boolean __original_is_null = {safename} == null;
Expand Down Expand Up @@ -725,8 +725,6 @@ def declare_id_field(
}}
__baseUri = (String) {safename};
"""
if subscope is not None:
name = name + subscope

self.current_loader.write(
set_uri.format(safename=self.safe_name(name), fieldname=shortname(name))
Expand Down
29 changes: 22 additions & 7 deletions schema_salad/python_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,12 +457,11 @@ def declare_id_field(
fieldtype: TypeDef,
doc: str,
optional: bool,
subscope: Optional[str],
) -> None:
if self.current_class_is_abstract:
return

self.declare_field(name, fieldtype, doc, True)
self.declare_field(name, fieldtype, doc, True, "")

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

if subscope is not None:
name = name + subscope

self.out.write(
"""
__original_{safename}_is_none = {safename} is None
Expand All @@ -492,7 +488,12 @@ def declare_id_field(
)

def declare_field(
self, name: str, fieldtype: TypeDef, doc: Optional[str], optional: bool
self,
name: str,
fieldtype: TypeDef,
doc: Optional[str],
optional: bool,
subscope: str,
) -> None:

if self.current_class_is_abstract:
Expand All @@ -506,12 +507,25 @@ def declare_field(
spc = " "
else:
spc = ""

if subscope:
self.out.write(
"""
{spc} subscope_baseuri = expand_url('{subscope}', baseuri, loadingOptions, True)
""".format(
subscope=subscope, spc=spc
)
)
baseurivar = "subscope_baseuri"
else:
baseurivar = "baseuri"

self.out.write(
"""{spc} try:
{spc} {safename} = load_field(
{spc} _doc.get("{fieldname}"),
{spc} {fieldtype},
{spc} baseuri,
{spc} {baseurivar},
{spc} loadingOptions,
{spc} )
{spc} except ValidationException as e:
Expand All @@ -526,6 +540,7 @@ def declare_field(
safename=self.safe_name(name),
fieldname=shortname(name),
fieldtype=fieldtype.name,
baseurivar=baseurivar,
spc=spc,
)
)
Expand Down
2 changes: 1 addition & 1 deletion schema_salad/sourceline.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def strip_dup_lineno(text: str, maxline: Optional[int] = None) -> str:


def cmap(
d: Union[int, float, str, Dict[str, Any], List[Any], None],
d: Union[int, float, str, MutableMapping[str, Any], MutableSequence[Any], None],
lc: Optional[List[int]] = None,
fn: Optional[str] = None,
) -> Union[int, float, str, CommentedMap, CommentedSeq, None]:
Expand Down
7 changes: 2 additions & 5 deletions schema_salad/typescript_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ def declare_field(
fieldtype: TypeDef,
doc: Optional[str],
optional: bool,
subscope: str,
) -> None:
"""Output the code to load the given field."""
safename = self.safe_name(name)
Expand Down Expand Up @@ -642,10 +643,9 @@ def declare_id_field(
fieldtype: TypeDef,
doc: str,
optional: bool,
subscope: Optional[str],
) -> None:
"""Output the code to handle the given ID field."""
self.declare_field(name, fieldtype, doc, True)
self.declare_field(name, fieldtype, doc, True, "")
if optional:
opt = """{safename} = "_" + uuidv4()""".format(
safename=self.safe_name(name)
Expand All @@ -655,9 +655,6 @@ def declare_id_field(
fieldname=shortname(name)
)

if subscope is not None:
name = name + subscope

self.current_loader.write(
"""
const original{safename}IsUndefined = ({safename} === undefined)
Expand Down

0 comments on commit 9bd6086

Please sign in to comment.