如何将注释导入到proto中?

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

How do I import annotations into proto?

问题

我正在尝试将注释导入到我的proto文件中。我将"google"文件夹复制到"project/proto/google"目录下。

import "google/api/annotations.proto";

路径google/api/annotations.proto是项目所在的位置。我尝试使用go mode init和vendor,但没有帮助。

在proto文件中,只有google/protobuf提供自动完成,我在GOPATH中没有看到这样的文件夹。

我该怎么做才能使其工作?

编辑1-

我使用以下命令:

protoc -I $GOPATH/src/github.com/jeka2708/test-grpc/proto/ --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/login.proto
英文:

I am trying to import annotations into my proto file. I copied the "google" folder to "project / proto/google".

import "google/api/annotations.proto";

the path google / api / annotations.proto - the project is located.
I am tried use the go mode init and vendor, it didn`t help

In the proto file, only google/protobuf offers autocomplete and that's it, I don't see such a folder in GOPATH

What do I do to make it work?

Edit 1-

enter image description here

I use

protoc -I $GOPATH/src/github.com/jeka2708/test-grpc/proto/ --go_out=. --go_opt=paths=source_relative  --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/login.proto

答案1

得分: 1

google/api/annotations.proto不是protoc编译器分发的基本标准包含文件的一部分(请参见最新版本此处)。

要将其纳入protoc构建中,只需下载所需版本(最新版本),还需要其包含的依赖项http.proto),并将其放置在后缀与导入路径匹配的目录中,例如:

/SOME/DIRECTORY/PATH/google/api/annotations.proto
/SOME/DIRECTORY/PATH/google/api/http.proto

前缀路径可以是系统默认的包含路径(例如/usr/include),例如:

/usr/include/google/api/annotations.proto
/usr/include/google/api/http.proto

或者您可以将其放置在某个项目目录中:

/home/user/my_proj/google/api/annotations.proto
/home/user/my_proj/google/api/http.proto

protoc编译器将默认查找当前工作目录,或者您可以通过命令行选项指定自定义包含路径:

Usage: ./protoc [OPTION] PROTO_FILES

解析PROTO_FILES并根据给定的选项生成输出:
  -IPATH, --proto_path=PATH   指定要搜索导入的目录。可以多次指定;将按顺序搜索目录。如果未指定,则使用当前工作目录。如果在这些目录中找不到,则将检查--descriptor_set_in描述符是否需要proto文件。
英文:

google/api/annotations.proto is not part of the base standard of include files that comes with the protoc compiler distribution (see latest here).

To incorporate it in your protoc build - simply download the version you need (latest, you'll also need it's included dependency http.proto) - and place it in a directory who's suffix matches the import path i.e.

/SOME/DIRECTORY/PATH/google/api/annotations.proto
/SOME/DIRECTORY/PATH/google/api/http.proto

The prefix path could be the system default include path (/usr/include) e.g.

/usr/include/google/api/annotations.proto
/usr/include/google/api/http.proto

or you can place in some project directory:

/home/user/my_proj/google/api/annotations.proto
/home/user/my_proj/google/api/http.proto

and the protoc compiler will default to looking in the current working directory - or you can specify custom include paths via the command-line options:

Usage: ./protoc [OPTION] PROTO_FILES

Parse PROTO_FILES and generate output based on the options given:
  -IPATH, --proto_path=PATH   Specify the directory in which to search for
                              imports.  May be specified multiple times;
                              directories will be searched in order.  If not
                              given, the current working directory is used.
                              If not found in any of the these directories,
                              the --descriptor_set_in descriptors will be
                              checked for required proto file.

huangapple
  • 本文由 发表于 2021年7月21日 00:35:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/68458430.html
匿名

发表评论

匿名网友

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

确定