【问题标题】:Need to convert python dict in proto message format for gRPC需要将 python dict 转换为 gRPC 的 proto 消息格式
【发布时间】:2018-07-24 05:03:00
【问题描述】:

我有这样的格式,想把我的 python dict 转换成这个 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;

}

python 字典:

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

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

【问题讨论】:

    标签: python grpc proto


    【解决方案1】:

    尚不完全清楚您的.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 内容中,您在另一个消息定义中嵌套了两个消息定义,这也有点奇怪 - 这看起来根本没有必要。

    【讨论】:

      猜你喜欢
      • 2021-05-10
      • 2021-11-28
      • 2021-11-07
      • 2021-11-06
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      相关资源
      最近更新 更多