-1

我有这样的格式,想把我的 python dict 转换成这个 proto 格式的 proto 代码:

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;

}

蟒蛇字典:

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

如何将其转换为原始消息以将其传递给 gRPC。

4

1 回答 1

0

目前尚不清楚您.proto是否正确(或者其中是否有错别字?),但这看起来应该类似于

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'] ) ) . 您的内容也有点奇怪,您.proto将两个消息定义嵌套在另一个消息定义中 - 这看起来根本没有必要。

于 2018-02-14T15:05:27.450 回答