Can I define a constant string in protobuf?

I use protobuf enums to exchange values ​​between a C ++ application and a Java application. Thus, the same ( int ) values ​​are shared between languages, and the values ​​are available at compile time. Can I do something similar with a string, somehow defining it in a common .proto file?

+5
source share
1 answer

Not really.

There are several hacks that you can use. None of them work, and (I think) both go to proto3:

  • Define a message with a string field and give it a default value, which is your constant value. However, Protobuf 3 seems to remove the default values.
  • Use “customizable options,” which should probably be called “annotations,” since they are very similar to annotations in Java or other languages. You can declare an annotation of a type string, and then annotate some dummy declaration using annotation and use a constant value. However, custom options are based on extensions that are also removed in proto3, so I assume that custom options were also removed. (This is the answer suggested here: fooobar.com/questions/1244104 / .... )

FWIW, Cap'n Proto , an alternative to protocol buffers, supports constants. (Disclosure: I am the author of Cap'n Proto, as well as most of Google Protobuf v2.)

+10
source

Source: https://habr.com/ru/post/1244101/


All Articles