mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-24 09:34:24 +01:00
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:
committed by
Rusty Russell
parent
60b12ec096
commit
168bc54700
@@ -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)
|
||||
|
||||
36
contrib/msggen/msggen/checks.py
Normal file
36
contrib/msggen/msggen/checks.py
Normal 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")
|
||||
Reference in New Issue
Block a user