2016-09-06 11:41:03 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package pack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ugorji/go/codec"
|
|
|
|
|
|
|
|
"github.com/abcum/surreal/sql"
|
|
|
|
)
|
|
|
|
|
2016-09-14 21:25:18 +00:00
|
|
|
var jh codec.JsonHandle
|
|
|
|
var ch codec.CborHandle
|
|
|
|
var bh codec.BincHandle
|
|
|
|
var mh codec.MsgpackHandle
|
|
|
|
|
2016-09-06 11:41:03 +00:00
|
|
|
func init() {
|
2016-09-14 21:25:18 +00:00
|
|
|
|
|
|
|
// JSONHandle
|
|
|
|
|
|
|
|
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{})
|
|
|
|
|
2016-09-06 11:41:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 21:25:18 +00:00
|
|
|
// ToJSON encodes a data object to a JSON byte slice.
|
|
|
|
func ToJSON(src interface{}) (dst []byte) {
|
|
|
|
codec.NewEncoderBytes(&dst, &jh).Encode(src)
|
2016-09-06 11:41:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:25:18 +00:00
|
|
|
// FromJSON decodes a JSON byte slice into a data object.
|
|
|
|
func FromJSON(src []byte, dst interface{}) {
|
|
|
|
codec.NewDecoderBytes(src, &jh).Decode(dst)
|
|
|
|
return
|
|
|
|
}
|
2016-09-06 11:41:03 +00:00
|
|
|
|
2016-09-14 21:25:18 +00:00
|
|
|
// ToCBOR encodes a data object to a CBOR byte slice.
|
|
|
|
func ToCBOR(src interface{}) (dst []byte) {
|
|
|
|
codec.NewEncoderBytes(&dst, &ch).Encode(src)
|
|
|
|
return
|
|
|
|
}
|
2016-09-06 11:41:03 +00:00
|
|
|
|
2016-09-14 21:25:18 +00:00
|
|
|
// FromCBOR decodes a CBOR byte slice into a data object.
|
|
|
|
func FromCBOR(src []byte, dst interface{}) {
|
|
|
|
codec.NewDecoderBytes(src, &ch).Decode(dst)
|
|
|
|
return
|
|
|
|
}
|
2016-09-06 11:41:03 +00:00
|
|
|
|
2016-09-14 21:25:18 +00:00
|
|
|
// ToBINC encodes a data object to a BINC byte slice.
|
|
|
|
func ToBINC(src interface{}) (dst []byte) {
|
|
|
|
codec.NewEncoderBytes(&dst, &bh).Encode(src)
|
2016-09-06 11:41:03 +00:00
|
|
|
return
|
2016-09-14 21:25:18 +00:00
|
|
|
}
|
2016-09-06 11:41:03 +00:00
|
|
|
|
2016-09-14 21:25:18 +00:00
|
|
|
// FromBINC decodes a BINC byte slice into a data object.
|
|
|
|
func FromBINC(src []byte, dst interface{}) {
|
|
|
|
codec.NewDecoderBytes(src, &bh).Decode(dst)
|
|
|
|
return
|
2016-09-06 11:41:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 21:25:18 +00:00
|
|
|
// ToPACK encodes a data object to a MsgPack byte slice.
|
2016-09-06 11:41:03 +00:00
|
|
|
func ToPACK(src interface{}) (dst []byte) {
|
2016-09-14 21:25:18 +00:00
|
|
|
codec.NewEncoderBytes(&dst, &mh).Encode(src)
|
2016-09-06 11:41:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:25:18 +00:00
|
|
|
// FromPACK decodes a MsgPack byte slice into a data object.
|
2016-09-06 11:41:03 +00:00
|
|
|
func FromPACK(src []byte, dst interface{}) {
|
2016-09-14 21:25:18 +00:00
|
|
|
codec.NewDecoderBytes(src, &mh).Decode(dst)
|
2016-09-06 11:41:03 +00:00
|
|
|
return
|
|
|
|
}
|