.proto file version constant

2,498 views
Skip to first unread message

Jesper

unread,
Sep 9, 2010, 2:42:49 AM9/9/10
to Protocol Buffers
I would like to be able to have a version constant in the .proto file,
which can be encoded into messages, so that the communicating parties
can verify that they are using a sufficiently new .proto file. I tried
using a custom FileOption as described here>
http://code.google.com/apis/protocolbuffers/docs/proto.html#options,
but I was unable to compile the resulting code (I don't have the exact
compiler errors at hand at the moment, but they were about missing
definitions related to the Descriptor C++ type).

I could put the protocol version number in a header file, but since
I'm using both C++ and Java I would like to be able to encode the
version such that it can be easily picked up by both C++ and Java.

users ....

unread,
Sep 9, 2010, 11:45:33 AM9/9/10
to Protocol Buffers
Can this be accomplished using a required field with a default value?
Each time you update the .proto you update the default value.
Obviously if you were collaborating with teams you would have to
either remove the setter methods or trust developers.

Jason Hsueh

unread,
Sep 10, 2010, 1:10:11 PM9/10/10
to Jesper, Protocol Buffers
Can you provide a small reproduction of the problem? A couple common errors:
- custom options need to be specified as "option (<option_name>) = <value>;" (parentheses around the identifier)
- if you're using the option in a different package than the one in which its defined, it needs to be qualified with the package name

Also, note that protocol buffers were originally designed to get away from having to deal with version numbers (see "A bit of history": http://code.google.com/apis/protocolbuffers/docs/overview.html) Typically, you would define your protocol in such a way that different versions are still compatible. When that's not practical/possible, people may just switch to entirely new messages.


--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.


Jesper

unread,
Sep 10, 2010, 2:39:48 PM9/10/10
to Protocol Buffers


On Sep 9, 5:45 pm, "users ...." <moofis...@gmail.com> wrote:
> Can this be accomplished using a required field with a default value?

As I understand it, no. I think this has been discussed previously,
and the problem is that default values are not encoded in the message.

Jesper

unread,
Sep 10, 2010, 2:50:14 PM9/10/10
to Protocol Buffers

On Sep 10, 7:10 pm, Jason Hsueh <jas...@google.com> wrote:
> Can you provide a small reproduction of the problem? A couple common errors:
> - custom options need to be specified as "option (<option_name>) = <value>;"
> (parentheses around the identifier)
> - if you're using the option in a different package than the one in which
> its defined, it needs to be qualified with the package name

Yes, I'm pretty sure I followed the instructions in the documentation.

> Also, note that protocol buffers were originally designed to get away from
> having to deal with version numbers (see "A bit of history":http://code.google.com/apis/protocolbuffers/docs/overview.html) Typically,
> you would define your protocol in such a way that different versions are
> still compatible. When that's not practical/possible, people may just switch
> to entirely new messages.

We have a Java-program A which spawns a C++-program B, and A needs to
know that B can deal with the messages it sends, and if new
functionality in the protocol is added, A needs to be able to figure
out if B is sufficiently new to handle the new functionality. So this
is strictly not so much a protocol version, but rather a program
version.

I solved this by having one version number encoded in the C++-program
and another in the Java-program, and a version-message is exchanged
when they hook up. If B does not present a new enough version number,
the program is terminated. The only drawback is that I have to change
the version number in two places when adding functionality to the
protocol.


Daniel Wright

unread,
Sep 10, 2010, 3:03:49 PM9/10/10
to Jesper, Protocol Buffers


2010/9/10 Jesper <jesper....@gmail.com>
Can you use and enum in the .proto file, with an enum value that you change (that's kind-of like a constant defined in the .proto file).  Then do the version number exchange like you do now, but have both the java and c++ code get the version number from the enum "constant".

Jesper

unread,
Sep 10, 2010, 3:30:15 PM9/10/10
to Protocol Buffers


On Sep 10, 9:03 pm, Daniel Wright <dwri...@google.com> wrote:
> 2010/9/10 Jesper <jesper.eskil...@gmail.com>
That may be a good idea, yes.
Reply all
Reply to author
Forward
0 new messages