The answer to this is a little bit complicated and depends on whether
you're using proto2 or proto3. Enums and int32 do share the same wire
format, but you have to be careful with unknown enum values. In proto2, if
you parse an unknown enum value then it will end up in the unknown field
set and the enum field will appear to be absent. So if you migrate your
field from an enum to int32 then you have to be aware that binaries using
the old schema will have this parsing behavior if they try to parse an int
that wasn't in the original enum definition. Actually to be fair, the same
problem can occur any time you add new values to a proto2 enum.

We fixed this problem in proto3 by making proto3 enums "open", meaning they
can store arbitrary integer values normally instead of treating them like
unknown fields. So in proto3, moving from an enum to int32 is safer.

Overall I think this is probably a safe change even in proto2 if you do it
carefully, especially if you can make sure all binaries get updated before
you start using int values that weren't included in the original enum.

On Wed, Jan 6, 2021 at 7:55 AM vole zheng <volezh...@gmail.com> wrote:

> Hi,
>
> I have a similar problem with this one
> <https://stackoverflow.com/questions/26810188/protocol-buffers-and-enums-combinations>.
> Now I'd like to change an Enum to an int32, but I'm wondering if it's a
> safe and compatible change?
>
> From this section
> <https://developers.google.com/protocol-buffers/docs/proto#updating>, it
> seems to only mention 'int32, uint32, int64, uint64, and bool are all
> compatible'. But from this section
> <https://developers.google.com/protocol-buffers/docs/encoding#structure>,
> it seems 'int32, int64, uint32, uint64, sint32, sint64, bool, enum' are
> using the same wire format. So is enum and int32 compatible?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to protobuf+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/protobuf/a4c61eaa-ca14-4ef3-8cb3-b0a54ed27c77n%40googlegroups.com
> <https://groups.google.com/d/msgid/protobuf/a4c61eaa-ca14-4ef3-8cb3-b0a54ed27c77n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/protobuf/CADqAXr5pt%2Bc4SN_bUadTnuH%3D3UdDjb34BVHgR-8pPYmPg%3DOoVQ%40mail.gmail.com.

Reply via email to