Skip to content

Commit 1829427

Browse files
tom-tanmr-c
authored andcommitted
Add checks for redundant fields in codegen-ed code (#169)
* Move attrs to a class variable in codegen
1 parent 6505d0a commit 1829427

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

schema_salad/codegen.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,13 @@ def codegen(lang, # type: str
9090

9191
cg.declare_field(fieldpred, tl, f.get("doc"), optional)
9292

93-
cg.end_class(rec["name"])
93+
field_names = []
94+
for f in rec.get("fields", []):
95+
jld = f.get("jsonldPredicate")
96+
name = f["name"]
97+
field_names.append(shortname(name))
98+
99+
cg.end_class(rec["name"], field_names)
94100

95101
rootType = list(documentRoots)
96102
rootType.append({

schema_salad/codegen_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def begin_class(self, classname, extends, doc, abstract):
3838
# type: (Text, List[Text], Text, bool) -> None
3939
raise NotImplementedError()
4040

41-
def end_class(self, classname):
42-
# type: (Text) -> None
41+
def end_class(self, classname, field_names):
42+
# type: (Text, List[Text]) -> None
4343
raise NotImplementedError()
4444

4545
def type_loader(self, t):

schema_salad/java_codegen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def begin_class(self, classname, extends, doc, abstract):
6868
void Load() {
6969
""")
7070

71-
def end_class(self, classname):
71+
def end_class(self, classname, field_names):
7272
with open(os.path.join(self.outdir, "%s.java" % self.current_class), "a") as f:
7373
f.write("""
7474
}

schema_salad/python_codegen.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,27 @@ def save(self):
8888
r = {}
8989
""")
9090

91-
def end_class(self, classname):
92-
# type: (Text) -> None
91+
def end_class(self, classname, field_names):
92+
# type: (Text, List[Text]) -> None
9393

9494
if self.current_class_is_abstract:
9595
return
9696

9797
self.out.write("""
98+
for k in doc.keys():
99+
if k not in self.attrs:
100+
errors.append(SourceLine(doc, k, str).makeError("invalid field `%s`, expected one of: {attrstr}" % (k)))
101+
break
102+
98103
if errors:
99-
raise ValidationException(\"Trying '%s'\\n\"+\"\\n\".join(errors))
100-
""" % self.safe_name(classname))
104+
raise ValidationException(\"Trying '{class_}'\\n\"+\"\\n\".join(errors))
105+
""".
106+
format(attrstr=", ".join(["`%s`" % f for f in field_names]),
107+
class_=self.safe_name(classname)))
108+
self.serializer.write(" return r\n\n")
109+
110+
self.serializer.write(" attrs = frozenset({attrs})\n".format(attrs=field_names))
101111

102-
self.serializer.write(" return r\n")
103112
self.out.write(self.serializer.getvalue())
104113
self.out.write("\n\n")
105114

0 commit comments

Comments
 (0)