引用外部协议,如Google/rpc/status.proto [英] Referencing external protos like google/rpc/status.proto

查看:21
本文介绍了引用外部协议,如Google/rpc/status.proto的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何正确引用外部原型文件。假设我有一个.proto文件,它引用了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;
}

当然,我们必须通过-I/--proto_path告诉protoc如何找到该文件所在的位置。

我的问题是:实际引用此文件的最佳实践是什么,特别是让版本控制满意的最佳实践是什么?Protobufs似乎没有go mod等价物。我看到过它被逐字复制到项目中(例如在grpc-gateway中),或者只是从本地文件系统引用。

推荐答案

我认为您在这里差不多回答了自己的问题。我已经成功地完成了这两项工作:逐字手动复制必要的文件(从https://github.com/googleapis/googleapis/tree/master/googlehttps://github.com/protocolbuffers/protobuf/tree/master/src/google/protobuf),并引用文件的本地副本。

如果您希望这样做并使版本控制满意,您可以将这两个存储库作为git submodules添加到您的存储库中。只需确保使用-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>

至于引用文件的本地副本,并在尝试构建之前确保它们存在,您可能会发现向Makefile添加类似以下内容会有所帮助(这是在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>

这篇关于引用外部协议,如Google/rpc/status.proto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆