protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Method to generate a proto file from a FileDescriptor

Open perj opened this issue 3 years ago • 2 comments

Is your feature request related to a problem? Please describe. Similar to DebugString in C++ and GetDebugString in Python, I'd like to be able to create a textual proto file from a FileDescriptor. The main reason for this is that this gives a canonical textual representation that can be used for both printing and comparison. I realise those functions have Debug in their name but I've seen other projects using them this way.

Describe the solution you'd like I would either expect a new method in the FileDescriptor interface or a function in the protoreflect package doing the equivalent thing.

Describe alternatives you've considered Changing the default output of Format() could be considered but it's current output is also useful. It doesn't relate to Python or C++ in any way though, which would be a key difference.

Additional context Originally filed here: https://github.com/protocolbuffers/protobuf/issues/9865 I left the example of the current Format method output in that issue.

perj avatar Apr 28 '22 06:04 perj

The protodesc package can convert a protoreflect.FileDescriptor to a FileDescriptorProto, which can then be formatted or operated on in whatever fashion you desire.

Note, by the way, that GetDebugString does not return a canonical textual representation: There is no guarantee that this text is consistent between implementations, nor that it will not change over time.

neild avatar Apr 28 '22 18:04 neild

I understand. But perhaps there should be. :)

Ok, so I can use FileDescriptorProto to implement this myself, that's good to know. I think it could still be useful to go from descriptor to proto file, if there's not any strong reason not to. It would allow for only keeping track of the descriptor and generating the proto when it's required to display it to the user.

The original place I saw this was the Confluent Kafka Schema Registry, where it's possible to upload the binary descriptor to the Schema Registry but then also possible to fetch back a generated proto file.

perj avatar Apr 28 '22 20:04 perj