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的更多相关文章

  1. dart之旅(二)- 内建类型

    目录 number 类型 字符串 布尔类型 像大多数语言一样,dart 也提供了 number,string,boolean 等类型,包括以下几种: numbers strings booleans ...

  2. python高级编程之(类级):子类内建类型

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #类级 #在2.2中,提出了类型(type0与类(class)统一( ...

  3. 零基础学python-4.2 其它内建类型

    这一章节我们来聊聊其它内建类型 1.类型type 在python2.2的时候,type是通过字符串实现的,再后来才把类型和类统一 我们再次使用上一章节的图片来说明一些问题 我们通过对照上面的图片.在p ...

  4. C/C++ 指针的非空判断

    一定要分得清楚C和C++的“空指针常量”不是一样的.C标准不保证NULL等于0,所以做指针非空判断时,应该用if(p != NULL):因为“上下文转换到bool值”的统一性,C++就应该用if(p) ...

  5. JAVAWEB servlet验证登录时进行完全的非空判断防止空值登录

    如果不进行完全的非空判断,那么对 "" 这种类型的空值就会导致直接登录 所以需要用下面的字符串处理方法对其进行判断 这样就可以防止空值登录了 容易出现的混淆错误: 这里的空值登录容 ...

  6. java基础之----非空判断

    大家好,第一次写博客,一直想写博客,用于自我总结,也用于帮助新同学成长. 平常我们开发的时候,用到很多非空判断,但是很多同学用到的地方不是很准确,这里,我把自己平时遇到的坑跟大家说说.我废话不多,只想 ...

  7. List和String的非空判断

    1.如果想判断list是否为空,可以这么判断: if(null == list || list.size() ==0 ){ //为空的情况 }else{ //不为空的情况 } 2.list.isEmp ...

  8. throw关键字和Objects非空判断_requireNonNull方法

    作用: 可以使用throw关键字在指定的方法中抛出指定的异常 使用格式: throw new xxxException("异常产生的原因") 注意: 1.throw关键字必须写在方 ...

  9. String的非空判断、Integer的非空判断、list的大小判断,对象的非空判断

    1.String的非空判断. StringUtils.isNotEmpty(String str); 2.Integer的非空判断. null != Integer ; 3.list的大小判断. li ...

随机推荐

  1. 谈谈对MapTask任务分配和Shuffle的理解

    一.切片与MapTask的关系 1.概述 大家要注意区分切片与切块的区别: 切块Block是HDFS物理上把数据分成一块一块的,默认是128M: 数据切片:只是在逻辑上对输入进行分片,并不会在磁盘上分 ...

  2. 吴裕雄--天生自然java开发常用类库学习笔记:Map接口使用的注意事项

    import java.util.HashMap ; import java.util.Map ; import java.util.Set ; import java.util.Iterator ; ...

  3. MongoDB_01

    解释:MongoDB可应对 --三高需求 High performance-对数据库高并发读写的需求 Huge Storage -对海量数据的高效率存储和访问的需求 High Scalability ...

  4. [强网杯 2019]Upload

    0x00 知识点 代码审计,PHP 反序列化. 0x01 解题 先注册一个账号,再登陆 上传 简单测试一下: 只能上传能被正常查看的 png. F12看到文件上传路径 扫扫敏感文件 存在:/www.t ...

  5. node重点 模块

    node模块 1.全局模块(对象)(像js中的window document) 定义:何时何地都可以访问,不需要引用 1.process.env 环境变量 计算机属性 高级系统设置 高级 环境变量 作 ...

  6. JS - 查找字符串中的某个值,截取其之前。和之后的值

    var str = "11:222"; /* *   截取 “ :”之前和之后的值 */document.write(str.split(':')[0])    //输出11doc ...

  7. 3 —— node —— 文件追加内容

    思想 : 先读取 , 再追加 const fs = require('fs') fs.readFile("./hello.txt","utf-8",(err,d ...

  8. gitolite服务器配置的一些心得

    1.假设说有服务器1,hostname为lab1,服务器2,hostname为lab2,分别生成的给对方使用的公钥为server-lab1.pub.server-lab2.pub,服务器1和2都有自己 ...

  9. 031-PHP获取图像信息

    <?php list($width, $height, $type, $attr) = getimagesize("plane.jpg"); echo "宽度为:& ...

  10. 114-PHP判断类变量是否相同

    <?php class ren{ //定义人类 } class mao{ //定义人类 } $ren=new ren(); //实例化人类的对象 $ren_a=new ren(); //实例化人 ...