-1

I have a format like this and want to convert my python dict to this proto format proto code:

message template{

 message NicSettings{
    string network_adapter = 1;
    string network_name = 2;
 }

 message NetConfig{
    bool keep_mac_address =1;
    bool same_as_source = 2;
   repeated  NicSettings nic_settings = 3;
 }

 NetworkConfig network_config = 3;

}

python dict:

 template:
  { keepMacAddress: true, 
    sameAsSource : false, 
    nicSettings: [ {networkAdapter: "ethernet0", 
                    networkName: "Calpal1"
                    } ,
                    {networkAdapter: "ethernet1", 
                    networkName: "Calpal2"
                    } 
                 ] 
   }

How do I convert this to a proto message to pass it to gRPC.

Rohan Nagalkar
  • 433
  • 2
  • 5
  • 15

1 Answers1

0

It's not entirely clear that your .proto is correct (or are there typos in it?) but this looks like it should be something along the lines of

my_template_message = my_message_module_pb2.template( keep_mac_address=my_dictionary['keepMacAddress'], same_as_source=my_dictionary['sameAsSource'], nic_settings=tuple( my_message_module_pb2.template.NicSettings( network_adapter=my_subdictionary['networkAdapter'], network_name=my_subdictionary['networkName']) for my_subdictionary in my_dictionary['nicSettings'] ) ) . It's also a little odd in your .proto content that you've nested two message definitions inside another message definition - that doesn't look necessary at all.