From 473f71f0fa0885aebd425bc29e410f224530db4d Mon Sep 17 00:00:00 2001 From: Michael Schuldes Date: Thu, 6 Mar 2025 08:22:38 +0100 Subject: [PATCH] add serialized descriptorproto to python classes --- src/betterproto2_compiler/plugin/models.py | 8 ++++++++ src/betterproto2_compiler/templates/template.py.j2 | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/betterproto2_compiler/plugin/models.py b/src/betterproto2_compiler/plugin/models.py index 52b69d54..844326f6 100644 --- a/src/betterproto2_compiler/plugin/models.py +++ b/src/betterproto2_compiler/plugin/models.py @@ -266,6 +266,10 @@ def custom_methods(self) -> list[str]: return methods_source + @property + def descriptor(self): + return self.proto_obj.SerializeToString() + def is_map(proto_field_obj: FieldDescriptorProto, parent_message: DescriptorProto) -> bool: """True if proto_field_obj is a map, otherwise False.""" @@ -595,6 +599,10 @@ def py_name(self) -> str: def deprecated(self) -> bool: return bool(self.proto_obj.options and self.proto_obj.options.deprecated) + @property + def descriptor(self): + return self.proto_obj.SerializeToString() + @dataclass(kw_only=True) class ServiceCompiler(ProtoContentBase): diff --git a/src/betterproto2_compiler/templates/template.py.j2 b/src/betterproto2_compiler/templates/template.py.j2 index f51a3b39..2ccca896 100644 --- a/src/betterproto2_compiler/templates/template.py.j2 +++ b/src/betterproto2_compiler/templates/template.py.j2 @@ -6,6 +6,10 @@ class {{ enum.py_name | add_to_all }}(betterproto2.Enum): """ {% endif %} + @staticmethod + def _serialized_pb(): + return {{ enum.descriptor }} + {% for entry in enum.entries %} {{ entry.name }} = {{ entry.value }} {% if entry.comment %} @@ -45,6 +49,10 @@ class {{ message.py_name | add_to_all }}(betterproto2.Message): """ {% endif %} + @staticmethod + def _serialized_pb(): + return {{ message.descriptor }} + {% for field in message.fields %} {{ field.get_field_string() }} {% if field.comment %}