官方网站:

https://developers.google.com/protocol-buffers/docs/proto3

1、简单定义一个Message 类型

pb语法文件以"*.proto"为文件扩展名。在版本proto3文件头需要包含版本类型“syntax = "proto3";”,缺省情况为proto2类型。

官方示例:

syntax = "proto3";

message SearchRequest {
  string query = 1;
  int32 page_number = 2;
  int32 result_per_page = 3;
}

message为类标识符

SearchRequest为类名

string/int32为字段类型

query/page_number/result_per_page为字段名称

1/2/3为编号标签,用于序列化后内容标识,序号1~15需要1个字节,16~2047需要2个字节,最小编号为1,最大编号229-1或536870911。19000~19999系统预留,不可用。

2、字段描述规则

字段的规则主要分为单数和复数,用于描述字段是否是单个字段还是数组。默认情况下字段定义都是单数“singular”,如果需要定义数组需要在字段类型前面加“repeated”。

3、添加注释

 可以在字段定义结束后加“//”注释,该注释会在生成的字段代码中。
官方示例:

message SearchRequest {

string query = 1;

int32 page_number = 2;  // Which page number do we want?

int32 result_per_page = 3;  // Number of results to return per page.

}

4、保留字段

编号定义后,无法更改,如果需要重新定义类型或删除旧的类型,需要在序列化文件中占位原定义的类型。这样才能保证更改后数据正常反序列化,并对以前数据兼容。否则数据格式不正确后,将导致对更改前的数据反序列化出错。

官方示例:

message Foo {
  reserved 2, 15, 9 to 11;
  reserved "foo", "bar";
}

5、字段类型

.proto Type Notes C++ Type Java Type Python Type[2] Go Type Ruby Type C# Type PHP Type
double   double double float float64 Float double float
float   float float float float32 Float float float
int32 Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead. int32 int int int32 Fixnum or Bignum (as required) int integer
int64 Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead. int64 long int/long[3] int64 Bignum long integer/string[5]
uint32 Uses variable-length encoding. uint32 int[1] int/long[3] uint32 Fixnum or Bignum (as required) uint integer
uint64 Uses variable-length encoding. uint64 long[1] int/long[3] uint64 Bignum ulong integer/string[5]
sint32 Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s. int32 int int int32 Fixnum or Bignum (as required) int integer
sint64 Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s. int64 long int/long[3] int64 Bignum long integer/string[5]
fixed32 Always four bytes. More efficient than uint32 if values are often greater than 228. uint32 int[1] int uint32 Fixnum or Bignum (as required) uint integer
fixed64 Always eight bytes. More efficient than uint64 if values are often greater than 256. uint64 long[1] int/long[3] uint64 Bignum ulong integer/string[5]
sfixed32 Always four bytes. int32 int int int32 Fixnum or Bignum (as required) int integer
sfixed64 Always eight bytes. int64 long int/long[3] int64 Bignum long integer/string[5]
bool   bool boolean bool bool TrueClass/FalseClass bool boolean
string A string must always contain UTF-8 encoded or 7-bit ASCII text. string String str/unicode[4] string String (UTF-8) string string
bytes May contain any arbitrary sequence of bytes. string ByteString str []byte String (ASCII-8BIT) ByteString string
  • string, 默认值为空字符串.
  • bytes, 默认值为空字节数组.
  • bool, 默认值为false.
  • 数值类型, 默认值为0.
  • enums, 从0开始的枚举.
  • message对象列, 没有设置默认值,根据不同语言给定相应的规则
  • int32,uint32,int64,uint64、bool和枚举是兼容的,可以相互转换,更改字段类型后,可以向前和向后兼容,但会强制类型转化,会截断数值,如64变32位,以前的数据会只被读取32位值。
  • sint32和sint64彼此兼容,fixed32与sfixed32彼此兼容。

6、枚举类型的定义

官方示例:

message SearchRequest {
  string query = 1;
  int32 page_number = 2;
  int32 result_per_page = 3;
  enum Corpus {
    UNIVERSAL = 0;
    WEB = 1;
    IMAGES = 2;
    LOCAL = 3;
    NEWS = 4;
    PRODUCTS = 5;
    VIDEO = 6;
  }
  Corpus corpus = 4;
}

enum为枚举关键字

enum类型必须定义枚举值0.

一般情况下不允许同值枚举存在。如果需要增加同值枚举需要添加“option allow_alias = true;”

官方示例:

enum EnumAllowingAlias {
  option allow_alias = true;
  UNKNOWN = 0;
  STARTED = 1;
  RUNNING = 1;
}

7、引用其他文件

官方示例:

import "myproject/other_protos.proto";

在多次引用过程中也可以控制引用的作用域。加关键字“public”将可以被下下次引用使用。

官方示例:

// new.proto // All definitions are moved here

 
// old.proto
// This is the proto that all clients are importing.
import public "new.proto";
import "other.proto";
   old 引用了public new和other,other只能在old里面使用。
   
// client.proto
import "old.proto";
// You use definitions from old.proto and new.proto, but not other.proto client引用了old,因为new在old引用中是public 的,所以client是可以使用new. 但不能使用other. 7、嵌套类型 message 可以嵌套多层message.
message 可以嵌套 enum.
官方示例:
message Outer {                  // Level 0
  message MiddleAA {  // Level 1
    message Inner {   // Level 2
      int64 ival = 1;
      bool  booly = 2;
    }
  }
  message MiddleBB {  // Level 1
    message Inner {   // Level 2
      int32 ival = 1;
      bool  booly = 2;
    }
  }
} 8、Any类型 PB支持未知类型。该类型包含任意序列化的消息作为字节,以及一个充当全局唯一标识符并解析为该消息类型的URL。要使用Any类型,您需要导入google / protobuf / any.proto。
官方示例:
import "google/protobuf/any.proto"; message ErrorStatus {
  string message = 1;
  repeated google.protobuf.Any details = 2;
} 9、Oneof类型
如果有一个包含多个字段的消息,并且最多可以同时设置一个字段,则可以通过使用该功能强制执行此行为并节省内存。
Oneof字段就像常规字段,除了一个共享内存中的所有字段,最多可以同时设置一个字段。 设置任何成员自动清除所有其他成员。 可以根据选择的语言检查使用case()或WhereOneof()方法设置一个值中的值。
官方示例:
message SampleMessage {
  oneof test_oneof {
    string name = 4;
    SubMessage sub_message = 9;
  }
} 10、Map类型
语法规则为:
map<key_type, value_type> map_field = N; 示例:
map<string, Project> projects = 3; 11、包Package
我们可以在“.proto”文件中定义包,可以防止类的名称冲突,在我们使用过程可以加包名来定义字段类型。
官方示例:
package foo.bar;
message Open { ... } 使用时,
message Foo {
  ...
  foo.bar.Open open = 1;
  ...
}

阅读Google Protocol Buffers 指南,整理pb语法的更多相关文章

  1. Google Protocol Buffers介绍

    简要介绍和总结protobuf的一些关键点,从我之前做的ppt里摘录而成,希望能节省protobuf初学者的入门时间.这是一个简单的Demo. Protobuf 简介 Protobuf全称Google ...

  2. C# 使用Google Protocol Buffers

    Google Protocol Buffers 使用3.0版本 下载protoc.exe 下载链接 https://github.com/protocolbuffers/protobuf/releas ...

  3. Google Protocol Buffers 入门

    Google Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化.它很适合做数据存储或 RPC 数据交换格式.可用于通讯协议.数据存储等领域的 ...

  4. Google Protocol Buffers 快速入门(带生成C#源码的方法)

    Google Protocol Buffers是google出品的一个协议生成工具,特点就是跨平台,效率高,速度快,对我们自己的程序定义和使用私有协议很有帮助. Protocol Buffers入门: ...

  5. Google Protocol Buffers简介

    什么是 protocol buffers ? Protocol buffers 是一种灵活.高效的序列化结构数据的自动机制--想想XML,但是它更小,更快,更简单.你只需要把你需要怎样结构化你的数据定 ...

  6. Google Protocol Buffers 反序列化 转

    http://www.cnblogs.com/royenhome/archive/2010/10/30/1865256.html   本文作为结束篇,会稍微介绍下怎么反序列化GoogleBuffer数 ...

  7. Google Protocol Buffers和java字符串处理控制

    大多数的操作码被从夜晚复制.懒得敲. 直接在源代码和测试结果如下. serabuffer.proto档.使用下面的命令来生成java代码. protoc -I=./ --java_out=./ ser ...

  8. DELPHI、FLASH、AS3、FLEX使用Protobuf(google Protocol Buffers)的具体方法

    最近因为工作需要,需要在不同的开发环境中应用Protobuf,特此,我专门研究了一下.为了防止自己忘记这些事情,现在记录在这里!需要的朋友可以借鉴一些,因为这些东西在GOOGLE和百度上搜索起来真的很 ...

  9. C#语言下使用gRPC、protobuf(Google Protocol Buffers)实现文件传输

    初识gRPC还是一位做JAVA的同事在项目中用到了它,为了C#的客户端程序和java的服务器程序进行通信和数据交换,当时还是对方编译成C#,我直接调用. 后来,自己下来做了C#版本gRPC编写,搜了很 ...

随机推荐

  1. jq修改导航栏样式(选中、使用两张图片替代的是否选中效果)

    <footer class="toolbar"> <ul> <li> <a href="{:url('Index/home')} ...

  2. git 使用遇到的问题

    本博客只记录遇到的问题和解决方案 问题一:git上与本地不同步无法上传 先git pull origin master再git push -u origin master(实在不行或者清空本地,或者清 ...

  3. Java 接口多继承

    按照理解,一般都是说Java 类是单继承,但可以实现多个接口.但是可以通过接口来实现类的多继承.(如何通过接口来实现多继承???) 那么就一直以为Java里面是单继承,今天看FutureTask源码的 ...

  4. Django实现微信消息推送

    一 所需准备条件 微信公众号的分类 微信消息推送 公众号 已认证公众号 服务号 已认证服务号 企业号 基于:微信认证服务号 主动推送微信消息.前提:关注服务号环境:沙箱环境 沙箱环境地址: https ...

  5. 代码之髓读后感——容器&并发

    容器 单个地址存放单个数据,但是如果有多个数据,而这些数据互相关联,则我们更希望的是将他们能够更好的在内存中组织在一起.于是便出现了容器的概念. 在不同的语言中,容器的名称不同,性质各异.比如,C 语 ...

  6. TensorFlow笔记之常见七个参数

    对TensorFlow深度学习中常见参数的总结分析 神经网络中常见的参数有:初始学习率.学习率衰减率.隐藏层节点数量.迭代轮数.正则化系数.滑动平均衰减率.批训练数量七个参数. 对这七个参数,大部分情 ...

  7. PHP-预定义函数访问数据库

    (1)复习:自定义函数 (2)调用PHP预定义的函数——访问MySQL数据库 1.函数的基础概念   定义一个简单的函数: function  函数名( ){ #函数主体 }  调用/运行一次函数: ...

  8. poj3304(是否存在一条直线与所有给出线段相交

    题意:给出n条线段,问你是否存在一条直线让他与所有线段相交. 思路:枚举两条直线的起点和终点做一条直线,看他是否与所有线段相交. #include<cstdio> #include< ...

  9. s6-1 传输层概述

    6.1 传输层概述 传输层概述 传输层是整个协议栈(TCP/IP)的核心 传输层的任务是提供可靠的.高效的数据传输 传输层的地位 网络层.传输层和应用层  传输层在应用层和网络层之间提供了无缝接口 ...

  10. $q的基本用法

    angularjs的http是异步的没有同步,一般都会遇到一个场景,会把异步请求的参数作为条件执行下一个函数,之前一直在看其他人的博客理论太多看了很久才看懂 http({ method:'post', ...