bufbuild/protocompile

Compile protobuf from string or bytes

paulolimarb opened this issue · 2 comments

Hi @jhump

I get "my_content.proto" in response from my API and I would like to compile protobuf in memory from string or bytes content.

my_content_from_api := `message Person {
  optional string name = 1;
  optional int32 id = 2;
  optional string email = 3;
}`

compiler := protocompile.Compiler{
		Resolver:       &protocompile.SourceResolver{},
		SourceInfoMode: protocompile.SourceInfoStandard,
	}

compiler.Compile(context.Background(), my_content_from_api)

In your library this is possible?

And thank you for your excellent work.

jhump commented

Hi, @paulolimarb, yes, this is possible.

For your Resolver, use a protocompile.SourceResolver with a custom Accessor function. For the function, you can use protocompile.SourceAccessorFromMap:

compiler := protocompile.Compiler{
	Resolver: &protocompile.SourceResolver{
		Accessor: protocompile.SourceAccessorFromMap(map[string]string{
			"my_content.proto": my_content_from_api,
		}),
	},
	SourceInfoMode: protocompile.SourceInfoStandard,
}

Hi @jhump

Thanks for the quick response.

Regards