Skip to content

Instantly share code, notes, and snippets.

Last active March 21, 2023 12:40
  • Star 19 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Encoding a protobuf with `protoc --encode`

I needed to quickly encode a protobuf from the command-line, and while I pretty much immediately came across protoc --encode as the obvious solution, I did not find much documentation on the input textual syntax.

Here is the relevant snippet from protoc --help:

--encode=MESSAGE_TYPE       Read a text-format message of the given type
                              from standard input and write it in binary
                              to standard output.  The message type must
                              be defined in PROTO_FILES or their imports.

Here are a few examples based on some quick experimentation (as opposed to looking at whatever parsing/grammar is used by protoc itself... which would probably have taken a bit more than the few minutes for the experiments below).

Assume the following proto def (coming from prometheus):

message Query {
  required int64 start_timestamp_ms = 1;
  required int64 end_timestamp_ms = 2;
  repeated prometheus.LabelMatcher matchers = 3;

message LabelMatcher {
  enum Type {
    EQ  = 0;
    NEQ = 1;
    RE  = 2;
    NRE = 3;
  Type type    = 1;
  string name  = 2;
  string value = 3;

Then any of the following text snippets are valid input to protoc --encode


type: EQ
name: "foo"
value: "bar"
type: EQ, name: "foo", value: "bar"
type: EQ;name: "foo";value: "bar"

(Note the absence of outer curly braces!)


start_timestamp_ms: 0
end_timestamp_ms: 100
matchers: [{type: EQ; name: "bla"; value: 'val'}]
start_timestamp_ms: 0
end_timestamp_ms: 100
matchers: [
  type: EQ
  name: "bla"
  value: 'val'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment