Skip to content

Commit

Permalink
msggen: Add VersioningCheck
Browse files Browse the repository at this point in the history
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
cdecker authored and rustyrussell committed Apr 6, 2023
1 parent 60b12ec commit 168bc54
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions contrib/msggen/msggen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def run(rootdir: Path):
p.apply(service)
OptionalPatch().apply(service)

# Run the checks here, we should eventually split that out to a
# separate subcommand
VersioningCheck().check(service)
generator_chain = GeneratorChain()

add_handler_gen_grpc(generator_chain, meta)
Expand Down
36 changes: 36 additions & 0 deletions contrib/msggen/msggen/checks.py
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")

0 comments on commit 168bc54

Please sign in to comment.