如何在我的Java代码和.proto文件之间共享枚举值 [英] How do I Share Enum values between my Java code and .proto file
问题描述
java代码:
public enum位置{
UNDEF(0),HOME(1),WORK(2);
...
}
.proto文件对应代码:
消息地址{
枚举位置{
UNDEF = 0;
HOME = 1;
WORK = 2;
}
可选位置addressLocation;
...
}
保持同步的东西的最佳解决方案通常是代码生成;确定您的定义是单一真理点,并从中创建其他定义。协议缓冲区具有内置Java代码生成支持具有自动处理重复枚举值的漂亮功能。
I have a class that I wish to protobuf. in that class one of the fields is an enum (in a class of it's own). Can I avoid defining an identical enum value in my .proto file ? Or will I have to manually make sure the enum definition in the java code is the same as in the .proto file?
java code:
public enum Location {
UNDEF(0),HOME(1), WORK(2);
...
}
.proto file corresponding code:
message Address{
enum location {
UNDEF = 0;
HOME = 1;
WORK = 2;
}
optional location addressLocation;
...
}
The best solution for keeping things like this in sync is often code generation; determine which of your definitions is the Single Point of Truth, and create the others from it. Protocol Buffers has built-in support for Java code generation with nifty features like automatically handling duplicate enum values.
这篇关于如何在我的Java代码和.proto文件之间共享枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!