Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Protocol Buffers, is it safe to move enum from inside message to outside message?

I've run into a use case where I'd like to move an enum declared inside a protocol buffer message to outside the message so that other messages van use the same Enum.

ie, I'm wondering if there are any issues moving from this

message Message {
   enum Enum {
       VALUE1 = 1;
       VALUE2 = 2;
   } 
   optional Enum enum_value = 1;
}

to this

enum Enum {
    VALUE1 = 1;
    VALUE2 = 2;
}
message Message {
    optional Enum enum_value = 1;
}

Would this cause any issues de-serializing data created with the first protocol buffer definition into the second?

like image 661
Daniel Gephardt Avatar asked Aug 17 '17 15:08

Daniel Gephardt


1 Answers

It doesn't change the serialization data at all - the location / name of the enums are irrelevant for the actual data, since it just stores the integer value.

What might change is how some languages consume the enum, i.e. how they qualify it. Is it X.Y.Foo, X.Foo, or just Foo. Note that since enums follow C++ naming/scoping rules, some things (such as conflicts) aren't an issue: but it may impact some languages as consumers.

So: if you're the only consumer of the .proto, you're absolutely fine here. If you have shared the .proto with other people, it may be problematic to change it unless they are happy to update their code to match any new qualification requirements.

like image 140
Marc Gravell Avatar answered Sep 20 '22 12:09

Marc Gravell