76

I have to compose a protobuf message which should have 1 integer variables and a integer array.

package protobuf;

message myProto {

optional uint32 message_id =1;
optional int update = 2;
//here I have to add a array of integers
//can I write like     optional int[] array =3;
//or should I use      optional repeated array;
//where array is another message with int variable

}

Is my approach correct?

1 Answer 1

103

Array is mapped via "repeated":

 repeated int32 data = 4;

Note you might want sint32/uint32. Also note that in all three cases "packed arrays" can be used, which are more efficient;

repeated int32 data = 4 [packed=true];
Sign up to request clarification or add additional context in comments.

8 Comments

is the packed array concept applicable for double arrays also?
in above 4 is tag or array size ?
@Raj tag; the size of the data is determined solely at runtime by how much data is there; there are no fixed sized arrays in protobuf. In the case of "packed", the size (in bytes) is prefixed to the data.
Update: developers.google.com/protocol-buffers/docs/proto3 states "In proto3, repeated fields of scalar numeric types use packed encoding by default."
@Jason very true (I wrote a different reply, then realised the question was purely about .proto schema, not the thing I was talking about)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.