我想要通过命令行从proto文件生成protobufs,而不使用”option go_package”。

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

I want to generate protobufs from proto files from cmd without using "option go_package "

问题

我需要从一个导入另一个proto文件的proto文件中生成protobuf文件。
另外一个要求是,这必须通过命令行生成(而不是在proto文件中定义"option go_package")。

英文:

I have to generate protobuf file from a proto file which import another proto file.
One another requirement, this has to be generated by cmd (by not defining "option go_package" in the proto file).

答案1

得分: 1

你可以使用M标志来指定Go包。以下是执行protoc命令的示例:

protoc -I=protos \
       --go_out=. \
       --go_opt=Mperson.proto=internal/pb \
       --go_opt=Maddressbook.proto=internal/pb \
       person.proto addressbook.proto

上述命令的目录结构如下:

└── protos
    ├── addressbook.proto
    └── person.proto

protos/person.proto:

syntax = "proto3";

package example;

message Person {
  string name = 1;
  int32 id = 2;
  string email = 3;
}

protos/addressbook.proto:

syntax = "proto3";

package example;

import "person.proto";

message AddressBook {
  repeated Person people = 1;
}

最终的目录结构如下:

├── internal
│    └── pb
│        ├── addressbook.pb.go
│        └── person.pb.go
└── protos
    ├── addressbook.proto
    └── person.proto

对于复杂的用例,建议使用Buf这个答案展示了如何使用Buf从复杂的proto文件生成代码。

英文:

You can use the M flag to specify the go package. This is an example for how to execute the protoc command:

protoc -I=protos \
       --go_out=. \
       --go_opt=Mperson.proto=internal/pb \
       --go_opt=Maddressbook.proto=internal/pb \
       person.proto addressbook.proto

The directory structure for the above command is:

└── protos
    ├── addressbook.proto
    └── person.proto

protos/person.proto:

syntax = "proto3";

package example;

message Person {
  string name = 1;
  int32 id = 2;
  string email = 3;
}

protos/addressbook.proto:

syntax = "proto3";

package example;

import "person.proto";

message AddressBook {
  repeated Person people = 1;
}

And the final directory structure is:

├── internal
│   └── pb
│       ├── addressbook.pb.go
│       └── person.pb.go
└── protos
    ├── addressbook.proto
    └── person.proto

For complex use cases, it's recommended to use Buf. This answer shows how it can be used to generate code from complex proto files.

答案2

得分: 1

假设你有3个文件foo.protopoo.protozoo.proto。文件foo.protopoo.protozoo.proto导入了一个消息结构。你需要按照特定的顺序编写命令,将foo.proto(从其他文件导入)放在最前面,然后是poo.proto(提供给其他文件导入)zoo.proto(提供给其他文件导入)

以下是生成protobuf文件的命令,不需要定义option go_package

protoc --proto_path=./ --go_out=./pb --go-grpc_out=./pb
--go_opt=Mfoo.proto=./ --go-grpc_opt=Mfoo.proto=./
--go_opt=Mpoo.proto=./ --go-grpc_opt=Mpoo.proto=./
--go_opt=Mzoo.proto=./ --go-grpc_opt=Mzoo.proto=./
foo.proto goo.proto zoo.proto

注意文件名前面的M,它替代了你代码中的option go_package

foo.proto

syntax = "proto3";
import "goo.proto";
import "zoo.proto";

service Foo {
    rpc FooRPC(PooMessage) returns (ZooMessage) {}
}

poo.proto

syntax = "proto3";

message PooMessage {
    string body = 1;
}

zoo.proto

syntax = "proto3";

message ZooMessage {
    string body = 1;
}
英文:

Let's say you have 3 files foo.proto, poo.proto and zoo.proto. The file foo.proto imports a message struct from poo.proto and zoo.proto. You need to write the command in a specific order which puts foo.proto(importing from other) first and poo.proto(giving import to other) and zoo.proto(giving import to other) after that.

Here is the command to generate protobufs without defining option go_package

protoc --proto_path=./ --go_out=./pb --go-grpc_out=./pb
--go_opt=Mfoo.proto=./ --go-grpc_opt=Mfoo.proto=./
--go_opt=Mpoo.proto=./ --go-grpc_opt=Mpoo.proto=./
--go_opt=Mzoo.proto=./ --go-grpc_opt=Mzoo.proto=./
foo.proto goo.proto zoo.proto

Notice the M before filename, it replaces option go_package in your code

foo.proto

syntax = "proto3";
import "goo.proto";
import "zoo.proto";

service Foo {
    rpc FooRPC(PooMessage) returns (ZooMessage) {}
}

poo.proto

syntax = "proto3";

message PooMessage {
    string body = 1;
}

zoo.proto

syntax = "proto3";

message ZooMessage {
    string body = 1;
}

huangapple
  • 本文由 发表于 2023年5月3日 16:14:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76161742.html
匿名

发表评论

匿名网友

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

确定