简体   繁体   English

如何使用protobuf在python中设置oneof字段值

[英]How to set oneof field value in python using protobuf

I have a protobuf with oneof field. 我有一个proofbuf与oneof场。 Does "Oneof" take one of the field values or all together? “ Oneof”采用字段值之一还是全部取值? Not sure if there is a way to set this. 不知道是否有办法设置它。

I am expecting a true value when I run "pb2.HasField('signal') 我在运行"pb2.HasField('signal')时期望一个真实值

Proto file: 原始文件:

message Log {
  string id = 1;       
  Severity severity = 2; 
  string Uid = 3;        
oneof signal{
    GPS gps_log = 1;
    Ignition ignition_log = 2;
  }
enum Severity {
    info = 0;
    critical = 1
}
message GPS{
  EventType event_type = 1;
enum EventType {
    ON = 0;
    OFF = 1;
  }
}

message Ignition{
  EventType event_type = 1;
enum EventType {
    ON = 0;
    OFF = 1;
  }
}

So after importing pb2 in python, how do I set value for the 'signal'. 因此,在python中导入pb2之后,如何设置“信号”的值。 When I try : 当我尝试:

message=pb2.Log()
res = message.DESCRIPTOR.fields
res = [field.name for field in message.DESCRIPTOR.fields]
The o/p is ['id','severity','Uid','gps_log','ignition']  - All the fields.

How to force the 'Oneof' field to have only on of the messages, eg GPS. 如何强制“一个”字段仅包含消息中的一个,例如GPS。 Also pb2.WhichOneof('signal') returns nothing (empty). 同样pb2.WhichOneof('signal')返回任何值(空)。 I am not even sure, if this is something possible. 我什至不确定,是否有可能。

Looks like i was missing the master protobuf file,which the information I wanted. 好像我缺少主protobuf文件,该文件是我想要的信息。 The field values are specified in the prot_master file. 字段值在prot_master文件中指定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM