Golang map/array(非结构体)序列化

huangapple go评论86阅读模式
英文:

Golang map/array (non-struct) serialization

问题

我会为你翻译以下内容:

我进行了很多查询,结果是地图或切片/数组的地图,就像这样:

// 包M
type SX map[string]interface{}
type IX map[int64]interface{}
type IAX map[int64][]interface{}
type SAX map[string][]interface{}
type SS map[string]string
type SF map[string]float64
type II map[int64]int64
type IB map[int64]bool
type SI map[string]int64
type IS map[int64]string
type SB map[string]bool

// 包A
type X []interface{}
type MSX []map[string]interface{}

所以我可以这样声明:

// 导入gitlab.com/kokizzu/gokil/A
// 导入gitlab.com/kokizzu/gokil/M
values := M.SX{
orderId: 1-12-1,
apiKey: 16313c061a8e3288528123bd8,
country: 360,
currency: 360,
payType: 1,
items: A.MSX{
M.SX{
code: subscription for 7 days,
name: Bla bla,
price: price,
},
},
profile: M.SX{
entry: A.MSX{
M.SX{
key: need_mno_id,
value: yes,
},
M.SX{
key: foo,
value: bar,
},
},
},
profile: A.MSX{
M.SX{foo: bar, age: 123},
M.SX{foo: wow, age: 234, currency: 360},
M.SX{foo: such, age: 45, is_admin: true},
M.SX{foo: wow, age: 57, is_deleted: true},
},
}

除了encoding/gobencoding/json之外,还有哪些支持这种序列化方式(无需生成结构/模式)的库?

github.com/alecthomas/binary
github.com/davecgh/go-xdr/xdr
github.com/Sereal/Sereal/Go/sereal
github.com/ugorji/go/codec
gopkg.in/vmihailenco/msgpack.v2 --> 有一个示例用于编码/解码地图
labix.org/v2/mgo/bson
github.com/tinylib/msgp(用于msgpack的代码生成器)
github.com/golang/protobuf(生成的代码)
github.com/gogo/protobuf(生成的代码,goprotobuf的优化版本)
github.com/DeDiS/protobuf(基于反射)
github.com/google/flatbuffers
github.com/hprose/hprose-go/io
github.com/glycerine/go-capnproto
zombiezen.com/go/capnproto2
github.com/andyleap/gencode
github.com/pascaldekloe/colfer

注意:Gob没有问题(我目前在使用它),我只是需要为当Gob不再足够(不够快/小)时准备替代方案(或从最佳方案开始),因为我将其用于在RAM上缓存数据库(具有不断变化的模式)的查询结果。

英文:

I do lot of queries resulting in map or slice/array of map, something like this:

// package M
type SX map[string]interface{}
type IX map[int64]interface{}
type IAX map[int64][]interface{}
type SAX map[string][]interface{}
type SS map[string]string
type SF map[string]float64
type II map[int64]int64
type IB map[int64]bool
type SI map[string]int64
type IS map[int64]string
type SB map[string]bool

// package A
type X []interface{}
type MSX []map[string]interface{}

So I could declare it something like this:

 // import `gitlab.com/kokizzu/gokil/A`
 // import `gitlab.com/kokizzu/gokil/M`
values := M.SX{
	`orderId`:  `1-12-1`,
	`apiKey`:   `16313c061a8e3288528123bd8`,
	`country`:  `360`, 
	`currency`: `360`, 
	`payType`:  1,
	`items`: A.MSX{
		M.SX{
			`code`:  `subscription for 7 days`,
			`name`:  `Bla bla`,
			`price`: price,
		},
	},
	`profile`: M.SX{
		`entry`: A.MSX{
			M.SX{
				`key`:   `need_mno_id`,
				`value`: `yes`,
			},
			M.SX{
				`key`:   `foo`,
				`value`: `bar`,
			},
		},
	},
	`profile`: A.MSX{
		M.SX{`foo`:`bar`,`age`:123},
		M.SX{`foo`:`wow`,`age`:234,`currency`:360},
		M.SX{`foo`:`such`,`age`:45,`is_admin`:true},
		M.SX{`foo`:`wow`,`age`:57,`is_deleted`:true},
	},
}

Which one of the list other than encoding/gob and encoding/json, that support this kind of serialization (no need to generate struct/schema)?

github.com/alecthomas/binary
github.com/davecgh/go-xdr/xdr
github.com/Sereal/Sereal/Go/sereal
github.com/ugorji/go/codec
gopkg.in/vmihailenco/msgpack.v2 --> has example for enc/dec-ing a map
labix.org/v2/mgo/bson
github.com/tinylib/msgp (code generator for msgpack)
github.com/golang/protobuf (generated code)
github.com/gogo/protobuf (generated code, optimized version of goprotobuf)
github.com/DeDiS/protobuf (reflection based)
github.com/google/flatbuffers
github.com/hprose/hprose-go/io
github.com/glycerine/go-capnproto
zombiezen.com/go/capnproto2
github.com/andyleap/gencode
github.com/pascaldekloe/colfer

Note: there is nothing wrong with Gob (currently I use them), I just need to prepare for the alternative (or start with the best one) when Gob no longer suffice (not fast/small enough) since I use it to cache the database (with ever changing schema) queries result on RAM.

答案1

得分: 1

你的数据结构看起来可以表示为多个数据表。也就是说,可以将其规范化为表格形式(就像数据库表格一样)。如果是这种情况,考虑使用基于表格的 FlatBuffers 子集。

我刚刚发布了一个 Go 库和实用程序,可以实现这一点。你只需要将你的数据结构重新设计为表格格式,并用一个简单的表格数据格式表示它们,然后安装并运行一个名为 gotflat 的实用程序,它将为你生成所有的接口。以下链接展示了如何安装和使用它。希望对你有帮助。

https://github.com/urban-wombat/gotablesutils/releases/tag/v0.2-alpha

英文:

Your data structures look like they can be represented as several tables of
data. That is, normalised into tables (like with database tables). If that is
the case, then consider using a table-based subset of FlatBuffers.

I have just released a Go library and utility that does that. All you need to
do is redesign your data structures into tabular format and represent them
in a simple table data format, then install and run a utility gotflat which
will generate all the glue for you. The following link shows how to install
and use it. I hope that helps.

https://github.com/urban-wombat/gotablesutils/releases/tag/v0.2-alpha

huangapple
  • 本文由 发表于 2016年11月15日 16:57:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/40605630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定