protobuf 嵌套示例


1.嵌套 Message

message Person

{

required string name = 1;

required int32 id = 2;        // Unique ID number for this person.

optional string email = 3;

enum PhoneType

{

MOBILE = 0;

HOME = 1;

WORK = 2;

}

message PhoneNumber

{

required string number = 1;

optional PhoneType type = 2 [default = HOME];

}

repeated PhoneNumber phone = 4;

}

2. Import Message

在一個 .proto 文件中,還可以用 Import 關鍵字引入在其他 .proto 文件中定義的消息,這可以稱做 Import Message,或者 Dependency Message

import common.header;

message youMsg

{

required common.info_header header = 1;

required string youPrivateData = 2;

}

Import Message 的用處主要在於提供了方便的代碼管理機制,類似 C 語言中的頭文件。您可以將一些公用的 Message 定義在一個 package 中,然后在別的 .proto 文件中引入該 package,進而使用其中的消息定義。Google Protocol Buffer 可以很好地支持嵌套 Message 和引入 Message

3.更多關於動態編譯的內容,參數[1]

原文

[1]http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/

[2]https://developers.google.com/protocol-buffers/docs/overview?hl=zh-CN

[3]https://developers.google.com/protocol-buffers/docs/cpptutorial?hl=zh-CN


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM