Enum and int compatibility

5,326 views
Skip to first unread message

Josh Kelley

unread,
Sep 30, 2011, 11:17:30 AM9/30/11
to prot...@googlegroups.com
Are enums compatible with ints?  For example, can I serialize an int now and change it to an enum later without losing the ability to parse old messages?  Under http://code.google.com/apis/protocolbuffers/docs/proto.html#updating, it explains that int32, int64, uint32, uint64, and bool are all interchangeable when updating message types, but there's no description of enumerations.  (The text under enumerations makes it sound like they're compatible, but I'd like to verify.)

--
Josh Kelley

Benjamin Wright

unread,
Sep 30, 2011, 2:50:09 PM9/30/11
to Protocol Buffers
Yes and no....

The wire type encodings are all varint - so yes it is wire compatible.

"Since enum values use varint encoding on the wire" -http://
code.google.com/apis/protocolbuffers/docs/proto.html#enum

However protobuf treats enums a little differently at runtime...

an integer value (int32, etc) is valid as long as it is in range...
while an enum literal is considered an "Unknown Field" if it does not
resolve to a known enum literal.

That is to say that an application expecting the enum will fail to
recognize that the field is even set (unless you're using protocol
buffers 'reflection' and look for it manually) if the value is not one
of the enum literals.

Going enum -> int32 = no loss of data
Going int32 -> enum = possible missing fields (all used values must
have a literal associated with them)


On Sep 30, 11:17 am, Josh Kelley <josh...@gmail.com> wrote:
> Are enums compatible with ints?  For example, can I serialize an int now and
> change it to an enum later without losing the ability to parse old
> messages?  Underhttp://code.google.com/apis/protocolbuffers/docs/proto.html#updating, it
Reply all
Reply to author
Forward
0 new messages