Protocol buffers 引用外部proto,如google/rpc/status.proto

Protocol buffers 引用外部proto,如google/rpc/status.proto,protocol-buffers,Protocol Buffers,我想知道如何引用外部proto文件的属性。假设我有一个.proto文件,它引用了标准的protobuf类型,比如Timestamp: syntax = "proto3"; package api; import "google/protobuf/timestamp.proto"; message ServerTimeResponse { google.protobuf.Timestamp ts = 1; } 简单。时间戳在编译时自动可用 现在我添加一个外部 键入,比如说google.r

我想知道如何引用外部proto文件的属性。假设我有一个.proto文件,它引用了标准的protobuf类型,比如
Timestamp

syntax = "proto3";
package api;

import "google/protobuf/timestamp.proto";

message ServerTimeResponse {
  google.protobuf.Timestamp ts = 1;
}
简单。时间戳在编译时自动可用

现在我添加一个外部 键入,比如说
google.rpc.Status

syntax = "proto3";
package api;

import "google/protobuf/timestamp.proto";
import "google/rpc/status.proto";

message ServerTimeResponse {
  google.protobuf.Timestamp ts = 1;
  google.rpc.Status status = 2;
}
当然,我们必须告诉
protoc
如何通过
-I
/
--proto\u路径找到这个文件


我的问题是:实际引用此文件的最佳实践是什么,特别是为了使版本控制更方便?protobufs似乎没有一个
go mod
等价物。我看到它将逐字复制到项目中(如中),或者只是从本地文件系统引用。

我认为您在这里回答了自己的问题。我已经成功地完成了这两项工作:手动逐字复制必要的文件(从和),并引用文件的本地副本

如果您想做到这一点并使版本控制愉快,您可以在存储库中添加这两个存储库。只需确保使用
-I
将正确的位置传递到
protoc
。例如:

cd $PROJECT_DIR
mkdir third_party && cd third_party
git submodule add https://github.com/googleapis/googleapis/tree/master/google
cd $PROJECT_DIR
<git commit the change>
protoc -I third_party/google <the rest of your protoc command>
cd$PROJECT\u DIR
mkdir第三方和cd第三方
git子模块添加https://github.com/googleapis/googleapis/tree/master/google
cd$PROJECT_DIR
protoc-I第三方/谷歌
至于引用文件的本地副本,并在尝试构建之前确保它们存在,您可能会发现向Makefile添加类似于以下内容的内容会有所帮助(这是在Go build环境中):

go-get-u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get-u github.com/golang/protobuf/protoc-gen-go
grpc_gateway_path=$(go list-m-f'{{.Dir}}'github.com/grpc生态系统/grpc网关)
googleapis_path=“$grpc_网关_path/第三方/googleapis”
protoc-I$googleapis\u path--go\u out=。

我按照您的建议选择了子模块路线,结果很好,谢谢。很高兴听到您的建议!伟人所见略同:)我真的很喜欢你的
go-list-m
建议。在这种情况下,我从go模块和子模块得到了相同的东西,看起来非常干净。我要试一试。再次感谢。
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -u github.com/golang/protobuf/protoc-gen-go
grpc_gateway_path=$(go list -m -f '{{.Dir}}' github.com/grpc-ecosystem/grpc-gateway)
googleapis_path="$grpc_gateway_path/third_party/googleapis"
protoc -I $googleapis_path --go_out=. <list of input files>