一、简介

proto3是新版本的protobuf语法。它简化了部分语法,并扩大了支持的语言,Ruby、C#。目前谷歌推荐只在是用新支持的语言或使用新的gRPC框架的时候使用。
proto2和proto3不是完全兼容的。这里列出一些proto2与proto3的区别。

二、proto3

希望编译器使用proto3进行编译需要在文件对行加上:

syntax = "proto3";

下面是一个简单的例子:

syntax = "proto3";

message Person {
string name = 1;
int32 age = 2;
repeated string loction = 3;
}

可以看到相比于proto2语法这里没有前面的required/optional,在proto3中所有字段都是可选的,同时取消了自定义默认值,默认值为0或空。

枚举

与proto2不同proto3的枚举值第一个值得tag必须为0,同时枚举值的默认值将默认使用第一个值,这样就和其他类型都保持了统一,即默认值为0。

 enum Location {
SHANGHAI = 0;
BEIJING = 1;
GUANGZHOU = 2;
}

同时proto2的枚举类型不能被proto3直接import,但是间接引用不受影响。

Any

proto3不支持proto2中的extension,但是引入了Any。
在使用Any时需要引入any.proto:

import "google/protobuf/any.proto";

message ErrorStatus {
string message = 1;
repeated google.protobuf.Any details = 2;
}
-----------------------
public static void main(String[] args) throws IOException, ClassNotFoundException {
//定义any
Any any= Any.pack(StringValue.newBuilder().setValue("aaa").build());
//赋值并构建
Demo.Person person=Demo.Person.newBuilder().setDetail(any).build();
//取值
System.out.println(person.getDetail().unpack(StringValue.class));
}

三、其他

proto3去除了proto2中group,新增了一些timestamp、empty的格式(需要手动import)。

proto 3 语法的更多相关文章

  1. proto 2 语法

    一.proto文件 PB的定义是通过proto文件进行定义的,一个标准的类型如下: message SearchRequest { required string query = 1; optiona ...

  2. github atom创建自己的语法高亮

    使用atom一段时间了,有些插件还不是很成熟.比如项目中使用protobuf,早就有人写了语法高亮(https://github.com/podgib/atom-protobuf),但是效果不是很好. ...

  3. Protobuf 语法指南

    英文: Proto Buffers Language Guide 本指南描述了怎样使用protocol buffer 语法来构造你的protocol buffer数据,包括.proto文件语法以及怎样 ...

  4. iOS 集成Protobuf,转换proto文件

    原文地址:http://blog.csdn.net/hyq4412/article/details/54891038 附加Homebrew安装地址:https://brew.sh/index_zh-c ...

  5. protobuf 语法 与 protocol-buffers 的使用

    前言 protocol-buffers 是 node.js 平台对支持 protobuf 封装的三方模块,下面的例子都通过 protocol-buffers 的使用来说明. 什么是protobuf G ...

  6. gRPC-Protocol语法指南

    语法指南 (proto3) Defining A Message Type Scalar Value Types Default Values Enumerations Using Other Mes ...

  7. gRPC源码分析0-导读

    gRPC是Google开源的新一代RPC框架,官网是http://www.grpc.io.正式发布于2016年8月,技术栈非常的新,基于HTTP/2,netty4.1,proto3.虽然目前在工程化方 ...

  8. Protocol buffers 介绍

    Protocol buffers和mxl一样在序列化数据结构时很灵活.高效和智能,但是它的优势在于定义文件更小,读取速度更快,使用更加简单.目前protocol buffers支持C++.java和p ...

  9. Protobuf语言指南(转)

    Protobuf语言指南 l  定义一个消息(message)类型 l  标量值类型 l  Optional 的字段及默认值 l  枚举 l  使用其他消息类型 l  嵌套类型 l  更新一个消息类型 ...

随机推荐

  1. Linux 文件umask默认权限_012

    一.       umask介绍 Linux 系统用户创建一个新的目录或文件时,系统会默认会分配相应的权限.目录或文件的权限是如何产生的呢? 1.这就是umask的功能,umask设置了用户创建文件或 ...

  2. Centos 使用Systemctl报Error getting authority: Error initializing authority: Error calling StartServiceByName for org.freedesktop.PolicyKit1: Timeout was reached (g-io-error-quark, 24)

    在使用centos7.4 安装服务的时候报错: Error getting authority: Error initializing authority: Error calling StartSe ...

  3. phpstorm 破解

    http://idea.lanyus.com/ https://www.imsxm.com/jetbrains-license-server.html [  http://www.activejetb ...

  4. Oracle导出数据EXP00106错误

    在导出dmp文件的时候(命令:exp 用户名/密码@IP/实例名  file=D:\20180910.dmp log=D:\20180910.log),遇到以下错误: 错误原因: 导出使用的是Orac ...

  5. cpu选型

    就像手机有骁龙845和麒麟980等,电脑的cpu也有intel和amd,intel有台式机cpu, 网上搜索最具性价比神u:G4560,i3-8100, intel八代,包含奔腾G5600/G5500 ...

  6. hibernate02环境的搭建

    hibernate: 持久层的框架!是一个开放源代码的对象关系映射框架(ORM)!之前我们访问数据库使用JDBC!对JDBC进行了轻量级的对象封装!是一个全自动ORM框架!(底层可以自动生成sql语句 ...

  7. 45道sql

    一.设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2 ...

  8. 设置弹窗、遮罩的样式设置(包括:left、heigh等)

    .zhezhao { width:100%; background-color:#000; filter:alpha(opacity=50); -moz-opacity:0.5; opacity:0. ...

  9. PAT甲级1057 Stack【树状数组】【二分】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和 ...

  10. Codeforces 1072 - A/B/C/D - (Done)

    链接:http://codeforces.com/contest/1072/ A - Golden Plate - [计算题] #include<bits/stdc++.h> using ...