I have a google protobuf message:
message Foo {
required int bar = 1;
}
I know that in order to test the fields of message, we can use:
foo.bar = 1
assert foo.HasField("bar")
However "HasField" doesn't work for repeated field types. How to test for existence of field for "repeated type" of fields?
message Foo {
repeated int bar = 1;
}
Yes, repeated fields retain the order of items. From Google's Protocol Buffers encoding specification: The order of the elements with respect to each other is preserved when parsing, though the ordering with respect to other fields is lost.
repeated : this field can be repeated any number of times (including zero) in a well-formed message. The order of the repeated values will be preserved.
A repeated field can be accessed as an ARRAY type in standard SQL. A RECORD column can have REPEATED mode, which is represented as an array of STRUCT types. Also, a field within a record can be repeated, which is represented as a STRUCT that contains an ARRAY . An array cannot contain another array directly.
namespace google::protobuf. Defines Message, the abstract interface implemented by non-lite protocol message objects. Although it's possible to implement this interface manually, most users will use the protocol compiler to generate implementations.
You could try to test the lenght of the repeated field:
assert len(foo.bar) == 0
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