- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要修复一个 grpc 服务,所以我想了解编译它们的逻辑。
在下面的示例中,我不明白为什么 protoc 不编译 address.proto
,因为它是由 person.proto
导入的。
没有构建错误,所以我认为这不是导入/命名问题。不幸的是,只生成了一个person_pb2.py
...
// file: address.proto
syntax="proto3";
message Address {
string city = 1;
string road = 3;
int32 roadNumber = 4;
}
// file: person.proto
syntax="proto3";
import "address.proto";
message Person {
string name = 1;
Address home = 3;
Address work = 4;
}
构建命令:
python -m grpc_tools.protoc --proto_path ../protos --python_out=. person.proto
最佳答案
这是一个合理的问题。
答案是你只是要求 protoc
来编译 person.proto
(而且它是按字面意思)。但是,生成的代码不会运行,因为 person_pb2.py
依赖于 address_pb2
:
import address_pb2 as address__pb2
您需要为您的代码所需的所有 Protocol Buffer 类型提供 Python 源代码。
python3 \
-m grpc_tools.protoc \
--proto_path=../protos \
--python_out=. \
person.proto address.proto
这个问题(正在解决)的一个很好的例子是谷歌所谓的 Well-Known Types (WKTs)
您可以包括例如google.protobuf.Timestamp
在 Person
中添加 import "google/protobuf/timestamp.proto"
:
syntax="proto3";
import "google/protobuf/timestamp.proto";
import "address.proto";
message Person {
string name = 1;
Address home = 3;
Address work = 4;
google.protobuf.Timestamp timestamp = 5;
}
并且您可以protoc
无需更改并且您的代码将工作。
这是因为 Google 将 WKTs 和生成的 Python 代码与 grpcio-tool
捆绑在一起。与任何其他来源一样,您确实需要导入 Google 提供的 Python 来源才能使用代码。
为此,你必须
main.py
:
import address_pb2
import person_pb2
from google.protobuf.timestamp_pb2 import Timestamp
那么,这些文件在哪里?
以我为例:
lib/python3.8/site-packages/grpc_tools/_proto/google/protobuf/timestamp.proto
和:
lib/python3.8/site-packages/google/protobuf/timestamp_pb2.py
NOTE
It's convenient that Google bundles the generated sources but this is not required and it creates two potential problems.
timestamp.proto
is the source-of-truth and we must assume|trust thatgoogle/protobuf/timestamp_pb2.py
was generated from it.- If we're the proto developer, we're expected to produce sources for all languages that our proto's developers may use every time we update the protos.
For these reasons, generally (!) developers provide only the protos and don't include generated sources and assume that you're able to plug the proto into
protoc
and generated the perfect facsimile as code yourself.
关于protocol-buffers - 如何让 protoc 编译一个 proto 及其依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70295689/
我一直在尝试使用 protoc cli 实用程序对字符串进行编码。 注意到输出仍然包含纯文本。 我究竟做错了什么? osboxes@osboxes:~/proto/bin$ cat ./teststr
要为 Python 编译 proto 文件,我可以 protoc -I=.--python_out=$DST_DIR sommem.proto 基于 https://developers.google
如何查看protoc-gen-go和protoc-gen-validate的版本? protoc-gen-go --version protoc-gen-validate --version 我试过上
我们使用的是 protoc-gen-go v2(v2 是 google.golang.org/protobuf,v1 是 github.com/golang/protobuf) 当我们尝试编译 gRP
我正在尝试用 C++ 生成 Google Assistant 库。 我已经使用 protoc 编译器编译了 embedded_assistant.proto 文件,并获得了用于库的 embedded_
我想使用来自 protoc-gen-grpc-kotlin 的 protoc-gen-grpc-kotlin-1.0.0我得到了 Execution failed for task ':generat
我想使用来自 protoc-gen-grpc-kotlin 的 protoc-gen-grpc-kotlin-1.0.0我得到了 Execution failed for task ':generat
我只是想使用 Protocol Buffer 为 golang 生成代码。但是无法使用 protoc 命令生成服务。 syntax = "proto3"; package greet; option
我正在尝试构建一个自定义协议(protocol)插件来从 .proto 文件生成自定义输出。我从 protoc custom plugin 复制了包含 CodeGenerator 的 java 文件作
protoc-gen-doc 自定义模板规则详解 配套演示工程 此项目中所用 proto 文件位于 ./proto 目录下,来源于 官方proto示例 此项目中所列所有
我正在尝试运行: protoc *.proto -python_out=. 在 tensorflow > models > research > object_detection > protos 内
我想使用 protoc生成java文件的命令 我一直在遵循来自 github https://github.com/protocolbuffers/protobuf 的自述文件中的说明但它只是告诉您下
我想实现dart-protobuf,但是dart-protoc-plugin(从proto定义生成Dart protobuf库的项目)当前不支持Windows。我找不到该插件的替代品。没有其他方法可以
问题: 我在根目录中有 2 个文件。 我使用 Makefile 从 .proto 文件生成 Go 代码。 但是 Video 结构中的语言字段是一个值,而不是指向该值的指针。 Video 结构中的 su
我正在尝试在 How to write a custom Protobuf CodeGenerator in Java 制作工作示例。当我尝试使用编译文件时 import com.google.pro
当我运行这个时: protoc --python_out=. .\object_detection\protos\anchor_generator.proto 在命令行上的 tensorflow 对象
我正在尝试开发一个插件,以便自动生成特定于我的应用程序的代码。虽然一个可能更简单的策略是让我的代码使用由 c++ 插件生成的文件,但我正在尝试从头开始编写插件。 正如我添加到我的包中的文档中所解释的那
描述 我想用java调用golang grpc,我用老golang的consumer_proto.proto来生成java代码 过程 protoc --java_out=/home/xxx/src/
我正在开发一个应用程序,该应用程序必须针对使用 google protocol buffers 3.0.0 版的目标进行交叉编译。有没有办法在 protoc 命令(或 .proto 文件)中指定生成的
即使每个 python grpc 快速入门引用都使用 grpc_tools.protoc生成实现proto文件的python类,the closest thing to documentation t
我是一名优秀的程序员,十分优秀!