inheritance - 扩展Protobuf消息

标签 inheritance serialization protocol-buffers extends

我有许多不同的模式,但是每个模式都包含一组字段。我想知道是否有办法让其他架构扩展父架构并继承其字段。例如,这就是我想要的:

message Parent {
    required string common1 = 0;
    optional string common2 = 1;
}

message Child1 { // can we extend the Parent?
    // I want common1, common2 to be fields here
    required int c1 = 2;
    required string c2 = 3;
}

message Child2 { // can we extend Parent?
    // I want common1, common2 to be fields here
    repeated int c3 = 2;
    repeated string c4 = 3;
}


这样,Child1和Child2还包含来自Parent的common1和common2字段(可能还有更多)。

这有可能吗?

最佳答案

这不是您所提问题的确切答案,但是我们可以做这样的事情来共享通用参数。

message Child1 { 
    required int c1 = 2;
    required string c2 = 3;
}

message Child2 { 
    required int c1 = 2;
    required string c2 = 3;
}

message Request {
    required string common1 = 0;
    optional string common2 = 1;
    oneof msg { Child1 c1 = 2; Child2 c2 = 3; }

}


另一种选择是使用扩展关键字

message Parent {
    required string common1 = 0;
    optional string common2 = 1;
}

message Child1 { 
    extend Parent
    {       
        optional Child1 c1 = 100;
    }

    required int c1 = 2;
    required string c2 = 3;
}

关于inheritance - 扩展Protobuf消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29263507/

相关文章:

php - 无法在长文本字段中保存序列化数组

java - 无法从 java 文件 Intellij id 导入 proto 文件

java - 类C实现了接口(interface)B,而接口(interface)B扩展了接口(interface)A,你能说C实现了A吗?

C++ 方法继承,返回类型取决于类

c++ - 使用基类指针访问继承变量

java - 为多个版本的 Hadoop 编写 Java 代码的好方法是什么?

python - 将 proto 缓冲区转换为 ProtoRPC

PHP - 重新定义方法并调用相同方法的祖先版本

python - Django REST Framework 嵌套序列化器无效

java - Gson无法在Android(Java)中序列化LocalDateTime数据