My goal is to get FileDescriptor from .proto file dynamically, how should I do it?
input:
syntax = "proto3";
package "halo";
message SearchRequest {
string query = 1;
string data = 2;
}
output:
import "google.golang.org/protobuf/types/descriptorpb"
descriptorpb.FileDescriptorProto{
Package: "halo",
MessageType: []{
descriptorpb.DescriptorProto{query},
descriptorpb.DescriptorProto{data},
}
}
Unless a platform-specific runtime/library parser exists for your platform, the simplest way to do this is to use protoc
with the -oFILE
/ --descriptor_set_out=FILE
option, which parses the schema and outputs a protobuf payload that is the serialized FileDescriptorSet
contents. You would deserialize this in your specific platform, and take the first (usually only) file. For this deserialize step, you would typically use protoc
with the descriptor.proto schema as input, to get a platform/language-specific parser for that schema.
Platform-specific runtime/library parsers aren't common, and tend to be more common in 3rd-party tooling; the Google golang protobuf implementation is 1st-party, and no such parser exists for golang AFAIK.