proto buf
22 July 2016
Field Rule
proto2
required optional repeated
proto3
{none} == optional repeated
Field type
Scalar
string bool bytes float double int32 / int64 uint32 / uint64 sint32 / sint64 fixed32 / fixed64 sfixed32 / sfixed64
Enumeration
message Customer {
enum Type {
REGULAR = 0;
MEMBER = 1;
SPONSOR = 2;
}
Type type = 1;
}
Message Type
Field Name
- lower case
- use underscore (_) as separator
- converted to correct style for each language
Field Tag
- unique
- integer
- reserved [19000, 19999]
- smaller values are more efficient
Default values
proto2
optional int32 results_per_page = 1 [default = 25];
proto3
no default value, always zero value
protoc --js_out=import_style=commonjs, binary:. messages.proto
Other Message Members
- oneof
- map
- any
oneof access_type {
string email = 1;
string username = 2;
}
oneof
- only one field can hold value
- setting one field clears others
maps
message Customer {
map<string, string> email_addresses = 1;
// keys must be strings or integer types
}
any
import "google/protobuf/any.proto";
message Customer {
int32 id = 1;
string username = 2;
repeated google.protobuf.Any details = 3;
}
reversed Tags
the reversed tag can not be used if the tag is set as reverved
reserved 1;
reserved 1,2,3;
reserved 5 to 10;
reserved 2, 4, 15 to 25;
recommanded
- don’t change tags for existing fields
- reserve old tags and field names
blog comments powered by Disqus