Inheritance 扩展Protobuf消息
我有许多不同的模式,但是每个模式都包含一组字段。我想知道是否有一种方法可以让不同的模式扩展父模式并继承其字段。例如,这就是我想要的:Inheritance 扩展Protobuf消息,inheritance,serialization,protocol-buffers,extends,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
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还包含来自父级的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; }
}
另一个选项是使用extend关键字
不支持继承,但作为穷人的解决方案,您可以使用嵌套结构,其中
Child1
和Child2
的第一个字段的类型为Parent
。为了访问“基类”中的字段,您必须先显式地访问父类型的字段。还有一些扩展。proto buffer 3中不支持扩展,因此父级必须知道所有可能的子级,这些子级可以位于不同的文件中。。。ugly@gtato查看Avro联盟:)所有模式都需要在一个文件中,我看到它们增长到几十个lol
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;
}