googleapis / proto-plus-python

Beautiful, idiomatic protocol buffers in Python
Apache License 2.0
171 stars 36 forks source link

Generating .proto file from proto-plus (or vice versa) #420

Open sshumaker opened 11 months ago

sshumaker commented 11 months ago

Thanks for stopping by to let us know something could be better!

Is your feature request related to a problem? Please describe. We want to use the proto-plus library for our own protobuf types, but these protobufs need to work across platform (e.g. C++). It doesn't seem possible to either go from .proto to proto-plus nor the reverse. So today, our only option is to effectively write the definition twice - once for the .proto, and then manually re-implement it in proto-plus to match the .proto definition.

Describe the solution you'd like Ideally, protoc has an option to emit proto_plus classes, not just the low level classes. We'd also be ok with the reverse - using proto-plus as the source of truth, but have a slight preference for .proto. I have to imagine you have some tool at Google that can do this, as you ship proto-plus implementations for some of the cloud offerings and there are also .proto files.

Describe alternatives you've considered Manual re-implementation, so we're writing the protos effectively twice.

techdragon commented 9 months ago

So I did some digging around and it looks like theres a tool that sort of does this... but its currently i need of improvements before I would recommend it to anyone. It appears to be closely coupled to internal use by google and is less flexible than the existing protoc compiler due to this coupling. If there were improvements made to resolve the issue I opened on that project, then this might resolve this issue here. https://github.com/googleapis/gapic-generator-python/issues/1970

Also it looks as though at least the generation from a .proto file has been decided to be out of the scope of this project in the past. https://github.com/googleapis/proto-plus-python/issues/84 which does make sense given the existence of other tools to do that.

xxgreg commented 9 months ago

Regarding .proto to proto-plus python source code generation, I wrote a code generator which generates idiomatic python dataclasses which wrap the official protobuf python objects. I've been successfully using this for a project. But don't have time to maintain it officially. See: https://github.com/xxgreg/python-protodc

I use this to generate protojson requests which are used to communicate with a ConnectRPC service.

I'd definitely appreciate a supported official tool which works with proto-plus-python, as then I wouldn't need to continue maintaining this code.

The generator doesn't target proto-plus-python currently, but it would also be relatively straight forward to fork this generator to target proto-plus instead of my custom python dataclass approach.

I'm swamped, so can't do this myself, but I would be happy to chat with anyone who wanted to give it a shot.