Proto3:Options - 选项

Individual declarations in a .proto file can be annotated with a number of options. Options do not change the overall meaning of a declaration, but may affect the way it is handled in a particular context. The complete list of available options is defined in google/protobuf/descriptor.proto.
.proto文件中的单个声明可以使用多个选型进行注释。选项不会改变声明的整体语义,但是可以影响其在特殊上下文中的处理方式。可用选项的完整列表定义在google/protobuf/descriptor.proto中。

Some options are file-level options, meaning they should be written at the top-level scope, not inside any message, enum, or service definition. Some options are message-level options, meaning they should be written inside message definitions. Some options are field-level options, meaning they should be written inside field definitions. Options can also be written on enum types, enum values, oneof fields, service types, and service methods; however, no useful options currently exist for any of these.
有些选项是文件级别选项,意味着他们应该写在最高级作用域里,而不是在任何message、枚举或者服务定义中。有些选项是message级别选项,意味着他们应该写在message定义中。有些选项是字段级别选项,意味着他们应该写在字段定义中。选项也可以写在枚举类型、枚举值、oneof字段、服务类型和服务方法中;然而,目前没有选项可以满足所有情况。

Here are a few of the most commonly used options:
以下是一些最常用的选项:

  • java_package (file option): The package you want to use for your generated Java/Kotlin classes. If no explicit java_package option is given in the .proto file, then by default the proto package (specified using the "package" keyword in the .proto file) will be used. However, proto packages generally do not make good Java packages since proto packages are not expected to start with reverse domain names. If not generating Java or Kotlin code, this option has no effect.
    java_package(文件选项):你要用于生成的Java/Kotlin类的包。如果.proto文件中没有明确的java_package选项,会默认使用proto文件中的package(在.proto文件中package关键字所指定)。然而,proto包通常不会成为好的Java包因为proto包不应该反向域名开头。如果不生成Java或Kotlin代码,该选项不起作用。
option java_package = "com.example.foo";
  • java_outer_classname (file option): The class name (and hence the file name) for the wrapper Java class you want to generate. If no explicit java_outer_classname is specified in the .proto file, the class name will be constructed by converting the .proto file name to camel-case (so foo_bar.proto becomes FooBar.java). If the java_multiple_files option is disabled, then all other classes/enums/etc. generated for the .proto file will be generated within this outer wrapper Java class as nested classes/enums/etc. If not generating Java code, this option has no effect.
    java_outer_classname(文件选项):你要生成的包装器Java类的类名(之后的文件名)。.proto文件中如果没有明确指定java_output_classname,类名将通过把.proto文件名转换为驼峰命命名法(foo_bar.proto变为FooBar.java)来构造。
    如果禁用 java_multiple_files 选项,则为 .proto 文件生成的所有其他类/枚举/等将作为嵌套类/枚举/等在此外部包装器 Java 类中生成。如果不生成Java代码,则该选项不起作用。
option java_outer_classname = "Ponycopter";
  • java_multiple_files (file option): If false, only a single .java file will be generated for this .proto file, and all the Java classes/enums/etc. generated for the top-level messages, services, and enumerations will be nested inside of an outer class (see java_outer_classname). If true, separate .java files will be generated for each of the Java classes/enums/etc. generated for the top-level messages, services, and enumerations, and the wrapper Java class generated for this .proto file won't contain any nested classes/enums/etc. This is a Boolean option which defaults to false. If not generating Java code, this option has no effect.
    java_multiple_files(文件选项):如果为false,.proto文件和所有的Java类、枚举等仅生成单个java文件。生成的顶层消息、服务和枚举将被内嵌到一个外部类中(参见java_outer_classname)。如果为true,每个Java类、枚举等将回生成一个单独的java文件。该.proto文件生成的顶层消息、服务、枚举和包装器java类将不会内嵌任何类、枚举等。这是一个默认值为false的布尔选项。如果不生成Java代码,该选项不起作用。
option java_multiple_files = true;
  • optimize_for (file option): Can be set to SPEED, CODE_SIZE, or LITE_RUNTIME. This affects the C++ and Java code generators (and possibly third-party generators) in the following ways:
    optimize_for(文件选项):可被设置为SPEED, CODE_SIZE, 或LITE_RUNTIME。该选项作用于C++和Java代码生成器(和可能的三方生成器),方式如下:

    • SPEED (default): The protocol buffer compiler will generate code for serializing, parsing, and performing other common operations on your message types. This code is highly optimized.
      SPEED(默认的):protocol buffer编译器会为序列化、解析以及消息类型的其他常见操作的执行生成代码。该代码是高度优化的。

    • CODE_SIZE: The protocol buffer compiler will generate minimal classes and will rely on shared, reflection-based code to implement serialialization, parsing, and various other operations. The generated code will thus be much smaller than with SPEED, but operations will be slower. Classes will still implement exactly the same public API as they do in SPEED mode. This mode is most useful in apps that contain a very large number of .proto files and do not need all of them to be blindingly fast.
      CODE_SIZE:Protocol Buffer 编译器将生成最少的类,并将依赖共享的、基于反射的代码来实现序列化、解析和各种其他操作。因此生成的代码比SPEED小得多,但是速度会慢。类中将实现和SPEED模式下完全相同的API。该模式下对于包含了大量的.proto文件但不要求它们所有文件都很快的应用最有用。

    • LITE_RUNTIME: The protocol buffer compiler will generate classes that depend only on the "lite" runtime library (libprotobuf-lite instead of libprotobuf). The lite runtime is much smaller than the full library (around an order of magnitude smaller) but omits certain features like descriptors and reflection. This is particularly useful for apps running on constrained platforms like mobile phones. The compiler will still generate fast implementations of all methods as it does in SPEED mode. Generated classes will only implement the MessageLite interface in each language, which provides only a subset of the methods of the full Message interface.
      protocol buffer编译器仅生成依赖于“精简版”运行时库(libprotobuf-lite 而不是libprotobuf)。精简版运行时比完整版库小的多,但是省略了某些功能如描述符和反射。这对于在手机等受限平台上的应用程序特别有用。编译器仍然会像在SPEED模式下一样生成所有方法的快速实现。生成的类将只实现每种语言的MessageLite接口,它只提供所有Message接口的一个子集。

option optimize_for = CODE_SIZE;
  • cc_enable_arenas (file option): Enables arena allocation for C++ generated code.
    cc_enable_arenas(文件选项):为生成的C++代码启用竞技场分配。

  • objc_class_prefix (file option): Sets the Objective-C class prefix which is prepended to all Objective-C generated classes and enums from this .proto. There is no default. You should use prefixes that are between 3-5 uppercase characters as recommended by Apple. Note that all 2 letter prefixes are reserved by Apple.
    objc_class_prefix(文件选项):设置Objective-C类前缀,该前缀附加到此.proto中所有Objective-C生成的类和枚举。无默认值。你应该使用3-5个大写字符的前缀,如[苹果推荐的](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Conventions/Conventions.html#//apple_ref/doc/uid/TP40011210-CH10-SW4)那样。注意所有的2个字母的前缀均由苹果保留。

  • deprecated (field option): If set to true, indicates that the field is deprecated and should not be used by new code. In most languages this has no actual effect. In Java, this becomes a @Deprecated annotation. In the future, other language-specific code generators may generate deprecation annotations on the field's accessors, which will in turn cause a warning to be emitted when compiling code which attempts to use the field. If the field is not used by anyone and you want to prevent new users from using it, consider replacing the field declaration with a reservedstatement.
    deprecated(字段选项):如果设置为true,表明该字段已弃用,不应该在新的代码中使用。多数语言中该字段没有实际效果。在Java中,该选项会变为@Deprecated注释。未来,其他特殊特定语言代码生成器或许会在字段访问器上生成弃用注释,这反过来会导致在编译尝试使用该字段的代码时发出警告。如果该字段不会再被任何人使用,而你想方式新用使用它的话,考虑使用reserved语句代替该字段声明。

int32 old_field = 6 [deprecated = true];

Custom Options - 自定义选项

Protocol Buffers also allows you to define and use your own options. This is an advanced feature which most people don't need. If you do think you need to create your own options, see the Proto2 Language Guide for details. Note that creating custom options uses extensions, which are permitted only for custom options in proto3.
Protocol Buffers 也允许你定义和使用你自己的选项。这是一项高级选项,大部分人不需要它。如果你认为你需要创建你自己的选项,查看详情请参阅Proto2 语言指导。注意创建自定义选项时使用extensions,这仅允许用于proto3中自定义选项。

你可能感兴趣的:(Proto3:Options - 选项)