proto文件之间共享枚举值

proto文件之间共享枚举值

本文介绍了如何在我的Java代码和.proto文件之间共享枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我想要的protobuf类。在那个类中,一个字段是枚举(在它自己的类中)。我可以避免在.proto文件中定义相同的枚举值吗?或者我必须手动确保java代码中的枚举定义与.proto文件中的相同?



java代码:

  public enum位置{
UNDEF(0),HOME(1),WORK(2);
...
}

.proto文件对应代码:

 消息地址{
枚举位置{
UNDEF = 0;
HOME = 1;
WORK = 2;
}
可选位置addressLocation;
...
}


解决方案

保持同步的东西的最佳解决方案通常是代码生成;确定您的定义是单一真理点,并从中创建其他定义。协议缓冲区具有具有自动处理重复枚举值的漂亮功能。


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文件之间共享枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:24