False 布尔值未在 proto3 中显示 Python
False Boolean is not showing in proto3 Python
我在 python
中使用 protoBuffer 3
它没有正确显示 bool
字段。
entity_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
这在某种程度上是可以的。主要问题是当我使用 Http trancoding
时如果我尝试打印 console.log(foo.active)
是给我 undefined
而不是 false
(lang: JavaScript)
谁能告诉我为什么 False
值没有显示。
Prototbuf 具有 默认 字段值,例如布尔值 false、数字零或空字符串。
它不会对这些进行编码,因为它会浪费 space and/or 带宽(传输时)。这可能就是它没有出现的原因。
检查这一点的一个好方法是将 id
设置为空字符串并查看其行为是否类似:
foo = entity_proto.Foo(id='', active=True)
print(foo)
# active: True (I suspect).
解决方案实际上取决于 undefined
的来源。 Javascript 有一个真正的未定义值,在这种情况下,您可以使用 null/undefined 合并运算符:
console.log(foo.active ?? false)
或者,如果此 HTTP 转码器正在执行类似创建文字 "undefined" 字符串的操作,您将必须弄清楚如何将(可能的)None
转换为 "false".
根据 protobuf 语言指南
https://developers.google.com/protocol-buffers/docs/proto3#scalar
"Also note that if a scalar message field is set to its default, the value will not be serialized on the wire."
所以对于你的布尔字段,如果它的默认值甚至没有序列化。您的另一个选择是设置一些 int/string 字段并将其设置为某个值,以便您可以决定您的逻辑。
我在 python
中使用 protoBuffer 3
它没有正确显示 bool
字段。
entity_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
这在某种程度上是可以的。主要问题是当我使用 Http trancoding
时如果我尝试打印 console.log(foo.active)
是给我 undefined
而不是 false
(lang: JavaScript)
谁能告诉我为什么 False
值没有显示。
Prototbuf 具有 默认 字段值,例如布尔值 false、数字零或空字符串。
它不会对这些进行编码,因为它会浪费 space and/or 带宽(传输时)。这可能就是它没有出现的原因。
检查这一点的一个好方法是将 id
设置为空字符串并查看其行为是否类似:
foo = entity_proto.Foo(id='', active=True)
print(foo)
# active: True (I suspect).
解决方案实际上取决于 undefined
的来源。 Javascript 有一个真正的未定义值,在这种情况下,您可以使用 null/undefined 合并运算符:
console.log(foo.active ?? false)
或者,如果此 HTTP 转码器正在执行类似创建文字 "undefined" 字符串的操作,您将必须弄清楚如何将(可能的)None
转换为 "false".
根据 protobuf 语言指南
https://developers.google.com/protocol-buffers/docs/proto3#scalar
"Also note that if a scalar message field is set to its default, the value will not be serialized on the wire."
所以对于你的布尔字段,如果它的默认值甚至没有序列化。您的另一个选择是设置一些 int/string 字段并将其设置为某个值,以便您可以决定您的逻辑。