3

我对 protoc 的使用感到非常困惑,并且在互联网上找不到有关其使用的示例:-

protoc -IPATH=path_to_proto_file_directory  path_to_proto_file --decode=MESSAGE_TYPE  < ./request.protobuf 

那么这里的 message_type 是什么,有人可以写一个完整的正确示例吗

4

2 回答 2

2
syntax = "proto3";
package response;

// protoc --gofast_out=. response.proto

message Response {
  int64 UID        
  ....
}

use protoc:
protoc --decode=response.Response response.proto < response.bin
protoc --decode=[package].[Message type] proto.file < protobuf.response

or use: 
protoc --decode_raw < protobuf.response
without proto file.
于 2019-09-06T08:57:14.337 回答
0

几年后,这是另一个答案。

# Check the version
protoc --version
>libprotoc 3.0.0

原始解码

您可以使用--decode_raw没有架构(.proto 文件)的选项。考虑这个包含字节的简单示例消息0x08 0x01。我假设一个带有 echo 的 Linux 环境

# Use echo to print raw bytes and don't print an extra `\n` newline char at the end
echo -en '\x08\x01' | protoc --decode_raw

# Output below. This means field 1 integer type with a value of 1
1: 1

使用 Schema 解码(.proto 文件)

如果你有 proto 文件,那么你可以获得比--decode_raw. --decode如果您想使用新值对其进行编码,您甚至可以将输出从回发送到 protoc。

Example.proto 文件内容
syntax = "proto3";

message Test {
  int32 FieldOneNumber
}
# Decode the same message as the raw example against this schema
echo -en '\x08\x1' | protoc --decode="Test" --proto_path= ./Example.proto

# Output. Note that the generic field named 1 has been replaced by the proto file name of FieldOne.
FieldOne: 1
于 2021-05-22T20:30:02.507 回答