I have a proto message:
syntax = "proto3";
import "google/protobuf/any.proto";
message Task {
repeated google.protobuf.Any targets = 1;
// ...
}
message Target {
string name = 1;
// ...
}
How should I add Target messages into Task.targets?
In official docs I've found info about how to assign value to a single Any type value, however in my case I have repeated Any field type.
Edit: Task.targets may contain different types of targets, that's why Any type is used. A single Target message is just for minimal reproducible example.
Thanks @Justin Schoen. According to https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/Any, you need to first create an Any object, then Pack a Target (or any other object type) before appending it to the repeated list.
from google.protobuf.any_pb2 import Any
task = Task()
target = Any()
target.Pack(Target())
task.targets.append(any)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With