1

In Java, libraries like protostuff allow you to generate buffers from a Java POJO approximately like so:

Schema<Foo> schema = RuntimeSchema.getSchema(Foo.class);
...
protostuff = ProtostuffIOUtil.toByteArray(foo, schema, buffer);

I've been trying to find a similar solution for Python, but except for attempting to programmatically build Descriptors and FieldDescriptors (which come with their own challenges and problems since 4.x.x), I couldn't find anything. Is this simply impossible in Python, just isn't implemented anywhere, or am I missing something obvious here?

zerohedge
  • 3,185
  • 4
  • 28
  • 63

1 Answers1

1

The alternative Python protobuf libraries of pure-protobuf and python-betterproto both have their own syntax that can be used directly from Python, without a .proto file.

It however doesn't work for completely plain Python objects, as you still need to specify the field types and tag numbers (example from pure-protobuf):

@message
@dataclass
class SearchRequest:
    query: str = field(1, default='')
    page_number: int32 = field(2, default=int32(0))
    result_per_page: int32 = field(3, default=int32(0))
jpa
  • 10,351
  • 1
  • 28
  • 45