Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create instance from proto #1

Open
lfvilella opened this issue Jan 30, 2022 · 2 comments
Open

Create instance from proto #1

lfvilella opened this issue Jan 30, 2022 · 2 comments

Comments

@lfvilella
Copy link

lfvilella commented Jan 30, 2022

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)

@Ed-XCF
Copy link
Owner

Ed-XCF commented Feb 1, 2022

Nice idea! You can implement it as you want

@Ed-XCF
Copy link
Owner

Ed-XCF commented Feb 1, 2022

maybe .from_proto need to support passing some parameters like including_default_value_fields preserving_proto_field_name etc

Repository owner deleted a comment from Arpitkandwal Feb 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants