Proto3:Using Other Message Types - 使用其他message类型

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类型作为字段类型。举个例子,比如说你想将Result message放入到每个SearchResponse message中 - 为了做到这一点,你可以在相同改的.proto文件中定义一个Result message 类型,然后将SearchReponse其中一个字段指定为Result类型。

message SearchResponse {
  repeated Result results = 1;
}

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

Importing Definitions - 导入定义

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类型是被定义在了和SearchResponse相同的文件中 - 如果你要使用的字段类型已经被定义在了其他.proto文件中会怎样呢?

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:
你可以通过导入它们的方式来使用外部定义。为了导入其他.proto文件,你需要在你文件的顶部添加一个导入声明:

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.
默认情况下,你仅能使用直接导入的.proto文件里的定义。然而有时你可能需要将.proto文件移动到新位置。你可以在老位置放置占位.proto文件,该占位文件利用public概念(关键字)转发所有的导入到新位置,而不是直接移动.proto文件然后更新所有的调用位置

Note that the public import functionality is not available in Java.
注意:public导入功能在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标志所指定的一组目录中所有被导入的文件。如果标志未被给定,就会在编译器被调用的所在目录中搜索。通常来说你应该将--proto_path 标志设置为项目根目录,对所有导入使用完全限定名。

Using proto2 Message Types - 使用proto2 Message类型

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).
有可能导入proto2message类型,应用于你的proto3 message类型,反之亦然。然而,proto2 枚举不能直接应用于proto3 语法中(如果导入的proto2消息使用它们是可以的)

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容