好的,对于主要问题:
b, err := ioutil.ReadFile("/path/to/descriptor.pb")
if err != nil {
log.Fatal(err)
}
fds := &descriptorpb.FileDescriptorSet{}
proto.Unmarshal(b, fds)
注意aFileDescriptorSet
不是 aFileDescriptorProto
我继续对脱节感到困惑Files
,Types
但我怀疑开发人员的意图是能够将例如从文件中获得的消息移动到类型中。
我解决了我的直接问题:
package main
import (
"fmt"
"io/ioutil"
"log"
pb ".../protos"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/dynamicpb"
"google.golang.org/protobuf/types/known/anypb"
)
var _ protodesc.Resolver = (*protoregistry.Files)(nil)
const (
projectRoot = "."
descriptorFilename = "descriptor.pb" // The output from `protoc --descriptor_set_out ...`
protoFilename = "adder.proto" // The protoFilename will be one (of possibly several) in desc
packageName = "e"
serviceName = "Adder"
)
var (
// By convention, the request message to be serviceName+"Request"
message = service + "Request"
)
func toAny(t string, m protoreflect.ProtoMessage) (*anypb.Any, error) {
v, err := proto.Marshal(m)
if err != nil {
return nil, err
}
return &anypb.Any{
TypeUrl: t,
Value: v,
}, nil
}
func readDescriptorSetFile(name string) (*descriptorpb.FileDescriptorSet, error) {
b, err := ioutil.ReadFile(name)
if err != nil {
return nil, err
}
fds := &descriptorpb.FileDescriptorSet{}
proto.Unmarshal(b, fds)
return fds, nil
}
func byPath(ff *protoregistry.Files, path string, messageName string) (protoreflect.MessageDescriptor, error) {
fd, err := ff.FindFileByPath(path)
if err != nil {
return nil, err
}
mds := fd.Messages()
return mds.ByName(protoreflect.Name(messageName)), nil
}
func byName(ff *protoregistry.Files, packageName string, messageName string) (protoreflect.MessageDescriptor, error) {
d, err := ff.FindDescriptorByName(protoreflect.FullName(fmt.Sprintf("%s.%s", packageName, messageName)))
if err != nil {
log.Fatal(err)
}
md, ok := d.(protoreflect.MessageDescriptor)
if !ok {
return nil, fmt.Errorf("type assertion to MessageDescriptor failed: %s", d.FullName())
}
return md, nil
}
func unmarshal(md protoreflect.MessageDescriptor, a *anypb.Any) (*dynamicpb.Message, error) {
m := dynamicpb.NewMessage(md)
err := proto.Unmarshal(a.GetValue(), m)
if err != nil {
return nil, err
}
return m, nil
}
func main() {
// protoc \
// --proto_path=./protos \
// --descriptor_set_out=descriptor.pb \
// protos/adder.proto
fds, err := readDescriptorSetFile(fmt.Sprintf("%s/%s", projectRoot, descriptorFilename))
ff, err := protodesc.NewFiles(fds)
if err != nil {
log.Fatal(err)
}
a, err := toAny(
fmt.Sprintf("%s.%s", packageName, message),
&pb.AdderRequest{
A: 39,
B: 3,
})
if err != nil {
log.Fatal(err)
}
{
md, err := byPath(ff, protoFilename, message)
if err != nil {
log.Fatal(err)
}
m, err := unmarshal(md, a)
if err != nil {
log.Fatal(err)
}
log.Printf("%+v", m)
}
{
md, err := byName(ff, packageName, message)
if err != nil {
log.Fatal(err)
}
m, err := unmarshal(md, a)
if err != nil {
log.Fatal(err)
}
log.Printf("%+v", m)
}
}
注意这可能看起来很迂回,因为我有pb.AdderRequest
,但我正在寻找一个解决方案,只有根据定义才可用。
注意代码复制了方法:byPath
因为byName
我不确定我想要哪种方法。