proto3 不支持内建类型的非空判断即 hasXXX
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.
proto3 不支持内建类型的非空判断即 hasXXX的更多相关文章
- dart之旅(二)- 内建类型
目录 number 类型 字符串 布尔类型 像大多数语言一样,dart 也提供了 number,string,boolean 等类型,包括以下几种: numbers strings booleans ...
- python高级编程之(类级):子类内建类型
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #类级 #在2.2中,提出了类型(type0与类(class)统一( ...
- 零基础学python-4.2 其它内建类型
这一章节我们来聊聊其它内建类型 1.类型type 在python2.2的时候,type是通过字符串实现的,再后来才把类型和类统一 我们再次使用上一章节的图片来说明一些问题 我们通过对照上面的图片.在p ...
- C/C++ 指针的非空判断
一定要分得清楚C和C++的“空指针常量”不是一样的.C标准不保证NULL等于0,所以做指针非空判断时,应该用if(p != NULL):因为“上下文转换到bool值”的统一性,C++就应该用if(p) ...
- JAVAWEB servlet验证登录时进行完全的非空判断防止空值登录
如果不进行完全的非空判断,那么对 "" 这种类型的空值就会导致直接登录 所以需要用下面的字符串处理方法对其进行判断 这样就可以防止空值登录了 容易出现的混淆错误: 这里的空值登录容 ...
- java基础之----非空判断
大家好,第一次写博客,一直想写博客,用于自我总结,也用于帮助新同学成长. 平常我们开发的时候,用到很多非空判断,但是很多同学用到的地方不是很准确,这里,我把自己平时遇到的坑跟大家说说.我废话不多,只想 ...
- List和String的非空判断
1.如果想判断list是否为空,可以这么判断: if(null == list || list.size() ==0 ){ //为空的情况 }else{ //不为空的情况 } 2.list.isEmp ...
- throw关键字和Objects非空判断_requireNonNull方法
作用: 可以使用throw关键字在指定的方法中抛出指定的异常 使用格式: throw new xxxException("异常产生的原因") 注意: 1.throw关键字必须写在方 ...
- String的非空判断、Integer的非空判断、list的大小判断,对象的非空判断
1.String的非空判断. StringUtils.isNotEmpty(String str); 2.Integer的非空判断. null != Integer ; 3.list的大小判断. li ...
随机推荐
- ACM-生化武器
Description在一个封闭的房间里,gogo给大家表演了他的屁遁术,人果然一下没影了,但是他留下的“生化武器”,却以每秒1米的速度向上下左右扩散出去.为了知道自己会不会被“毒”到,你果断写了个算 ...
- Xshell远程连接kali,SSH服务拒绝了密码
在kali里面/etc/ssh/目录下,修改sshd_config文件,不是ssh_config,ssh_config是针对客户端的配置文件,而sshd_config是针对服务器端的配置文件. 找到# ...
- POJ - 3264 Balanced Lineup(线段树或RMQ)
题意:求区间最大值-最小值. 分析: 1.线段树 #include<cstdio> #include<cstring> #include<cstdlib> #inc ...
- 041-PHP把闭包函数当做参数传递
<?php //把闭包函数当做参数传递 function demo($obj){ $obj('我爱PHP'); } # 传一个闭包过去 demo( function($txt){ echo $t ...
- C# 借助CommandLine 写命令行工具 在数据库中创建job
首先需要用到 CommandLine.dll 提供两个下载链接,云盘是我自己上传的,也就是我在用的 http://commandline.codeplex.com/ https://pan.baid ...
- HDU 4901 多校4 经典计数DP
RT 最近不想写博客,累积了一周多的题目,今天趁着周日放假,全部补上吧 dp[i][j]表示前i个数,操作后的值为j的总个数 注意取或不取,有种完全背包的意味.因为数字小于1024,所以异或的结果也绝 ...
- Elasticsearch核心概念
Elasticsearch 核心概念 Cluster 代表一个集群, 集群中有多个节点, 其中一个为主节点, 该节点可以通过选举产生.(主从节点只针对于集群内部) 去中心化: 对于集群外来说无中心节点 ...
- 逆向-PE重定位表
重定位表 当链接器生成一个PE文件时,会假设这个文件在执行时被装载到默认的基地址处(基地址+RVA就是VA).并把code和data的相关地址写入PE文件.如果像EXE一样首先加载就是它image ...
- DRF教程10-关系字段
https://www.django-rest-framework.org/api-guide/relations/ 在编程中核心的就是数据结构. 关系字段用来表示model之间的关系,比如外键,m2 ...
- 【剑指Offer】面试题03. 数组中重复的数字
题目 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中任意 ...