找不到Protobuf解组错误的解决方案

时间:2020-05-27 00:08:07

标签: go protocol-buffers unmarshalling proto

在解组二进制protobuf消息时,出现以下错误“解组错误:proto:无法解析保留的导线类型”。

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

这里是我从Protobuf Binary文件读取的数据,其内容类似于此处给出的Binary格式-> What does the protobuf text format look like?

读取文件并存储在数据中之后,数据看起来像这样[23 67 102 56 ...]

如何解决此错误?

1 个答案:

答案 0 :(得分:3)

[23 67 102 56 ... ]看起来像是一个整数切片,我假设这些整数是字节值。如果是这样,那么这不是protobuf流!

看着protobuf docs,第一个字节始终是varint键。它由字段号(前5位)和接线类型(后3位)组成。您的23的整数看起来像这样:

  0001  0111
  ^      ^
  varint wiretype

有效的电线类型在文档中显示。您的二进制值111是十进制7,这不是列出的类型之一。因此: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