proto3 Python中未显示False Boolean
我在proto3 Python中未显示False Boolean,python,protocol-buffers,Python,Protocol Buffers,我在python中使用protoBuffer 3,它没有正确显示bool字段 实体_proto message Foo { string id = 1; bool active = 3; } 在python中设置值 foo = entity_proto.Foo(id='id-123', active=True) print(foo) # id: id-123 # active: True # But if you set the value False it does not
python
中使用protoBuffer 3
,它没有正确显示bool
字段
实体_proto
message Foo {
string id = 1;
bool active = 3;
}
在python中设置值
foo = entity_proto.Foo(id='id-123', active=True)
print(foo)
# id: id-123
# active: True
# But if you set the value False it does not show 'active' in print statement
foo = entity_proto.Foo(id='id-123', active=False)
print(foo)
# id: id-123
如果您尝试打印print(foo.active)
输出为False
,这在某种程度上是正常的。当我试图打印console.log(foo.active)
时,使用httptrancoding
会出现主要问题。log(foo.active)是give me未定义的非假(lang:JavaScript)
有人能告诉我为什么它没有显示为False
值吗 Prototbuf具有字段的默认值,例如布尔值false、数字零或空字符串
它不需要对它们进行编码,因为这样会浪费空间和/或带宽(在传输时)。这可能就是它没有出现的原因
检查这一点的一个好方法是将id
设置为空字符串,并查看其行为是否类似:
foo = entity_proto.Foo(id='', active=True)
print(foo)
# active: True (I suspect).
解决方案实际上取决于未定义的来自何处。任一Javascript都有一个真正的未定义值,在这种情况下,您可以使用null/undefined合并运算符:
console.log(foo.active ?? false)
或者,如果这个HTTP转码器正在做一些类似于创建一个文本“未定义”字符串的事情,那么您必须根据protobuf语言指南,找出如何将(可能是什么)None
转换为“false”。
还请注意,如果标量消息字段设置为其默认值,则不会在线路上序列化该值
因此,对于布尔字段,如果其默认值为,则它甚至不会序列化。您的另一个选项是设置一些int/string字段并将其设置为某个值,以便您可以决定您的逻辑