gpt4 book ai didi

java - 检查 Protocol Buffer 3 中是否设置了字段

转载 作者:搜寻专家 更新时间:2023-11-01 02:19:32 26 4
gpt4 key购买 nike

我正在将 Java 应用程序从 Protocol Buffer 2 迁移到 Protocol Buffer 3。

在 proto 2 中检查是否设置了字段你有 hasfield() 方法为此生成的示例 Java 代码是:

public boolean hasText() {
return ((bitField0_ & 0x00000004) == 0x00000004);
}

但是在 proto 3 中它已被删除。你如何检查一个字段是否已经在 proto 3 中设置?

最佳答案

给出了一种建议的方法 here :

# NOTE: As of proto3, HasField() only works for message fields, not for
# singular (non-message) fields. First try to use HasField and
# if it fails (with a ValueError) we manually consult the fields.
try:
return message_pb.HasField(property_name)
except ValueError:
all_fields = set([field.name for field in message_pb._fields])
return property_name in all_fields

此外,来自同一页面:

In proto3, field presence for scalar fields simply doesn't exist. Your mental model for proto3 should be that it's a C++ or Go struct. For integers and strings, there is no such thing as being set or not, it always has a value. For submessages, it's a pointer to the submessage instance which can be NULL, that's why you can test presence for it.

关于java - 检查 Protocol Buffer 3 中是否设置了字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51918871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com