How to define value and id for enum in protobuf? (proto java client)

耗尽温柔 提交于 2019-12-11 09:07:41

问题


I am new to protobuf usage.

i am planning to write protobuf def with enum(s) in it.

is there any way to provide id, value and as well description in it.

after compilation i want generated enum should be equivalent as below example

enum Sample{
  W(0, "W"), P(0, "P"), C(0, "C"), B(0, "B")
  private final int id;
  private final String value;

  private Status(int id, String value) {
    this.id= id;
    this.value = value;
  }
}

Any help is very appreciated.


回答1:


There is no way to generate exactly the Java enum in your example, but you can use "custom options" to add arbitrary annotation to protobuf declarations. See the documentation (scroll down a little bit to "custom options").

import "google/protobuf/descriptor.proto";

extend google.protobuf.EnumValueOptions {
  optional string name = 51234;
}

enum MyEnum {
  FOO = 0 [(name) = "foo"];
  BAR = 1 [(name) = "bar"];
}

The annotation is accessed through the EnumValueDescriptor interface.



来源:https://stackoverflow.com/questions/28961605/how-to-define-value-and-id-for-enum-in-protobuf-proto-java-client

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