Skip to content

Commit

Permalink
重新设计设计文档
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Sep 27, 2017
1 parent 211e22b commit 82f0534
Show file tree
Hide file tree
Showing 5 changed files with 376 additions and 139 deletions.
231 changes: 144 additions & 87 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,100 +1,102 @@
# gosproto
[sproto](https://github.com/cloudwu/sproto) golang implement
[sproto](https://github.com/cloudwu/sproto)云风的sproto协议描述文件及代码生成工具

# Based on code
English Doc see below:
https://github.com/davyxu/gosproto/blob/master/README_en.md

# 代码基于

https://github.com/xjdrew/gosproto

# Features
Similar to [golang protobuf](https://github.com/golang/protobuf) version 3
# 特性

No need use sproto.Int32( ), sproto.String() wrapper to generate point field in order to implement optional field
* 兼容云风版sproto格式,并扩充的格式,比Protobuf更方便, 更快

Optional will be auto processed by comparing incoming value and type default value
* 代码生成输出支持go,c#,lua以及云风原版sproto

bool default is false
# 描述文件格式(*.sp)

integer default is 0

string default is ""
下面是sprotogen支持的推荐格式
```
Fields equals to its default value will be skipped encoding and decoding automatically'
enum Vocation {
Monkey
Monk
Pig
}
USE SPROTO LIKE NORMAL STRUCT!
message PhoneNumber {
# Type map
sproto type | golang type
---------------- | -------------------------------------------------
string | \string, []byte
integer | \int8, \uint8, \int16, \uint16, \int32, \uint32, \int64, \uint64, \int, \uint
boolean | \bool
object | \struct
*string | []string
*integer | []int8, []uint8, []int16, []uint16, []int32, []uint32, []int64, []uint64, []int, []uint
*boolean | []bool
*struct | []\*struct
*struct(index) | []\*struct
number string
# Schema parser and meta info
type int32
}
https://github.com/davyxu/gosproto/tree/master/meta
# Code generator
message Person {
https://github.com/davyxu/gosproto/tree/master/sprotogen
name string
## Install
id int32
```
go get -u -v github.com/davyxu/gosproto/sprotogen
```
email string
## Usage
phone PhoneNumber
voc Vocation
}
message AddressBook {
person []Person
}
```
# Generate go source file by sproto
sprotogen --type=go --out=addressbook.go --gopackage=example addressbook.sp
# Generate c# source file by sproto
sprotogen --type=cs --out=addressbook.cs --gopackage=example addressbook.sp
# Convert to standard sproto file
sprotogen --type=sproto --out=addressbook.sproto addressbook.sp
```
## 特性

# Protobuf Schema --> sproto Schema convertor
* 自动生成tag序列号(base0,sproto规范),也可以手动指定

https://github.com/davyxu/gosproto/tree/master/pb2sproto
* 自动生成枚举序号(base0),也可以手动指定

## Features
Keep all message leading comments and field trailing comments
* 类go结构体字段命名方式

## Install
* 比Protobuf更方便的导出注释内容做协议扩充

```
go get -u -v github.com/davyxu/gosproto/pb2sproto
```
Requires: github.com/davyxu/pbmeta
## 兼容云风的sproto描述格式(*.sproto)

## Usage
解析器可以同时兼容两种格式书写,可以在任何地方混合书写. 更推荐使用sp格式,有更强的扩展和兼容性

```
# Use protoc and github.com/davyxu/pbmeta/protoc-gen-meta to generate protobuf meta info file(contains comments)
# see github.com/davyxu/gosproto/pb2sproto/Make.bat
# Use meta info to generate sproto file
pb2sproto --pbmeta=meta.pb --outdir=.
```
.PhoneNumber {
## Remark
Due to sproto not support float field type, all float double format will convert to int32 type
number 0 : string
# Example
type 1 : integer
}
.Person {
name 0 : string
id 1 : integer
email 2 : string
phone 3 : *PhoneNumber
}
.AddressBook {
person 0 : *Person
}
```

https://github.com/davyxu/gosproto/tree/master/example

# Go版的使用方法
```golang
input := &AddressBook{
Person: []*Person{
Expand Down Expand Up @@ -128,7 +130,7 @@ https://github.com/davyxu/gosproto/tree/master/example
data, err := sproto.Encode(input)

if err != nil {
t.Log(err)
t.Log(err)
}

var sample AddressBook
Expand All @@ -140,45 +142,100 @@ https://github.com/davyxu/gosproto/tree/master/example

```

# 支持类型

# Test
* int32: 32位整形
* int64: 64位整形
* uint32: 无符号32位整形
* uint64: 无符号64位整形
* string: 字符串
* float32: 单精度浮点数(默认精度0.001)
* float64: 双精度浮点数(默认精度0.001)
* bytes: 二进制数据
* enum: int32封装
* bool: 布尔
* message 结构体

所有类型前添加[]表示数组

# 编译

```golang
go test github.com/davyxu/gosproto
```
go get -u -v github.com/davyxu/gosproto/sprotogen
```

# 下载

https://github.com/davyxu/gosproto/releases

# sprotogen命令行参数

# Benchmark
* go_out
输出Go源码

* lua_out
输出lua源码,兼容云风版本

* cs_out
输出C#源码, 配套基类库请参考https://github.com/davyxu/sproto-Csharp

* sproto_out
输出云风版sproto的描述文件

* package
包或者命名空间名

* cellnet_reg
在Go的生成代码中添加cellnet自动注册入口

# 使用方法

```
$ go test -bench . github.com/davyxu/gosproto
BenchmarkEncode-8 500000 2498 ns/op
BenchmarkDecode-8 500000 3134 ns/op
BenchmarkEncodePacked-8 500000 2894 ns/op
BenchmarkDecodePacked-8 500000 3480 ns/op
PASS
ok github.com/davyxu/gosproto 6.162s
# 输出go源码
sprotogen --go_out=addressbook.go --package=example addressbook.sp
# 生成C#源码
sprotogen --cs_out=addressbook.cs --package=example addressbook.sp
```

* xjdrew/gosproto Version
# 例子

https://github.com/davyxu/gosproto/tree/master/example



# 可选工具:Protobuf描述格式转sproto描述格式

https://github.com/davyxu/gosproto/tree/master/pb2sproto

## 特性
保留所有protobuf的注释

## 安装方法

```
$ go test -bench . github.com/xjdrew/gosproto
BenchmarkEncode-8 1000000 1931 ns/op
BenchmarkDecode-8 500000 3498 ns/op
BenchmarkEncodePacked-8 500000 2476 ns/op
BenchmarkDecodePacked-8 500000 3896 ns/op
PASS
ok github.com/xjdrew/gosproto 7.024s
go get -u -v github.com/davyxu/gosproto/pb2sproto
```
第三方库依赖: github.com/davyxu/pbmeta

## 使用方法

```
# 使用Protobuf编译器:protoc配合插件github.com/davyxu/pbmeta/protoc-gen-meta 生成pb的上下文信息
# 参见: github.com/davyxu/gosproto/pb2sproto/Make.bat
# 根据上下文信息导出sproto
pb2sproto --pbmeta=meta.pb --outdir=.
```

# Feedback
# 备注

Star me if you like or use, thanks
感觉不错请star, 谢谢!

blog(Chinese): [http://www.cppblog.com/sunicdavy](http://www.cppblog.com/sunicdavy)
博客: http://www.cppblog.com/sunicdavy

zhihu(Chinese): [http://www.zhihu.com/people/sunicdavy](http://www.zhihu.com/people/sunicdavy)
知乎: http://www.zhihu.com/people/sunicdavy

提交bug及特性: https://github.com/davyxu/gosproto/issues
Loading

0 comments on commit 82f0534

Please sign in to comment.