how to write proto file with bytes embedded message

868 views Asked by At

I have an encoded binary as follows:

0a 16 0a 06 72 63 6e 33 31 72 12 0a 65 37 36 30 34 32 33 35 32 37 1a 00
20 01 2a 06 34 34 38 37 38

I am not sure how to write the proto file reflecting the binary. I know the message contents.

1. First two bytes indicate an embedded message with 22 bytes in length.
2. Inside the embedded message there are three fields (three strings).
3. Then the rest of the message with two fields.

How to write the .proto file to reflect the above binary ?

message UserInfo
{
        required string username = 1;
        required string usserId  = 2;
        optional string extraInfo= 3;
}

message SendUserRequest
{
        required UserInfo uinfo = 1;
        ...
}
2

There are 2 answers

2
Kenton Varda On

Sorry, but this isn't what Protobuf is for. Protobuf can only decode Protobuf format; it cannot match arbitrary ad-hoc byte formats.

EDIT: I misunderstood the question. Sometimes people think Protobufs can describe arbitrary binary, and the exact bytes given were rejected by protoc --decode_raw, but it turns out there was a missing byte. I added a better answer above.

0
Kenton Varda On

The bytes you provided appear to be missing a byte. I added a 0 to the end, then ran it through protoc --decode_raw and got this:

1 {
  1: "rcn31r"
  2: "e760423527"
  3: ""
}
4: 1
5: "44878\000"  // (my added zero byte shows up here)

So in addition to what you wrote, your SendUserRequest should include two more fields:

optional int32 a = 4;
optional string b = 5;

Note that there's no way to tell what the correct names of these fields should be, nor whether they're required or optional. Moreover, a could technically be any of the integer types.