uncategorized

protobuf

a message type

  • field types

    1
    2
    string query
    int32 page_numbe
  • field numbers

    ​ each field in the message definition has a unique number. These field numbers are used to identify your fields in the message binary format, and should not be changed once your message type is in use.

  • field rules

    • singular: a well-formed message can have zero or one of this field(default)

    • repeated: this field can be repeated any number of times (including zero) in a well-formed message.

1
2
3
4
5
6
7
8
/* SearchRequest represents a search query, with pagination options to
* indicate which results to include in the response. */

message SearchRequest {
string query = 1;
int32 page_number = 2; // Which page number do we want?
int32 result_per_page = 3; // Number of results to return per page.
}

Reserved Fields

1
2
3
4
message Foo {
reserved 2, 15, 9 to 11;
reserved "foo", "bar";
}

Importing Definitions

1
import "myproject/other_protos.proto";

Move a .proto file to a new location

1
2
3
4
5
6
7
8
9
10
11
// new.proto
// All definitions are moved here

// old.proto
// This is the proto that all clients are importing.
import public "new.proto";
import "other.proto";

// client.proto
import "old.proto";
// You use definitions from old.proto and new.proto, but not other.proto

Packages

You can add an optional package specifier to a .proto file to prevent name clashes between protocol message types.

1
2
package foo.bar;
message Open { ... }

You can then use the package specifier when defining fields of your message type:

1
2
3
4
5
message Foo {
...
foo.bar.Open open = 1;
...
}

Reference

Share