proto3 移除了內建類型的非空判斷方法
- 即代碼生成工具不會為 bool int 等類型生成has方法
有使用過proto2 或者其它rpc 框架的人都知道使用has 方法去判斷消息里的值是否設置,
而在proto3 里只有自定義類型才能有這個方法了,
這個問題在github上引起激烈討論, 以下是開發人員在github的解釋
大致意思是從簡易性和實踐出發降低復雜度,停止支持為空的判斷, 但是用戶依舊有其它辦法支持has,例如oneof和自定義類型
I brought up the syntax sugar proposal in #1606 (comment) to protobuf team meetings and after some discussions the decision is to not move forward with the proposal:
Rationale of removing field presence in proto3:
Field presence in proto2 has caused confusions and it complicates the semantics, e.g. one
has to distinguish between absence fields vs fields set to their default values; users usually
check presence before accessing the fields which is unnecessary. We believe in most cases,
field presence info is not needed.
Removing field presence makes Proto3 significantly easier to implement with open struct
representations, as in languages like Android Java (go/nano-proto), or Go. The easier
implementation in turn makes it better accessible to external implementer communities.
If such presence info is explicitly needed, there are several workarounds, e.g. wrappers, explicit
has_field boolean. Oneof can also be used if backward wire compatibility with proto2 optional
field is desired.
Introducing a new keyword or reusing an existing keyword to support field presence in proto3
will complicate protobuf semantics. We believe it will lead to confusion and misuse, which
defeats the purpose of removing field presence in proto3.
We may reconsider the proposal in the future when there are more data showing field presence are used more often than we expect in proto3, but at the moment we recommend users to design their proto3 protos without relying on field presence.