如何使用 protobuf 在 python 中设置 oneof 字段值
How to set oneof field value in python using protobuf
我有一个带有oneof字段的protobuf。 "Oneof" 是取其中一个字段值还是一起取?不确定是否有办法设置它。
当我 运行 "pb2.HasField('signal')
时,我期待一个真实的值
原型文件:
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;
}
}
所以在 python 中导入 pb2 之后,我该如何设置 'signal' 的值。
当我尝试时:
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.
如何强制 'Oneof' 字段只包含一个消息,例如 GPS。
还有 pb2.WhichOneof('signal')
returns 什么都没有(空)。
我什至不确定这是否可能。
看起来我缺少主 protobuf 文件,这是我想要的信息。字段值在 prot_master 文件中指定。
我有一个带有oneof字段的protobuf。 "Oneof" 是取其中一个字段值还是一起取?不确定是否有办法设置它。
当我 运行 "pb2.HasField('signal')
原型文件:
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;
}
}
所以在 python 中导入 pb2 之后,我该如何设置 'signal' 的值。 当我尝试时:
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.
如何强制 'Oneof' 字段只包含一个消息,例如 GPS。
还有 pb2.WhichOneof('signal')
returns 什么都没有(空)。
我什至不确定这是否可能。
看起来我缺少主 protobuf 文件,这是我想要的信息。字段值在 prot_master 文件中指定。