msggen: Add VersioningCheck

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.
This commit is contained in:
Christian Decker
2023-03-31 17:52:52 +02:00
committed by Rusty Russell
parent 60b12ec096
commit 168bc54700
2 changed files with 39 additions and 0 deletions

View File

@@ -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)

View File

@@ -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")