Comments in textural protobuf?

shoosh picture shoosh · Sep 6, 2011 · Viewed 10k times · Source

I'm using textual protobuf files for system configuration.
One problem I have with this is that the serialized protobuf format does not support comments.
Is there any way around this?
I'm talking about the textual serialized data format, not the scheme definition.
Was this problem solved somewhere by someone?

Answer

sanchitarora picture sanchitarora · Jan 20, 2015

EDITED ANSWER FOR TEXTUAL PROTOBUF FORMAT

Textual Protobuf format supports comments using the # syntax. I could not find a reference for the same in any online documentation but have used the same in projects in the past so I put together a small example that one can test with:

Sample message description - [SampleProtoSchema.proto]

message SampleProtoSchema {
  optional int32 first_val = 1; // This supports C/C++ style comments
  optional int32 second_val = 2;
}

Sample text message - [SampleTextualProto.prototxt]

# This is how textual protobuf format supports comments
first_val: 12 # can also be inline comments
# This is another comment
second_val: 23

Compile and test -

> protoc --python_out=. SampleProtoSchema.proto
>
> ipython
[1]: import SampleProtoSchema_pb2
[2]: sps = SampleProtoSchema_pb2.SampleProtoSchema()
[3]: from google.protobuf import text_format
[4]: with open('SampleTextualProto.prototxt', 'r') as f:
         text_format.Merge(f.read(), sps)
[5]: sps.first_val
[5]> 12
[6]: sps.second_val
[6]> 23

OLD ANSWER

Protobuf Message Description Format supports C/C++-style comments using the // syntax.

Check Adding Comments section here: https://developers.google.com/protocol-buffers/docs/proto

This could be a newer addition since when the question was asked but this was one of the first links that shows up on Google search and the answer is not helpful.