Proto3:Using Other Message Types - 使用其他message類(lèi)型

You can use other message types as field types. For example, let's say you wanted to include Result messages in each SearchResponse message – to do this, you can define a Result message type in the same .proto and then specify a field of type Result in SearchResponse:
你可以使用其他message類(lèi)型作為字段類(lèi)型。舉個(gè)例子,比如說(shuō)你想將Result message放入到每個(gè)SearchResponse message中 - 為了做到這一點(diǎn),你可以在相同改的.proto文件中定義一個(gè)Result message 類(lèi)型,然后將SearchReponse其中一個(gè)字段指定為Result類(lèi)型。

message SearchResponse {
  repeated Result results = 1;
}

message Result {
  string url = 1;
  string title = 2;
  repeated string snippets = 3;
}

Importing Definitions - 導(dǎo)入定義

In the above example, the Result message type is defined in the same file as SearchResponse – what if the message type you want to use as a field type is already defined in another .proto file?
上面的例子中,Result message類(lèi)型是被定義在了和SearchResponse相同的文件中 - 如果你要使用的字段類(lèi)型已經(jīng)被定義在了其他.proto文件中會(huì)怎樣呢?

You can use definitions from other .proto files by importing them. To import another .proto's definitions, you add an import statement to the top of your file:
你可以通過(guò)導(dǎo)入它們的方式來(lái)使用外部定義。為了導(dǎo)入其他.proto文件,你需要在你文件的頂部添加一個(gè)導(dǎo)入聲明:

import "myproject/other_protos.proto";

By default, you can use definitions only from directly imported .proto files. However, sometimes you may need to move a .proto file to a new location. Instead of moving the .proto file directly and updating all the call sites in a single change, you can put a placeholder .proto file in the old location to forward all the imports to the new location using the import public notion.
默認(rèn)情況下,你僅能使用直接導(dǎo)入的.proto文件里的定義。然而有時(shí)你可能需要將.proto文件移動(dòng)到新位置。你可以在老位置放置占位.proto文件,該占位文件利用public概念(關(guān)鍵字)轉(zhuǎn)發(fā)所有的導(dǎo)入到新位置,而不是直接移動(dòng).proto文件然后更新所有的調(diào)用位置

Note that the public import functionality is not available in Java.
注意:public導(dǎo)入功能在Java中不適用。

import public dependencies can be transitively relied upon by any code importing the proto containing the import public statement. For example:

// new.proto
// All definitions are moved here
// old.proto
// This is the proto that all clients are importing.
import public "new.proto";
import "other.proto";
// client.proto
import "old.proto";
// You use definitions from old.proto and new.proto, but not other.proto

The protocol compiler searches for imported files in a set of directories specified on the protocol compiler command line using the -I/--proto_path flag. If no flag was given, it looks in the directory in which the compiler was invoked. In general you should set the --proto_path flag to the root of your project and use fully qualified names for all imports.
protocol 編譯器搜索protocol編譯器命令行中用-I/--proto_path標(biāo)志所指定的一組目錄中所有被導(dǎo)入的文件。如果標(biāo)志未被給定,就會(huì)在編譯器被調(diào)用的所在目錄中搜索。通常來(lái)說(shuō)你應(yīng)該將--proto_path 標(biāo)志設(shè)置為項(xiàng)目根目錄,對(duì)所有導(dǎo)入使用完全限定名。

Using proto2 Message Types - 使用proto2 Message類(lèi)型

It's possible to import proto2 message types and use them in your proto3 messages, and vice versa. However, proto2 enums cannot be used directly in proto3 syntax (it's okay if an imported proto2 message uses them).
有可能導(dǎo)入proto2message類(lèi)型,應(yīng)用于你的proto3 message類(lèi)型,反之亦然。然而,proto2 枚舉不能直接應(yīng)用于proto3 語(yǔ)法中(如果導(dǎo)入的proto2消息使用它們是可以的)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容