Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing of existence of a repeated field in a protobuff message

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;
}
like image 778
Ysh Avatar asked Oct 03 '16 13:10

Ysh


People also ask

Are repeated fields ordered in Protobuf?

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.

What is repeated field in Protobuf?

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.

What is a repeated field?

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.

What is Protobuf message?

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.


1 Answers

You could try to test the lenght of the repeated field:

assert len(foo.bar) == 0
like image 77
Emmanuel Jay Avatar answered Oct 15 '22 16:10

Emmanuel Jay