Extending Protobuf Messages

守給你的承諾、 提交于 2019-12-04 19:00:41

问题


I have many different schemas, however there are a set of fields which every schema contains. I was wondering if there was a way to have a different schema extend a parent schema and inherit its fields. For example this is what I want:

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

Such that Child1 and Child2 also contain the fields common1 and common2 (and potentially more) from Parent.

Is this possible and if so how?


回答1:


This is not the exact answer to your question but we can do something like this to share common parameters.

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

}

Other option is to use extend keyword

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


来源:https://stackoverflow.com/questions/29263507/extending-protobuf-messages

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