protoc –go-grpc_out 将生成的代码放在 –go_out 生成的代码之外的视野中。

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

protoc --go-grpc_out generate code in out of vision of --go_out generated code

问题

在之前的产品中,我使用的是旧的protoc-gen-go工具,它允许使用插件并在同一个pb文件中生成序列化/反序列化和gRPC客户端/服务器代码。

据我了解,protoc-gen-go v1.27.1将不再支持插件,并要求使用go-grpc_out标志来生成客户端/服务器代码。

按照以下命令进行操作:

protoc -I /usr/local/include -I $PWD/api/dummy-proto --go_out=generated --go-grpc_out=generated --go_opt=paths=source_relative proto/v1/foo.proto

我得到了以下结果:

generated
|_proto
  |_v1
  |_dummy
  | |_foo_grpc.pb.go // 包名为dummy
  |_foo.pb.go // 包名为dummy

由于创建了"dummy"文件夹,foo_grpc.pb.go中的函数无法看到在foo.pb.go中生成的Request和Response。

我做错了什么?是否有选项可以像以前一样生成一个文件?将foo_grpc.pb.go移动到与foo.pb.go相同的级别后,它将正常工作。

另外,是否可以使用旧的标志,例如--go_out=import_path=",并在proto文件中声明不带斜杠和go_options的包,例如-go_out=import_path=grpc_v1_proto,M$PWD/proto/v1/foo.proto=grpc_v1_proto"

foo.proto的内容如下:

syntax = "proto3";

package dummy.v1.foo;
option go_package = "proto/v1/dummy";

import "proto/v1/structures.proto";

service FooService {
  rpc reverse(ReverseRequest) returns (ReverseResponse);
  rpc getBar(GetBarRequest) returns (GetBarResponse);
}

message ReverseRequest {
  string text = 1;
}

message ReverseResponse {
  string reversed_text = 1;
}

message GetBarRequest {
}

message GetBarResponse {
  structures.Bar bar = 1;
}
英文:

In previos products i use old protoc-gen-go which allow to use plugins and generate serialization/deserialization and gRPC client/server in same pb file

as far i understand protoc-gen-go v1.27.1 will not allow plugins and demand to use go-grpc_out flag for client\server code

Follow this command

protoc -I /usr/local/include -I $PWD/api/dummy-proto  --go_out=generated --go-grpc_out=generated --go_opt=paths=source_relative proto/v1/foo.proto

i got

generated
|_proto
  |_v1
  |_dummy
  | |_foo_grpc.pb.go //package dummy
  |_foo.pb.go //package dummy

Because of created "dummy" folder foo_grpc.pb.go functions do not see Request and Response which generated in foo.pb.go

What i am doing wrong? Is there is option to generate one file as before? It will be work properly after move foo_grpc.pb.go on same level with foo.pb.go.

Also is there is possible to use old flag like --go_out=import_path=" and declare package with M without slashes and without go_options in proto like -go_out=import_path=grpc_v1_proto,M$PWD/proto/v1/foo.proto=grpc_v1_proto"

foo.proto

syntax = "proto3";

package dummy.v1.foo;
option go_package = "proto/v1/dummy";

import "proto/v1/structures.proto";

service FooService {
  rpc reverse(ReverseRequest) returns (ReverseResponse);
  rpc getBar(GetBarRequest) returns (GetBarResponse);
}

message ReverseRequest {
  string text = 1;
}

message ReverseResponse {
  string reversed_text = 1;
}

message GetBarRequest {
}

message GetBarResponse {
  structures.Bar bar = 1;
}

答案1

得分: 3

根据评论,您需要添加--go-grpc_opt=paths=source_relative。这在基础教程中有介绍(但只是给出了命令,没有详细说明)。

protoc-gen-go-grpc使用protoc-gen-go共享的代码来处理大多数这些选项,因此Go生成代码的文档可能会回答您的问题(只需将go_opt更改为go-grpc_opt)。

英文:

As per the comments you need to add --go-grpc_opt=paths=source_relative. This is covered in the basics tutorial (but that really just gives the command without much detail).

protoc-gen-go-grpc uses code shared with protoc-gen-go to process most of these options so the documentation for Go Generated Code will probably answer your questions (just change go_opt to go-grpc_opt).

huangapple
  • 本文由 发表于 2021年8月2日 00:42:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/68612482.html
匿名

发表评论

匿名网友

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

确定