Is there a way to import generated structs in grpc.pb.go during protoc compilation?

huangapple go评论102阅读模式
英文:

Is there a way to import generated structs in grpc.pb.go during protoc compilation?

问题

我将为生成的结构体从proto消息和生成的grpc服务接口指定不同的输出目录。

我使用以下两个标志来实现:

--go_out=internal/proto
--go-grpc_out=internal/grpc

生成后,internal/proto包在mymodel_grpc.pb.go文件中没有被导入,但如果在编译过程中有任何标志可以自动执行此操作,那将非常方便。

我猜测--go-grpc_opt标志可能有这样的选项,但我找不到任何相关信息。

对于这种情况有任何经验吗?

英文:

I am going to specify different out directories for generated structs from proto messages and generated interfaces from grpc service.

I achieve that with this two flags:

--go_out=internal/proto
--go-grpc_out=internal/grpc

After generating the internal/proto package is not imported in mymodel_grpc.pb.go file, but it would be very convenient if there is any flag to do that automatically during compilation.

I assume --go-grpc_opt flag could have such an option, but I couldn't find any.

Any experience with this kind of scenario is interesting.

答案1

得分: 1

如果我理解正确,您希望在一个包中生成protobuf消息,在另一个包中生成服务绑定。

Protobuf文件包括一个package指令,可以通过options指令进行绑定,例如对于Go语言可以使用go_package=。绑定是一对一的;一个包(及其消息|服务)绑定到一个Go包。

注意 Protobuf也支持多种语言,因此它必须提供通用解决方案,可以在Protobuf文件中应用一次,但在每种语言的生成代码中使用多次。

所以,我认为您的解决方案是将消息和服务分别放在不同的包中(具有不同的optionsgo_package规范)。然后,可以通过指导protoc将它们生成到不同的Golang包中。

英文:

If I understand correctly, you would like to generate the protobuf messages in one package and the service bindings in another package.

Protobuf files include a package directive and this can be bound to options directives e.g. for Go(lang) using go_package=. The binding is 1:1; one package (and its messages|services) are bound to one e.g. Go package.

> NOTE Protobuf supports multiple languages too and so it must provide generic solutions that can be applied once (in Protobuf file) but used many (in each language's generated code).

So, I think your solution is to separate your messages and services into different packages (with different options and go_package specifications. Then, protoc can be guided to generated these to separate Golang packages.

huangapple
  • 本文由 发表于 2022年6月9日 23:01:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/72562599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定