Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Add serialized descriptorprotos to python classes #56

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/betterproto2_compiler/plugin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions src/betterproto2_compiler/templates/template.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down Expand Up @@ -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 %}
Expand Down