protobuf2pydantic
protobuf2pydantic copied to clipboard
Create instance from proto
Hi there,
Very nice pkg, I was testing it and quickly (for my use case [1]) I needed to convert from proto
instance to pydantic
, then I was wondering what do you think about adding a helper method to the generated classes such .from_proto
? if you agree in the idea I can jump in in the implementation (or if you prefer implementing all good) .
[1] just playing in a proof of concept project that from a proto
definition I generate the following services rpc
(grpc), rest
(fastapi), graphql, I basically 1) compile the proto
and add the grpc server 2) use your lib to generate pydantic that empowers fastapi 3) from the generated pydantic
I use pydantic2graphene
to expose for graphql
from protobuf2pydantic import msg2py
from pydantic import validator
import transaction_pb2
class AmountResponse(msg2py(transaction_pb2.AmountResponse)):
@validator("amount")
def non_negative(cls, v):
assert v >= 0
return v
msg_proto = transaction_pb2.AmountResponse(amount=10.01, currency="USD")
amount_pydantic = AmountResponse.from_proto(msg_proto)
Nice idea! You can implement it as you want
maybe .from_proto
need to support passing some parameters like including_default_value_fields
preserving_proto_field_name
etc