How do I Share Enum values between my Java code and .proto file

感情迁移 提交于 2019-12-04 06:19:40

问题


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;
...
}

回答1:


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.



来源:https://stackoverflow.com/questions/19130965/how-do-i-share-enum-values-between-my-java-code-and-proto-file

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