Table of Contents

1. 前言



2. 释义


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.


2.1 deprecated


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.


int32 old_field = 6 [deprecated=true];

在field定义后面添加这样一点代码就能在Java里生成一段@Deprecated annotation。根据语言的不同,从这个option而产生的编译反应可能各不相同,这就是option的作用。

2.2 jstype

官方解释说明:[email protected]#L494

The jstype option determines the JavaScript type used for values of the field. The option is permitted only for 64 bit integral and fixed types(int64, uint64, sint64, fixed64, sfixed64). A field with jstype JS_STRING is represented as JavaScript string, which avoids loss of precision that can happen when a large value is converted to a floating point JavaScript. Specifying JS_NUMBER for the jstype causes the generated JavaScript code to use the JavaScript “number” type. The behavior of the default option JS_NORMAL is implementation dependent.


int64 isbn = 1 [jstype=JS_STRING];


3. 功能全集


The complete list of available options is defined in google/protobuf/descriptor.proto.

4. 自定义Option


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.