Cannot Find Solutions to a Protobuf Unmarshal Error

Viewed 11312

I'm getting the following error "unmarshaling error: proto: cannot parse reserved wire type" while unmarshaling a binary protobuf message.

newMessage := &MessageName{}
err = proto.Unmarshal(data, newMessage)

Here for data I'm reading from Protobuf Binary file whose contents look something like the Binary format given here -> What does the protobuf text format look like?

After reading the file and storing in data, data looks something like this [23 67 102 56 ... ]

How to fix this error?

1 Answers

[23 67 102 56 ... ] looks like a slice of ints, I'll assume the ints are the byte values. If that's true, this is not a protobuf stream!

Looking at the protobuf docs, the first byte is always a varint key. This is made up of a field number (first 5 bits) and a wire type (last 3 bits). Your int of 23 looks like this:

  0001  0111
  ^      ^
  varint wiretype

The valid wire types are shown in the docs. Your binary value 111 is decimal 7, which is not one of the listed types. Hence: cannot parse reserved wire type

Type    Meaning             Used For
0       Varint              int32, int64, uint32, uint64, sint32, sint64, bool, enum
1       64-bit              fixed64, sfixed64, double
2       Length-delimited    string, bytes, embedded messages, packed repeated fields
3       Start group         groups (deprecated)
4       End group           groups (deprecated)
5       32-bit              fixed32, sfixed32, float
Related