扩展Protobuf消息 [英] Extending Protobuf Messages

查看:144
本文介绍了扩展Protobuf消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这样Child1和Child2也包含字段common1和common2(可能还有更多)来自父母。

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

这是可能的,如果是这样的话?

Is this possible and if so how?

推荐答案

这不是你问题的确切答案,但我们可以做类似的事情来分享常见参数。

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

}

其他选项是使用 extend keyword

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

这篇关于扩展Protobuf消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆