dryeyhe0266 2019-07-17 21:32
浏览 380
已采纳

如何使用github.com/jhump/protoreflect解析知道消息描述符的原始消息的字节数组

I have a file containing a slice of bytes of the following protomessage.

syntax = "proto3";

package main;

message Address {
    string street = 1;
    string country = 2;
    string state = 3;
}

I have the message type described as follow:

func GetProtoDescriptor() (*descriptor.DescriptorProto, error) {

    return &descriptor.DescriptorProto{
        Name: proto.String("Address"),
        Field: []*descriptor.FieldDescriptorProto{
            &descriptor.FieldDescriptorProto{
                Name:     proto.String("street"),
                JsonName: proto.String("street"),
                Number:   proto.Int(1),
                Label:    descriptor.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
                Type:     descriptor.FieldDescriptorProto_TYPE_STRING.Enum(),
            },

            &descriptor.FieldDescriptorProto{
                Name:     proto.String("state"),
                JsonName: proto.String("state"),
                Number:   proto.Int(2),
                Label:    descriptor.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
                Type:     descriptor.FieldDescriptorProto_TYPE_STRING.Enum(),
            },

            &descriptor.FieldDescriptorProto{
                Name:     proto.String("country"),
                JsonName: proto.String("country"),
                Number:   proto.Int(2),
                Label:    descriptor.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
                Type:     descriptor.FieldDescriptorProto_TYPE_STRING.Enum(),
            },
        },
    }, nil

}

I would like to know how best I can use jhump/protoreflect to parse the content of the file using the message descriptor above.

Thank you for your help.

  • 写回答

1条回答 默认 最新

  • douhu4692 2019-07-18 14:08
    关注

    The typical way would be to compile the protocol to Go code using protoc:

    protoc main.proto --go_out=.
    

    This will generate main.pb.go, which will have a type called Address:

    var addr Address
    err := proto.Unmarshal(bytes, &addr)
    

    If this for some reason is not do-able (e.g. you must do it dynamically, with a descriptor), there are options. (But it's a lot more complicated.)

    1. Use a protoc-generated descriptor. You can have protoc export to a descriptor file (via the -o flag). If this is for RPC, have the server use server reflection; you can then use the github.com/jhump/protoreflect/grpcreflect package to download the server's descriptor (presumably generated by protoc).
    2. If you must create the descriptor programmatically, instead of using protoc, I recommend using the github.com/jhump/protoreflect/desc/builder package to construct it (instead of trying to create raw protos by hand). For example, your raw protos are not sufficient since they have no parent FileDescriptorProto, which is the root of any descriptor hierarchy. That builder package can take care of details like that for you (e.g. it will synthesize a parent file descriptor if necessary).

    In any event, the descriptor you want is a desc.Descriptor (from the github.com/jhump/protoreflect/desc package). The tips above (using other protoreflect sub-packages) will return desc.Descriptor instances. If you only have the raw descriptor protos (like in your example code), you'd want to first turn your *descriptor.FileDescriptorProto into a *desc.FileDescriptor using the desc#CreateFileDescriptor function.

    If you are using protoc and its -o option to create a descriptor set file (don't forget --include_imports flag, too), you can load it and turn it into a *desc.FileDescriptor using the desc#CreateFileDescriptorFromSet function. Here's an example:

    bytes, err := ioutil.ReadFile("protoset-from-protoc")
    if err != nil {
      panic(err)
    }
    var fileSet descriptor.FileDescriptorSet
    if err := proto.Unmarshal(bytes, &fileSet); err != nil {
      panic(err)
    }
    fd, err := desc.CreateFileDescriptorFromSet(&fileSet)
    if err != nil {
      panic(err)
    }
    // Now you have a *desc.FileDescriptor in `fd`
    

    Once you have that right kind of descriptor, you can create a *dynamic.Message. You can then unmarshal into that using either the proto#Unmarshal function or using the dynamic message's Unmarshal method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题