为什么我的 Protobuf 消息(在 Python 中)忽略零值? [英] Why is my Protobuf message (in Python) ignoring zero values?

查看:57
本文介绍了为什么我的 Protobuf 消息(在 Python 中)忽略零值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直致力于为一个项目实现 IPC 的 protobufs.出于某种原因,设置为 0 的值不会被设置/序列化.对于上下文,.proto 文件包含以下消息:

I've been working on implementing protobufs for IPC for a project. For some reason, values that are set to 0 are not being set/serialized. For context, the .proto file contains the following message:

syntax = "proto3";

enum SetGet {
    SET = 0;
    GET = 1;
}

message State {
    SetGet setget = 1;
    double x = 2;
    double y = 3;
    double depth = 4;
    double yaw = 5;
    double pitch = 6;
    double roll = 7; 
}

我使用 protoc 将文件编译为 Python _pb2 文件,然后尝试运行以下测试脚本:

I compile the file to a Python _pb2 file with protoc, and then I try running the following test script:

import filename_pb2 as pb

state = pb.State()
state.x = 0
state.y = 0
state.depth = 0
state.yaw = 0
state.pitch = 0
state.roll = 0
state.setget = pb.SET

print("State: {}".format(state))

state2 = pb.State()
state2.ParseFromString(state.SerializeToString())

print("State2: {}".format(state2))

当我运行它时,会打印以下输出:

When I run it, the following output is printed:

State: 
State2: 

似乎没有设置任何内容,或者以某种方式忽略了零值.但是,当我将值(x、y、深度等)更改为非零值时,例如 0.1,我得到以下预期结果:

It seems that nothing is being set, or that the zero values are somehow being ignored. However, when I change the values (x, y, depth, etc.) to something nonzero, say 0.1, I get the following, expected result:

State: x: 0.1
y: 0.1
depth: 0.1
yaw: 0.1
pitch: 0.1
roll: 0.1

State2: x: 0.1
y: 0.1
depth: 0.1
yaw: 0.1
pitch: 0.1
roll: 0.1

即使数字被打印出来,由于某种原因枚举仍然不是.为什么 protobuf 会发生这种情况?默认情况下双精度类型为 0,因此 protobuf 序列化程序通过忽略它们来节省空间?那么,为什么在解析 State2 时它们没有被恢复?我错过了文档中的某些行吗?提前致谢!

Even though the numbers are printed out, for some reason the enum still isn't. Why does this happen with protobufs? Is the double type 0 by default, so the protobuf serializer saves on space by ignoring them? Why, then, are they not being restored when State2 is parsed in? Is there some line in the documentation that I missed? Thanks in advance!

-- 蒂姆

推荐答案

是的,0 是默认值.文档中明确提到了这种情况:

Yes, 0 is the default. This case is mentioned explicitly in the documentation:

请注意,对于标量消息字段,一旦消息被解析,就会有无法判断某个字段是否明确设置为默认值值(例如布尔值是否设置为 false)或未设置完全:在定义消息类型时,您应该牢记这一点.例如,没有一个布尔值在以下情况下开启某些行为如果您不希望这种行为也发生,请设置为 false默认.另请注意,如果标量消息字段设置为其默认情况下,该值不会在线路上序列化.

Note that for scalar message fields, once a message is parsed there's no way of telling whether a field was explicitly set to the default value (for example whether a boolean was set to false) or just not set at all: you should bear this in mind when defining your message types. For example, don't have a boolean that switches on some behaviour when set to false if you don't want that behaviour to also happen by default. Also note that if a scalar message field is set to its default, the value will not be serialized on the wire.

这篇关于为什么我的 Protobuf 消息(在 Python 中)忽略零值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆