1 Star 0 Fork 0

flyingfether / gosproto

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Build Status codecov

gosproto

sproto's encoder and decoder in golang.

type map

sproto type golang type
string *string, []byte, string
integer *int8, *uint8, *int16, *uint16, *int32, *uint32, *int64, *uint64, *int, *uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64, int, uint
boolean *bool, bool
object *struct
array of string []string
array of integer []int8, []uint8, []int16, []uint16, []int32, []uint32, []int64, []uint64, []int, []uint
array of boolean []bool
array of object []*struct

schema

You can define go struct corresponding to sproto schema directly as examples in all test cases. Or use sprotodump to change sproto schema to go file.

test

go test github.com/xjdrew/gosproto

benchmark

BenchmarkEncode-8                 500000              2058 ns/op
BenchmarkDecode-8                 500000              3222 ns/op
BenchmarkEncodePacked-8           500000              2523 ns/op
BenchmarkDecodePacked-8           500000              3660 ns/op
PASS
ok      github.com/szyhf/go-sproto      5.864s

改进说明(improvement)

原来的sproto只支持指针类型的变量,要通过sproto.Int(ptr *int)类似的方法赋值访问,非常麻烦。

注:原来这是proto2的标准实现方法,但proto3已经不再使用指针的方案——20170228

这个库增强了Encode和Decode的能力,现在可以直接处理int string bool的值类型。

同时,使用相同位置及配置的值类型及对应指针,在tag一致的情况下,且指针均不为nil的情况下,编码结果是相等的,例如:

type WithPtr struct{
	ID *int `sproto:"integer,0,name=ID"`
}

type WithVal struct{
	ID int `sproto:"integer,0,name=ID"`
}

如果WithPtr.ID=nil,则使用WithVal对编码进行Decode之后,WithVal.ID会等于0。

个人倾向尽可能不要使用nil作为值,作为一个用于跨平台的编码,nil容易在不同平台上产生不同的解析结果,极易产生歧义。

同样,个人不建议使用值类型的Struct,所以不支持了。

更多的实现效果请参考encode_test.go中的例子。

空文件

简介

暂无描述 展开 收起
Go
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Go
1
https://gitee.com/flyingfether/gosproto.git
git@gitee.com:flyingfether/gosproto.git
flyingfether
gosproto
gosproto
master

搜索帮助