Is it possible to define an alias for type (enum or message) in google protobuf?

南笙酒味 提交于 2020-01-03 15:27:04

问题


I have two enums in my proto file which define almost the same values.

Is it possible to cut out one of them and to leave an alias to keep all the code working?

Example:

enum A {
   a = 0;
   b = 1;
}
enum B {
   a = 0;
   b = 1;
}

I want to have something like typedef in c++:

enum A {
   a = 0;
   b = 1;
}

typedef A B;

I haven't found this in documentation. Are there any workarounds ?


回答1:


This is an old question, but if some people are still interested, It is possible to create alias on enum with protobuf now

enum EnumAllowingAlias {
  option allow_alias = true;
  UNKNOWN = 0;
  STARTED = 1;
  RUNNING = 1;
}
enum EnumNotAllowingAlias {
  UNKNOWN = 0;
  STARTED = 1;
  // RUNNING = 1;  // Uncommenting this line will cause a compile error inside Google and a warning message outside.
}

As explained in the official documentation here. You just need to enable the alias option by adding option allow_alias = true;




回答2:


As of protobuf version 3, this is not possible.



来源:https://stackoverflow.com/questions/17533549/is-it-possible-to-define-an-alias-for-type-enum-or-message-in-google-protobuf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!