从另一个项目导入.proto文件。

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

Import .proto files from another project

问题

我想将一个在不同项目中定义的.proto文件导入到我的golang项目中,并在其中使用定义的消息类型。

我想要导入的.proto文件是:https://github.com/lyft/clutch/blob/main/api/k8s/v1/k8s.proto

我已经添加了导入语句:

import "github.com/lyft/clutch/api/k8s/v1/k8s.proto";

为了使用该文件中的消息类型"Job",我添加了以下内容:

message Jobs {
repeated clutch.k8s.v1.Job job = 1;
}

当我尝试编译.proto文件时,我遇到了以下错误:

导入"github.com/lyft/clutch/blob/main/api/k8s/v1/k8s.proto"未找到或存在错误。

"clutch.k8s.v1.Job"未定义。
英文:

I want to import .proto file defined in different project into my golang project and use a message type defined in it.

Proto file I want to import is: https://github.com/lyft/clutch/blob/main/api/k8s/v1/k8s.proto

I have added the import statement as :

import "github.com/lyft/clutch/api/k8s/v1/k8s.proto";

to use message type "Job" in that file, I added

message Jobs {
repeated clutch.k8s.v1.Job job = 1;
}

When I try to compile proto file, I'm getting the error

Import "github.com/lyft/clutch/blob/main/api/k8s/v1/k8s.proto" was not found or had errors.

"clutch.k8s.v1.Job" is not defined.

答案1

得分: 2

为了编译您的proto文件,您需要克隆依赖的仓库,并将其设置为protoc导入的包含路径,例如:

<kbd>job.proto</kbd>

syntax = "proto3";

package clutch.k8s.v1;

// 定义
option go_package = "github.com/job";

import "api/k8s/v1/k8s.proto";


message Jobs {
  repeated clutch.k8s.v1.Job job = 1;
}


<kbd>compile.sh</kbd>

proto_out_dir=.
GOBIN=~/go/bin

protoc \
      --go_out "${proto_out_dir}" \
      --go_opt paths=source_relative \
      --go-grpc_out "${proto_out_dir}" \
      --go-grpc_opt require_unimplemented_servers=false,paths=source_relative \
      --plugin protoc-gen-go="${GOBIN}/protoc-gen-go" \
      --plugin protoc-gen-go-grpc="${GOBIN}/protoc-gen-go-grpc" \
--go-grpc_opt=paths=source_relative \
--proto_path=../protoc-gen-validate \
--proto_path=../api-common-protos \
--proto_path=../clutch \
--proto_path=../clutch/api \
--proto_path=. \
job.proto

将生成:

<kbd>job.pb.go</kbd>

...
type Jobs struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	Job []*v1.Job `protobuf:"bytes,1,rep,name=job,proto3" json:"job,omitempty"`
}
...

还可以在源代码仓库中查看如何构建proto文件这里

英文:

In order to compile your proto, you should clone the dependency repos and set as include path in the protoc import, as example:

<kbd>job.proto</kbd>

syntax = &quot;proto3&quot;;

package clutch.k8s.v1;

// degine
option go_package = &quot;github.com/job&quot;;

import &quot;api/k8s/v1/k8s.proto&quot;;


message Jobs {
  repeated clutch.k8s.v1.Job job = 1;
}


<kbd>compile.sh</kbd>

proto_out_dir=.
GOBIN=~/go/bin

protoc \
      --go_out &quot;${proto_out_dir}&quot; \
      --go_opt paths=source_relative \
      --go-grpc_out &quot;${proto_out_dir}&quot; \
      --go-grpc_opt require_unimplemented_servers=false,paths=source_relative \
      --plugin protoc-gen-go=&quot;${GOBIN}/protoc-gen-go&quot; \
      --plugin protoc-gen-go-grpc=&quot;${GOBIN}/protoc-gen-go-grpc&quot; \
--go-grpc_opt=paths=source_relative \
--proto_path=../protoc-gen-validate \
--proto_path=../api-common-protos \
--proto_path=../clutch \
--proto_path=../clutch/api \
--proto_path=. \
job.proto

Will produce:

<kbd>job.pb.go</kbd>

...
type Jobs struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	Job []*v1.Job `protobuf:&quot;bytes,1,rep,name=job,proto3&quot; json:&quot;job,omitempty&quot;`
}
...

See also how the proto are build in the source repo here

huangapple
  • 本文由 发表于 2021年9月23日 18:06:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/69297989.html
匿名

发表评论

匿名网友

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

确定