I'm a newbie to protobufs and I can't seem to get it. I have a proto file like this.
message Address{
string Home=1;
State state=2;
string Name=3;
enum State{
STATE_UNKNOWN=0;
STATE_ARCHIVED=1;
}
}
And I've added data to the message that looks like this.
Address{
Home:"Cornfield";
State: STATE_UNKNOWN;
Name:"Corner";
}
Address{
Home:"Ham";
State: STATE_UNKNOWN;
Name:"Hammer";
}
data = Address.getfielddescriptor() The field descriptor method can't return a list of values like data=['Cornfield','Ham']
How would I do this?
For you to be able to use a list you need to define a field as repeated. So somewhere you need to define something like an Address Book where you store all your addresses:
message Address {
string home = 1;
State state = 2;
string name = 3;
enum State {
STATE_UNKNOWN = 0;
STATE_ARCHIVED = 1;
}
}
// Your address book message
message AddressBook {
repeated addresses= 1;
}
Next in python you use this as followed:
address_book = AddressBook()
addr = address_book.addresses.add()
addr.home = "Cornfield"
addr.state = STATE_UNKNOWN
addr.name = "Corner"
# You can also first create an address object and extend the list
addr2 = Address()
addr2.home = "Ham"
addr2.state = STATE_UNKNOWN
addr2.name = "Hammer"
address_book.addresses.extend(addr2)
# You can use the list of addresses like any other list:
# By index: address_book.addresses[0]
# or in a list: for addr in address_book.addresses:
Other methods of extending the address book can be found in the protobuf documentation here.