Protocol Buffers Schema compiler
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
protoc --version
protoc -I. --python_out=. <file>.proto
protoc -I. --go_out=. <file>.proto
protoc -I. --cpp_out=. <file>.proto
protoc -I. --descriptor_set_out=<f>.pb
<file>.proto
protoc -I. --encode=<Msg> <file>.proto
protoc -I. --decode=<Msg> <file>.proto
protoc --decode_raw
Concepts
- .proto file schema
- Generated code classes, stubs
- Binary wire data compact, tagged by field number
Invoke protoc
protoc --version
protoc -I <dir> <file>.proto
protoc --proto_path=<dir> <file>.proto
protoc --python_out=<dir> <file>.proto
protoc --cpp_out=<dir> <file>.proto
protoc --java_out=<dir> <file>.proto
protoc --go_out=<dir> --go_opt=paths=source_relative
<file>.proto
protoc --pyi_out=<dir> <file>.proto
protoc --<lang>_out=<opts>:<dir>
<file>.proto
protoc --plugin=protoc-gen-<name>=<path>
--<name>_out=<dir> <file>.proto
protoc @<argsfile>
protoc --fatal_warnings <file>.proto
protoc --helpDescriptors
protoc --descriptor_set_out=<f>.pb
<file>.proto
protoc --descriptor_set_out=<f>.pb --include_imports
<file>.proto
protoc --descriptor_set_out=<f>.pb
--include_source_info <file>.proto
protoc --descriptor_set_in=<f>.pb <Msg>
protoc --dependency_out=<f>.d
<file>.proto
Encode and decode
protoc --decode_raw < <f>.bin
protoc -I. --decode=<pkg.Msg> <file>.proto <
<f>.bin
protoc -I. --encode=<pkg.Msg> <file>.proto <
<f>.txt > <f>.bin
protoc --decode=<pkg.Msg>
--descriptor_set_in=<f>.pb < <f>.bin
protoc --deterministic_output --encode=<pkg.Msg>
<file>.proto
Messages and fields
syntax = "proto3";
package <name>;
import "<other>.proto";
message <Name> { <type> <field> =
<n>; }
optional <type> <field> = <n>;
repeated <type> <field> = <n>;
map<<key>, <value>> <field> =
<n>;
oneof <name> { <type> <a> = 1;
<type> <b> = 2; }
enum <Name> { <NAME>_UNSPECIFIED = 0; }
reserved 2, 15 to 20;
reserved "<old_name>";
message <Outer> { message <Inner> {} }
import public "<other>.proto";
option allow_alias = true;
option go_package = "<path>";
<type> <field> = <n> [deprecated =
true];
Scalar types
| Type | Notes |
|---|---|
int32 / int64 |
varint; poor for negatives |
sint32 / sint64 |
zigzag; negatives are cheap |
uint32 / uint64 |
unsigned varint |
fixed32 / fixed64 |
fixed width, large values |
float / double |
IEEE 754 |
bool |
true or false |
string |
UTF-8 or ASCII text |
bytes |
arbitrary byte data |
Services
service <Name> { rpc <Method>(<Req>)
returns (<Resp>); }
rpc <Method>(stream <Req>) returns
(<Resp>);
rpc <Method>(<Req>) returns (stream
<Resp>);
rpc <Method>(stream <Req>) returns (stream
<Resp>);
Compatibility
| Change | Safe? |
|---|---|
| Add a new field | yes |
| Remove a field and reserve it | yes |
| Rename a field | wire yes, JSON no |
| Reuse a field number | never |
| Change a field number | never |
| int32 / int64 / uint / bool | interchangeable, may truncate |
| string and bytes | if valid UTF-8 |
| Singular to repeated | strings, bytes, messages only |
| Move a field into an existing oneof | no |
Recipes
mkdir -p genprotoc -I proto --python_out=gen --pyi_out=gen proto/*.proto
protoc -I proto --include_imports \ --descriptor_set_out=api.pb proto/api.protogrpcurl -protoset api.pb list
# no schemaprotoc --decode_raw < payload.bin# with a schemaprotoc -I proto --decode=pkg.Msg proto/api.proto < payload.bin
echo 'name: "Ada"' \ | protoc -I proto --encode=pkg.Msg proto/api.proto > m.binprotoc -I proto --decode=pkg.Msg proto/api.proto < m.bin



