I have a a protobuf message in the form of:
message CustomMessage
{
SubMessageTypeA a_msg = 1;
SubMessageTypeB b_msg = 2;
SubMessageTypeC c_msg = 3;
}
I have several objects (assume 3) of this type that each have one of their respective fields populated. I want to collate these objects into 1 in Python. Here is a snippet of code I'm unsuccessfully using. Appreciate your help in finding the right answer:
collated_obj = CustomMessage()
for obj in [obj_1, obj_2, obj_3]:
for desc in obj.DESCRIPTOR.fields:
getattr(collated_obj, desc.name).CopyFrom(getattr(obj, desc.name))
What I'm doing is very brittle and have not been working. As a started, if the field is a basic type (e.g uint32), getattr is failing.
Is there a way to find a reference to the field of a proto other than using getattr
? Seems like that's the main part I'm stuck on.
I can convert everything to json and have an easier life. But trying to avoid repeated conversion and serialization/deserialization if possible.