Skip to content

Create instance from proto #1

@lfvilella

Description

@lfvilella

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions