In Go, using google.golang.org/protobuf@v1.25.0, I can turn a proto.Message 
m into a serialized description of its type like this:

ref := m.ProtoReflect()
desc := ref.Descriptor()
descProto := protodesc.ToDescriptorProto(desc)
proto.Marshal(descProto)

I would like to take a serialized type t like this and turn it back into a 
(blank) proto.Message of the appropriate type. (Error checks etc. omitted 
for clarity.)

var descProto descriptorpb.DescriptorProto
err := proto.Unmarshal(t, &descProto)
desc := ThisOperationDoesNotExist(&descProto) // The problem.
m := dynamicpb.NewMessage(desc)

I've been able to make ThisOperationDoesNotExist work by faking up a 
FileDescriptorSet, like this:

descProtos := []*descriptorpb.DescriptorProto{&descProto}
name := "x"
fdProtos := []*descriptorpb.FileDescriptorProto{{Name: &name, MessageType: 
descProtos}}
fdSet := &descriptorpb.FileDescriptorSet{File: fdProtos}
f, err := protodesc.NewFiles(fdSet)
&descriptorpb.FileDescriptorSet{File: []*descriptorpb.FileDescriptorProto{{
Name: &name, MessageType: []*descriptorpb.DescriptorProto{&dp}}}})

var desc protoreflect.MessageDescriptor
f.RangeFiles(func(fileDesc protoreflect.FileDescriptor) bool {
  msgDescs := fileDesc.Messages()
  desc = msgDescs.Get(0)
})

but that's pretty ugly, and probably brittle. Is there another way? And if 
there isn't, can one be added?

Thanks,
- Bob

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/protobuf/16f57ae2-d675-444c-ac03-e06eebcb7fdco%40googlegroups.com.

Reply via email to