Extending Protobuf Messages

前端 未结 1 749
不思量自难忘°
不思量自难忘° 2021-02-07 11:15

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 schem

相关标签:
1条回答
  • 2021-02-07 11:50

    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;
    }
    
    0 讨论(0)
提交回复
热议问题