Skip to content

Instantly share code, notes, and snippets.

@henridf
Last active May 29, 2024 22:08
Show Gist options
  • Save henridf/704c1c812f04a502c1c26f77a739090b to your computer and use it in GitHub Desktop.
Save henridf/704c1c812f04a502c1c26f77a739090b to your computer and use it in GitHub Desktop.
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

LabelMatcher:

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!)

Query:

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'
 }
]

@3Nigma
Copy link

3Nigma commented Sep 3, 2023

Thank you!

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