bufbuild/protocompile

How to convert FileDescriptor to []byte?

ankisme opened this issue · 3 comments

I use protoc + gogoproto before

Now I want to use protocompile + gogoproto

And I don't know how to convert FileDescriptor to []byte, how to convert?

Here is my code

func TestProto(t *testing.T) {
	t.Parallel()
	comp := protocompile.Compiler{
		Resolver:       &protocompile.SourceResolver{},
		SourceInfoMode: protocompile.SourceInfoStandard,
	}
	ctx := context.Background()

	files, err := comp.Compile(ctx, "desc_test1.proto")
	if !assert.Nil(t, err, "%v", err) {
		t.FailNow()
	}

	for _, fd := range files {
		fdProto := protodesc.ToFileDescriptorProto(fd)
---------->How to convert fdProto to []byte? Because the plugin gogoproto need the []byte argument.
	}
}
jhump commented

@ankisme, once you've got the file descriptor proto, you marshal it to bytes like you would any other proto:

bytes, err := proto.Marshal(fdProto)
jhump commented

@ankisme, also, I would highly recommend using protoutil.ProtoFromFileDescriptor. The method in protodesc re-creates a proto. But the above utility can more efficiently just extract the underlying proto for descriptors produced by the compiler.

@ankisme, once you've got the file descriptor proto, you marshal it to bytes like you would any other proto:

bytes, err := proto.Marshal(fdProto)

@jhump Nice, it solve my code problem.

Now my progress of protocompile + gogoproto is almost done about 80%.

Thank you for answering.