Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

protobuf-net - generated class from .proto - Is repeated field supposed to be Read Only with no setter?

Tags:

I am totally confused about this. I have looked around and can't seem to find a direct answer. I have a .proto file that my project, which has all been java, uses to create some messages.

There is a repeated Info field. Which is a type we created. When I generate the C# classes with protogen, this field comes up as read only and has no setter.

I can't fully build the message without this parameter. So my question is. Are repeated fields supposed to be generated like this and I am supposed to be accessing this read only List some other way? Or is this a bug in the generator?

Generated code:

private readonly global::System.Collections.Generic.List<StringMapEntry> _factoryProperty = new global::System.Collections.Generic.List<StringMapEntry>();
[global::ProtoBuf.ProtoMember(2, Name=@"factoryProperty", DataFormat = global::ProtoBuf.DataFormat.Default)]
public global::System.Collections.Generic.List<StringMapEntry> factoryProperty
{
  get { return _factoryProperty; }
}

Proto file section:

repeated StringMapEntry factoryProperty = 2;

I was probably just missing something really obvious. Thanks for any help!

like image 674
Mimerr Avatar asked May 17 '13 21:05

Mimerr


People also ask

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.

Why are Protobuf fields numbered?

Field numbers are an important part of Protobuf. They're used to identify fields in the binary encoded data, which means they can't change from version to version of your service. The advantage is that backward compatibility and forward compatibility are possible.

What is .proto file?

A . proto file is similar to a JSON file in that it represents structured data, but you can run a compiler on the . proto file to generate code that can read and write the data in the programming language of your choice.

What does oneof mean in Protobuf?

Protocol Buffer (Protobuf) provides two simpler options for dealing with values that might be of more than one type. The Any type can represent any known Protobuf message type. And you can use the oneof keyword to specify that only one of a range of fields can be set in any message.


1 Answers

The list is not read only... You just mutate the list it gives you:

var order = new Order();
order.Lines.Add( new OrderLine {...} );

It is actually pretty common for sub-collections to be get-only. That doesn't mean you can't change the contents.

like image 164
Marc Gravell Avatar answered Sep 17 '22 11:09

Marc Gravell