-
Notifications
You must be signed in to change notification settings - Fork 911
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a visitor that ensures every new field has at least an `added` field, and that we don't change the `added` or `deprecated` annotation after the fact.
- Loading branch information
1 parent
60b12ec
commit 168bc54
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from abc import ABC | ||
from msggen import model | ||
|
||
|
||
class Check(ABC): | ||
"""A check is a visitor that throws exceptions on inconsistencies. | ||
""" | ||
def visit(self, field: model.Field) -> None: | ||
pass | ||
|
||
def check(self, service: model.Service) -> None: | ||
def recurse(f: model.Field): | ||
# First recurse if we have further type definitions | ||
if isinstance(f, model.ArrayField): | ||
self.visit(f.itemtype) | ||
recurse(f.itemtype) | ||
elif isinstance(f, model.CompositeField): | ||
for c in f.fields: | ||
self.visit(c) | ||
recurse(c) | ||
# Now visit ourselves | ||
self.visit(f) | ||
for m in service.methods: | ||
recurse(m.request) | ||
recurse(m.response) | ||
|
||
|
||
class VersioningCheck(Check): | ||
"""Check that all schemas have the `added` and `deprecated` annotations. | ||
""" | ||
def visit(self, f: model.Field) -> None: | ||
if not hasattr(f, "added"): | ||
raise ValueError(f"Field {f.path} is missing the `added` annotation") | ||
if not hasattr(f, "deprecated"): | ||
raise ValueError(f"Field {f.path} is missing the `deprecated` annotation") |