Protocol Buffers 版本 2 语言规范

Protocol Buffers 语言(proto2)版本 2 的语言规范参考。

语法使用扩展巴克斯-诺尔范式 (EBNF)指定。

|   alternation
()  grouping
[]  option (zero or one time)
{}  repetition (any number of times)

有关使用 proto2 的更多信息,请参阅语言指南

词法元素

字母和数字

letter = "A" ... "Z" | "a" ... "z"
capitalLetter =  "A" ... "Z"
decimalDigit = "0" ... "9"
octalDigit   = "0" ... "7"
hexDigit     = "0" ... "9" | "A" ... "F" | "a" ... "f"

标识符

ident = letter { letter | decimalDigit | "_" }
fullIdent = ident { "." ident }
messageName = ident
enumName = ident
fieldName = ident
oneofName = ident
mapName = ident
serviceName = ident
rpcName = ident
streamName = ident
messageType = [ "." ] { ident "." } messageName
enumType = [ "." ] { ident "." } enumName
groupName = capitalLetter { letter | decimalDigit | "_" }

整数字面量

intLit     = decimalLit | octalLit | hexLit
decimalLit = [-] ( "1" ... "9" ) { decimalDigit }
octalLit   = [-] "0" { octalDigit }
hexLit     = [-] "0" ( "x" | "X" ) hexDigit { hexDigit }

浮点数字面量

floatLit = [-] ( decimals "." [ decimals ] [ exponent ] | decimals exponent | "."decimals [ exponent ] ) | "inf" | "nan"
decimals  = [-] decimalDigit { decimalDigit }
exponent  = ( "e" | "E" ) [ "+" | "-" ] decimals

布尔值

boolLit = "true" | "false"

字符串字面量

strLit = strLitSingle { strLitSingle }
strLitSingle = ( "'" { charValue } "'" ) | ( '"' { charValue } '"' )
charValue = hexEscape | octEscape | charEscape | unicodeEscape | unicodeLongEscape | /[^\0\n\\]/
hexEscape = '\' ( "x" | "X" ) hexDigit [ hexDigit ]
octEscape = '\' octalDigit [ octalDigit [ octalDigit ] ]
charEscape = '\' ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | '\' | "'" | '"' )
unicodeEscape = '\' "u" hexDigit hexDigit hexDigit hexDigit
unicodeLongEscape = '\' "U" ( "000" hexDigit hexDigit hexDigit hexDigit hexDigit |
                              "0010" hexDigit hexDigit hexDigit hexDigit

空语句

emptyStatement = ";"

常量

constant = fullIdent | ( [ "-" | "+" ] intLit ) | ( [ "-" | "+" ] floatLit ) |
                strLit | boolLit | MessageValue

MessageValue文本格式语言规范中定义。

语法

语法语句用于定义 protobuf 版本。

syntax = "syntax" "=" ("'" "proto2" "'" | '"' "proto2" '"') ";"

导入语句

导入语句用于导入另一个 .proto 的定义。

import = "import" [ "weak" | "public" ] strLit ";"

示例

import public "other.proto";

包说明符可用于防止协议消息类型之间的名称冲突。

package = "package" fullIdent ";"

示例

package foo.bar;

选项

选项可以在 proto 文件、消息、枚举和服务中使用。选项可以是 protobuf 定义的选项或自定义选项。有关更多信息,请参阅语言指南中的选项

option = "option" optionName  "=" constant ";"
optionName = ( ident | bracedFullIdent ) { "." ( ident | bracedFullIdent ) }
bracedFullIdent = "(" ["."] fullIdent ")"

例如

option java_package = "com.example.foo";

字段

字段是协议缓冲区消息的基本元素。字段可以是普通字段、组字段、oneof 字段或映射字段。字段具有标签、类型和字段编号。

label = "required" | "optional" | "repeated"
type = "double" | "float" | "int32" | "int64" | "uint32" | "uint64"
      | "sint32" | "sint64" | "fixed32" | "fixed64" | "sfixed32" | "sfixed64"
      | "bool" | "string" | "bytes" | messageType | enumType
fieldNumber = intLit;

普通字段

每个字段都有标签、类型、名称和字段编号。它可能具有字段选项。

field = label type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
fieldOptions = fieldOption { ","  fieldOption }
fieldOption = optionName "=" constant

示例

optional foo.bar nested_message = 2;
repeated int32 samples = 4 [packed=true];

组字段

请注意,此功能已弃用,创建新消息类型时不应使用它 - 请改用嵌套消息类型。

组是在消息定义中嵌套信息的一种方法。组名称必须以大写字母开头。

group = label "group" groupName "=" fieldNumber messageBody

示例

repeated group Result = 1 {
    required string url = 1;
    optional string title = 2;
    repeated string snippets = 3;
}

Oneof 和 Oneof 字段

一个 oneof 由 oneof 字段和一个 oneof 名称组成。Oneof 字段没有标签。

oneof = "oneof" oneofName "{" { option | oneofField } "}"
oneofField = type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"

示例

oneof foo {
    string name = 4;
    SubMessage sub_message = 9;
}

映射字段

映射字段具有键类型、值类型、名称和字段编号。键类型可以是任何整数或字符串类型。请注意,键类型不能是枚举。

mapField = "map" "<" keyType "," type ">" mapName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
keyType = "int32" | "int64" | "uint32" | "uint64" | "sint32" | "sint64" |
          "fixed32" | "fixed64" | "sfixed32" | "sfixed64" | "bool" | "string"

示例

map<string, Project> projects = 3;

扩展和保留

扩展和保留是声明字段编号或字段名称范围的消息元素。

扩展

扩展声明消息中的一系列字段编号可用于第三方扩展。其他人可以在自己的 .proto 文件中使用这些数字标签为您的消息类型声明新字段,而无需编辑原始文件。

extensions = "extensions" ranges ";"
ranges = range { "," range }
range =  intLit [ "to" ( intLit | "max" ) ]

示例

extensions 100 to 199;
extensions 4, 20 to max;

保留

保留声明消息中不能使用的字段编号或字段名称范围。

reserved = "reserved" ( ranges | strFieldNames ) ";"
strFieldNames = strFieldName { "," strFieldName }
strFieldName = "'" fieldName "'" | '"' fieldName '"'

示例

reserved 2, 15, 9 to 11;
reserved "foo", "bar";

顶级定义

枚举定义

枚举定义由名称和枚举体组成。枚举体可以包含选项、枚举字段和保留语句。

enum = "enum" enumName enumBody
enumBody = "{" { option | enumField | emptyStatement | reserved } "}"
enumField = ident "=" [ "-" ] intLit [ "[" enumValueOption { ","  enumValueOption } "]" ]";"
enumValueOption = optionName "=" constant

示例

enum EnumAllowingAlias {
  option allow_alias = true;
  EAA_UNSPECIFIED = 0;
  EAA_STARTED = 1;
  EAA_RUNNING = 2 [(custom_option) = "hello world"];
}

消息定义

消息由消息名称和消息体组成。消息体可以包含字段、嵌套枚举定义、嵌套消息定义、扩展语句、扩展、组、选项、oneof、映射字段和保留语句。消息不能在同一消息架构中包含两个名称相同的字段。

message = "message" messageName messageBody
messageBody = "{" { field | enum | message | extend | extensions | group |
option | oneof | mapField | reserved | emptyStatement } "}"

示例

message Outer {
  option (my_option).a = true;
  message Inner {   // Level 2
    required int64 ival = 1;
  }
  map<int32, string> my_map = 2;
  extensions 20 to 30;
}

消息内部声明的任何实体都不能具有冲突的名称。以下所有内容都禁止

message MyMessage {
  optional string foo = 1;
  message foo {}
}

message MyMessage {
  optional string foo = 1;
  oneof foo {
    string bar = 2;
  }
}

message MyMessage {
  optional string foo = 1;
  extend Extendable {
    optional string foo = 2;
  }
}

message MyMessage {
  optional string foo = 1;
  enum E {
    foo = 0;
  }
}

扩展

如果同一或导入的 .proto 文件中的消息为扩展保留了范围,则可以扩展该消息。

extend = "extend" messageType "{" {field | group} "}"

示例

extend Foo {
  optional int32 bar = 126;
}

服务定义

service = "service" serviceName "{" { option | rpc | emptyStatement } "}"
rpc = "rpc" rpcName "(" [ "stream" ] messageType ")" "returns" "(" [ "stream" ]
messageType ")" (( "{" { option | emptyStatement } "}" ) | ";" )

示例

service SearchService {
  rpc Search (SearchRequest) returns (SearchResponse);
}

Proto 文件

proto = [syntax] { import | package | option | topLevelDef | emptyStatement }
topLevelDef = message | enum | extend | service

一个 .proto 文件示例

syntax = "proto2";
import public "other.proto";
option java_package = "com.example.foo";
enum EnumAllowingAlias {
  option allow_alias = true;
  EAA_UNSPECIFIED = 0;
  EAA_STARTED = 1;
  EAA_RUNNING = 1;
  EAA_FINISHED = 2 [(custom_option) = "hello world"];
}
message Outer {
  option (my_option).a = true;
  message Inner {   // Level 2
    required int64 ival = 1;
  }
  repeated Inner inner_message = 2;
  optional EnumAllowingAlias enum_field = 3;
  map<int32, string> my_map = 4;
  extensions 20 to 30;
}
message Foo {
  optional group GroupMessage = 1 {
    optional bool a = 1;
  }
}