我有一个原型(prototype)消息:
syntax = "proto3";
import "google/protobuf/any.proto";
message Task {
repeated google.protobuf.Any targets = 1;
// ...
}
message Target {
string name = 1;
// ...
}
我应该如何将 Target 消息添加到 Task.targets
中?
在官方文档中,我找到了有关如何为单个 Any 赋值的信息类型值,但在我的例子中,我有 repeated Any
字段类型。
编辑:Task.targets
可能包含不同类型的目标,这就是使用Any
类型的原因。单个 Target
消息仅用于最小的可重现示例。
最佳答案
感谢@Justin Schoen。根据https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/Any ,您需要先创建一个 Any 对象,然后在将其附加到重复列表之前打包一个 Target(或任何其他对象类型)。
from google.protobuf.any_pb2 import Any
task = Task()
target = Any()
target.Pack(Target())
task.targets.append(any)
关于python-3.x - Protobuf 和 Python : How to add messages to "repeatable Any" field?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56425327/