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

如何使用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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题