Simplify pack data encode/decode package

This commit is contained in:
Tobie Morgan Hitchcock 2016-09-14 22:25:18 +01:00
parent fbc94f90e5
commit 218ce8ad82

View file

@ -15,102 +15,127 @@
package pack package pack
import ( import (
"bytes"
"reflect" "reflect"
"time" "time"
"encoding/gob"
"github.com/ugorji/go/codec" "github.com/ugorji/go/codec"
"github.com/abcum/surreal/sql" "github.com/abcum/surreal/sql"
) )
var jh codec.JsonHandle
var ch codec.CborHandle
var bh codec.BincHandle
var mh codec.MsgpackHandle
func init() { func init() {
gob.Register(time.Time{})
gob.Register([]interface{}{}) // JSONHandle
gob.Register(map[string]interface{}{})
jh.Canonical = true
jh.CheckCircularRef = false
jh.AsSymbols = codec.AsSymbolDefault
jh.SliceType = reflect.TypeOf([]interface{}(nil))
jh.MapType = reflect.TypeOf(map[string]interface{}(nil))
// CBORHandle
ch.Canonical = true
ch.CheckCircularRef = false
ch.AsSymbols = codec.AsSymbolDefault
ch.SliceType = reflect.TypeOf([]interface{}(nil))
ch.MapType = reflect.TypeOf(map[string]interface{}(nil))
// BINCHandle
bh.Canonical = true
bh.CheckCircularRef = false
bh.AsSymbols = codec.AsSymbolDefault
bh.SliceType = reflect.TypeOf([]interface{}(nil))
bh.MapType = reflect.TypeOf(map[string]interface{}(nil))
bh.SetBytesExt(reflect.TypeOf(time.Time{}), 1, extTime{})
bh.SetBytesExt(reflect.TypeOf(sql.All{}), 2, extSqlAll{})
bh.SetBytesExt(reflect.TypeOf(sql.Asc{}), 3, extSqlAsc{})
bh.SetBytesExt(reflect.TypeOf(sql.Desc{}), 4, extSqlDesc{})
bh.SetBytesExt(reflect.TypeOf(sql.Null{}), 5, extSqlNull{})
bh.SetBytesExt(reflect.TypeOf(sql.Void{}), 6, extSqlVoid{})
bh.SetBytesExt(reflect.TypeOf(sql.Empty{}), 7, extSqlEmpty{})
bh.SetBytesExt(reflect.TypeOf(sql.Ident{}), 8, extSqlIdent{})
bh.SetBytesExt(reflect.TypeOf(sql.Table{}), 9, extSqlTable{})
bh.SetBytesExt(reflect.TypeOf(sql.Thing{}), 10, extSqlThing{})
bh.SetBytesExt(reflect.TypeOf(sql.Field{}), 11, extSqlField{})
bh.SetBytesExt(reflect.TypeOf(sql.Group{}), 12, extSqlGroup{})
// PACKHandle
mh.WriteExt = true
mh.Canonical = true
mh.RawToString = true
mh.CheckCircularRef = false
mh.AsSymbols = codec.AsSymbolDefault
mh.SliceType = reflect.TypeOf([]interface{}(nil))
mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
mh.SetBytesExt(reflect.TypeOf(time.Time{}), 1, extTime{})
mh.SetBytesExt(reflect.TypeOf(sql.All{}), 2, extSqlAll{})
mh.SetBytesExt(reflect.TypeOf(sql.Asc{}), 3, extSqlAsc{})
mh.SetBytesExt(reflect.TypeOf(sql.Desc{}), 4, extSqlDesc{})
mh.SetBytesExt(reflect.TypeOf(sql.Null{}), 5, extSqlNull{})
mh.SetBytesExt(reflect.TypeOf(sql.Void{}), 6, extSqlVoid{})
mh.SetBytesExt(reflect.TypeOf(sql.Empty{}), 7, extSqlEmpty{})
mh.SetBytesExt(reflect.TypeOf(sql.Ident{}), 8, extSqlIdent{})
mh.SetBytesExt(reflect.TypeOf(sql.Table{}), 9, extSqlTable{})
mh.SetBytesExt(reflect.TypeOf(sql.Thing{}), 10, extSqlThing{})
mh.SetBytesExt(reflect.TypeOf(sql.Field{}), 11, extSqlField{})
mh.SetBytesExt(reflect.TypeOf(sql.Group{}), 12, extSqlGroup{})
} }
func Copy(src interface{}) (dst map[string]interface{}) { // ToJSON encodes a data object to a JSON byte slice.
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
dec := gob.NewDecoder(&buf)
enc.Encode(src)
dec.Decode(&dst)
return
}
// ToJSON converts the data object to a JSON byte slice.
func ToJSON(src interface{}) (dst []byte) { func ToJSON(src interface{}) (dst []byte) {
codec.NewEncoderBytes(&dst, &jh).Encode(src)
var opt codec.JsonHandle
opt.Canonical = true
opt.CheckCircularRef = false
opt.AsSymbols = codec.AsSymbolDefault
codec.NewEncoderBytes(&dst, &opt).Encode(src)
return return
} }
// Encode encodes the data object to a byte slice. // FromJSON decodes a JSON byte slice into a data object.
func FromJSON(src []byte, dst interface{}) {
codec.NewDecoderBytes(src, &jh).Decode(dst)
return
}
// ToCBOR encodes a data object to a CBOR byte slice.
func ToCBOR(src interface{}) (dst []byte) {
codec.NewEncoderBytes(&dst, &ch).Encode(src)
return
}
// FromCBOR decodes a CBOR byte slice into a data object.
func FromCBOR(src []byte, dst interface{}) {
codec.NewDecoderBytes(src, &ch).Decode(dst)
return
}
// ToBINC encodes a data object to a BINC byte slice.
func ToBINC(src interface{}) (dst []byte) {
codec.NewEncoderBytes(&dst, &bh).Encode(src)
return
}
// FromBINC decodes a BINC byte slice into a data object.
func FromBINC(src []byte, dst interface{}) {
codec.NewDecoderBytes(src, &bh).Decode(dst)
return
}
// ToPACK encodes a data object to a MsgPack byte slice.
func ToPACK(src interface{}) (dst []byte) { func ToPACK(src interface{}) (dst []byte) {
codec.NewEncoderBytes(&dst, &mh).Encode(src)
var opt codec.MsgpackHandle
opt.WriteExt = true
opt.Canonical = true
opt.RawToString = true
opt.CheckCircularRef = false
opt.AsSymbols = codec.AsSymbolDefault
opt.MapType = reflect.TypeOf(map[string]interface{}(nil))
opt.SetBytesExt(reflect.TypeOf(time.Time{}), 1, extTime{})
opt.SetBytesExt(reflect.TypeOf(sql.All{}), 101, extSqlAll{})
opt.SetBytesExt(reflect.TypeOf(sql.Asc{}), 102, extSqlAsc{})
opt.SetBytesExt(reflect.TypeOf(sql.Desc{}), 103, extSqlDesc{})
opt.SetBytesExt(reflect.TypeOf(sql.Null{}), 104, extSqlNull{})
opt.SetBytesExt(reflect.TypeOf(sql.Void{}), 105, extSqlVoid{})
opt.SetBytesExt(reflect.TypeOf(sql.Empty{}), 106, extSqlEmpty{})
opt.SetBytesExt(reflect.TypeOf(sql.Ident{}), 107, extSqlIdent{})
opt.SetBytesExt(reflect.TypeOf(sql.Table{}), 108, extSqlTable{})
opt.SetBytesExt(reflect.TypeOf(sql.Thing{}), 109, extSqlThing{})
opt.SetBytesExt(reflect.TypeOf(sql.Field{}), 110, extSqlField{})
opt.SetBytesExt(reflect.TypeOf(sql.Group{}), 111, extSqlGroup{})
codec.NewEncoderBytes(&dst, &opt).Encode(src)
return return
} }
// Decode decodes the byte slice into a data object. // FromPACK decodes a MsgPack byte slice into a data object.
func FromPACK(src []byte, dst interface{}) { func FromPACK(src []byte, dst interface{}) {
codec.NewDecoderBytes(src, &mh).Decode(dst)
var opt codec.MsgpackHandle
opt.WriteExt = true
opt.Canonical = true
opt.RawToString = true
opt.CheckCircularRef = false
opt.AsSymbols = codec.AsSymbolDefault
opt.MapType = reflect.TypeOf(map[string]interface{}(nil))
opt.SetBytesExt(reflect.TypeOf(time.Time{}), 1, extTime{})
opt.SetBytesExt(reflect.TypeOf(sql.All{}), 101, extSqlAll{})
opt.SetBytesExt(reflect.TypeOf(sql.Asc{}), 102, extSqlAsc{})
opt.SetBytesExt(reflect.TypeOf(sql.Desc{}), 103, extSqlDesc{})
opt.SetBytesExt(reflect.TypeOf(sql.Null{}), 104, extSqlNull{})
opt.SetBytesExt(reflect.TypeOf(sql.Void{}), 105, extSqlVoid{})
opt.SetBytesExt(reflect.TypeOf(sql.Empty{}), 106, extSqlEmpty{})
opt.SetBytesExt(reflect.TypeOf(sql.Ident{}), 107, extSqlIdent{})
opt.SetBytesExt(reflect.TypeOf(sql.Table{}), 108, extSqlTable{})
opt.SetBytesExt(reflect.TypeOf(sql.Thing{}), 109, extSqlThing{})
opt.SetBytesExt(reflect.TypeOf(sql.Field{}), 110, extSqlField{})
opt.SetBytesExt(reflect.TypeOf(sql.Group{}), 111, extSqlGroup{})
codec.NewDecoderBytes(src, &opt).Decode(dst)
return return
} }