Proto3: Packages - 包

You can add an optional package specifier to a .proto file to prevent name clashes between protocol message types.
你可以在.proto文件中添加一個(gè)可選package標(biāo)識(shí)符,以防止protocol message類型命名沖突。

package foo.bar;
message Open { ... }

You can then use the package specifier when defining fields of your message type:
之后你就可以在message類型中定義字段時(shí)使用該package標(biāo)識(shí)符:

message Foo {
  ...
  foo.bar.Open open = 1;
  ...
}

The way a package specifier affects the generated code depends on your chosen language:
包標(biāo)識(shí)符如何影響生成的代碼取決于你所選語(yǔ)言:

  • In C++ the generated classes are wrapped inside a C++ namespace. For example, Open would be in the namespace foo::bar.
    在C++中,生成的類被包裹在一個(gè)C++命名空間中。舉個(gè)例子,Open類會(huì)在命名空間foo::bar下。

  • In Java and Kotlin, the package is used as the Java package, unless you explicitly provide an option java_package in your .proto file.
    在Java和Kotlin中,package會(huì)被用作Java package,除非你在你的.proto文件中顯示的提供一個(gè)java_package選項(xiàng)

  • In Python, the package directive is ignored, since Python modules are organized according to their location in the file system.
    在Python中,package指令會(huì)被忽略,因?yàn)镻ython模塊是根據(jù)它們?cè)谖募到y(tǒng)中的位置規(guī)劃的。

  • In Go, the package is used as the Go package name, unless you explicitly provide an option go_package in your .proto file.
    在Go中,package被用作Go package名,除非你在你的.proto文件中顯示地提供一個(gè)go_package選項(xiàng)。

  • In Ruby, the generated classes are wrapped inside nested Ruby namespaces, converted to the required Ruby capitalization style (first letter capitalized; if the first character is not a letter, PB_ is prepended). For example, Open would be in the namespace Foo::Bar.
    在Ruby中,生成的類是被包裝在內(nèi)嵌Ruby命名空間中,被轉(zhuǎn)化為所要求的Ruby大寫風(fēng)格(首字母大寫;如果首字符不是字母,則在首部追加PB_)。舉個(gè)例子,Open類會(huì)在命名空間Foo::Bar下。

  • In C# the package is used as the namespace after converting to PascalCase, unless you explicitly provide an option csharp_namespace in your .proto file. For example, Open would be in the namespace Foo.Bar.
    在C#中,package轉(zhuǎn)化為帕斯卡命名(大駝峰命名)后被用作命名空間,除非你在你的.proto文件中顯示地提供一個(gè)csharp_namespace選項(xiàng)。舉個(gè)例子,Open類會(huì)在命名空間Foo.Bar下。

Packages and Name Resolution - 包與名稱解析

Type name resolution in the protocol buffer language works like C++: first the innermost scope is searched, then the next-innermost, and so on, with each package considered to be "inner" to its parent package. A leading '.' (for example, .foo.bar.Baz) means to start from the outermost scope instead.
在protocol buffer語(yǔ)言中類型名稱解析類似C++:首先最內(nèi)層作用域被搜索到,然后下一個(gè)最內(nèi)層等等。每一個(gè)包被認(rèn)為是其父包的“內(nèi)部”。一個(gè)領(lǐng)先'.'(舉個(gè)例子,.foo.bar.Baz)表示從最外層的作用域開始。

The protocol buffer compiler resolves all type names by parsing the imported .proto files. The code generator for each language knows how to refer to each type in that language, even if it has different scoping rules.
protocol buffer編譯器通過(guò)解析導(dǎo)入的.proto文件解析所有類型名稱。每種語(yǔ)言代碼生成器知道如何引用該語(yǔ)言中的每個(gè)類型。盡管它有不同的作用域規(guī)則。

?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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