so1n / protobuf_to_pydantic

Generate a pydantic.BaseModel with parameter verification function from the Python Message object(by the Protobuf file).
Apache License 2.0
85 stars 7 forks source link

Support `BaseModel` serialization to `Protobuf Message` #11

Open so1n opened 1 year ago

so1n commented 1 year ago

The current step of converting BaseModel to Protobuf Message is tedious and has poor performance. e.g:

from google.protobuf.json_format import ParseDict
from pydantic import BaseModel
from demo_pb2 import DemoMessage

class Demo(BaseModel):
    pass

ParseDict(Demo().dict(), DemoMessage())

Serialization can be optimized in the following ways:

mikey0000 commented 5 months ago

So yeah came here to use this to convert from protobuf to model and am stumped as there isn't really strong documentation for more complex multi-level models or submessage types. Love that this generates all the models so easily so big thank you!

so1n commented 5 months ago

@mikey0000 Protobuf's JsonFormat implementation is more complex there are many cases that can not be dealt with, you need to modify his code to achieve. So I will delay the implementation of this feature.