I use protobuf's enums to share values between a C++ app and a Java app. This way, the same (int
) values are shared between languages and the values are available at compile time. Can I do something similar with a string by somehow defining it in the common .proto
file?
A constant string is declared when it is required to be immutable, which means once any data is defined as constant, it cannot be changed. Constant strings are declared as private static final String in Java. These strings are initialized in the class and used in the different methods.
The Protobuf serialization mechanism is given through the protoc application, this compiler will parse the . proto file and will generate as output, source files according to the configured language by its arguments, in this case, C++. You can also obtain more information about, reading the section compiler invocation.
Protobuf treats strings as primitive types and therefore they can not be null.
For bool s, the default value is false. For numeric types, the default value is zero. For enums , the default value is the first value listed in the enum's type definition. This means care must be taken when adding a value to the beginning of an enum value list.
Not really.
There are a few hacks you can use. Neither is a great fit, and (I think) both are going away in proto3:
FWIW, Cap'n Proto, an alternative to protocol buffers, does support constants. (Disclosure: I am the author of Cap'n Proto as well as most of Google's Protobuf v2.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With