Add utility package for data encoding and decoding
This commit is contained in:
parent
b5a15a85ae
commit
b8d55c6a74
13 changed files with 501 additions and 0 deletions
116
util/pack/pack.go
Normal file
116
util/pack/pack.go
Normal file
|
@ -0,0 +1,116 @@
|
|||
// 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 (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"encoding/gob"
|
||||
|
||||
"github.com/ugorji/go/codec"
|
||||
|
||||
"github.com/abcum/surreal/sql"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gob.Register(time.Time{})
|
||||
gob.Register([]interface{}{})
|
||||
gob.Register(map[string]interface{}{})
|
||||
}
|
||||
|
||||
func Copy(src interface{}) (dst map[string]interface{}) {
|
||||
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) {
|
||||
|
||||
var opt codec.JsonHandle
|
||||
opt.Canonical = true
|
||||
opt.CheckCircularRef = false
|
||||
opt.AsSymbols = codec.AsSymbolDefault
|
||||
|
||||
codec.NewEncoderBytes(&dst, &opt).Encode(src)
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
// Encode encodes the data object to a byte slice.
|
||||
func ToPACK(src interface{}) (dst []byte) {
|
||||
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
// Decode decodes the byte slice into a data object.
|
||||
func FromPACK(src []byte, dst interface{}) {
|
||||
|
||||
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
|
||||
|
||||
}
|
25
util/pack/sql-all.go
Normal file
25
util/pack/sql-all.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
// 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
|
||||
|
||||
type extSqlAll struct{}
|
||||
|
||||
func (x extSqlAll) ReadExt(dst interface{}, src []byte) {
|
||||
return
|
||||
}
|
||||
|
||||
func (x extSqlAll) WriteExt(src interface{}) (dst []byte) {
|
||||
return []byte("*")
|
||||
}
|
25
util/pack/sql-asc.go
Normal file
25
util/pack/sql-asc.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
// 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
|
||||
|
||||
type extSqlAsc struct{}
|
||||
|
||||
func (x extSqlAsc) ReadExt(dst interface{}, src []byte) {
|
||||
return
|
||||
}
|
||||
|
||||
func (x extSqlAsc) WriteExt(src interface{}) (dst []byte) {
|
||||
return []byte("ASC")
|
||||
}
|
25
util/pack/sql-desc.go
Normal file
25
util/pack/sql-desc.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
// 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
|
||||
|
||||
type extSqlDesc struct{}
|
||||
|
||||
func (x extSqlDesc) ReadExt(dst interface{}, src []byte) {
|
||||
return
|
||||
}
|
||||
|
||||
func (x extSqlDesc) WriteExt(src interface{}) (dst []byte) {
|
||||
return []byte("DESC")
|
||||
}
|
25
util/pack/sql-empty.go
Normal file
25
util/pack/sql-empty.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
// 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
|
||||
|
||||
type extSqlEmpty struct{}
|
||||
|
||||
func (x extSqlEmpty) ReadExt(dst interface{}, src []byte) {
|
||||
return
|
||||
}
|
||||
|
||||
func (x extSqlEmpty) WriteExt(src interface{}) (dst []byte) {
|
||||
return []byte("EMPTY")
|
||||
}
|
44
util/pack/sql-field.go
Normal file
44
util/pack/sql-field.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
// 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 (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
|
||||
"github.com/abcum/surreal/sql"
|
||||
)
|
||||
|
||||
type extSqlField struct{}
|
||||
|
||||
func (x extSqlField) ReadExt(dst interface{}, src []byte) {
|
||||
buf := bytes.NewBuffer(src)
|
||||
dec := gob.NewDecoder(buf)
|
||||
dec.Decode(dst.(*sql.Field))
|
||||
return
|
||||
}
|
||||
|
||||
func (x extSqlField) WriteExt(src interface{}) (dst []byte) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
switch obj := src.(type) {
|
||||
case sql.Field:
|
||||
enc := gob.NewEncoder(buf)
|
||||
enc.Encode(obj)
|
||||
case *sql.Field:
|
||||
enc := gob.NewEncoder(buf)
|
||||
enc.Encode(obj)
|
||||
}
|
||||
return buf.Bytes()
|
||||
}
|
44
util/pack/sql-group.go
Normal file
44
util/pack/sql-group.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
// 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 (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
|
||||
"github.com/abcum/surreal/sql"
|
||||
)
|
||||
|
||||
type extSqlGroup struct{}
|
||||
|
||||
func (x extSqlGroup) ReadExt(dst interface{}, src []byte) {
|
||||
buf := bytes.NewBuffer(src)
|
||||
dec := gob.NewDecoder(buf)
|
||||
dec.Decode(dst.(*sql.Group))
|
||||
return
|
||||
}
|
||||
|
||||
func (x extSqlGroup) WriteExt(src interface{}) (dst []byte) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
switch obj := src.(type) {
|
||||
case sql.Group:
|
||||
enc := gob.NewEncoder(buf)
|
||||
enc.Encode(obj)
|
||||
case *sql.Group:
|
||||
enc := gob.NewEncoder(buf)
|
||||
enc.Encode(obj)
|
||||
}
|
||||
return buf.Bytes()
|
||||
}
|
34
util/pack/sql-ident.go
Normal file
34
util/pack/sql-ident.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
// 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 "github.com/abcum/surreal/sql"
|
||||
|
||||
type extSqlIdent struct{}
|
||||
|
||||
func (x extSqlIdent) ReadExt(dst interface{}, src []byte) {
|
||||
dst.(*sql.Ident).ID = string(src)
|
||||
return
|
||||
}
|
||||
|
||||
func (x extSqlIdent) WriteExt(src interface{}) (dst []byte) {
|
||||
switch obj := src.(type) {
|
||||
case sql.Ident:
|
||||
dst = []byte(obj.ID)
|
||||
case *sql.Ident:
|
||||
dst = []byte(obj.ID)
|
||||
}
|
||||
return
|
||||
}
|
25
util/pack/sql-null.go
Normal file
25
util/pack/sql-null.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
// 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
|
||||
|
||||
type extSqlNull struct{}
|
||||
|
||||
func (x extSqlNull) ReadExt(dst interface{}, src []byte) {
|
||||
return
|
||||
}
|
||||
|
||||
func (x extSqlNull) WriteExt(src interface{}) (dst []byte) {
|
||||
return []byte("NULL")
|
||||
}
|
34
util/pack/sql-table.go
Normal file
34
util/pack/sql-table.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
// 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 "github.com/abcum/surreal/sql"
|
||||
|
||||
type extSqlTable struct{}
|
||||
|
||||
func (x extSqlTable) ReadExt(dst interface{}, src []byte) {
|
||||
dst.(*sql.Table).TB = string(src)
|
||||
return
|
||||
}
|
||||
|
||||
func (x extSqlTable) WriteExt(src interface{}) (dst []byte) {
|
||||
switch obj := src.(type) {
|
||||
case sql.Table:
|
||||
dst = []byte(obj.TB)
|
||||
case *sql.Table:
|
||||
dst = []byte(obj.TB)
|
||||
}
|
||||
return
|
||||
}
|
44
util/pack/sql-thing.go
Normal file
44
util/pack/sql-thing.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
// 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 (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
|
||||
"github.com/abcum/surreal/sql"
|
||||
)
|
||||
|
||||
type extSqlThing struct{}
|
||||
|
||||
func (x extSqlThing) ReadExt(dst interface{}, src []byte) {
|
||||
buf := bytes.NewBuffer(src)
|
||||
dec := gob.NewDecoder(buf)
|
||||
dec.Decode(dst.(*sql.Thing))
|
||||
return
|
||||
}
|
||||
|
||||
func (x extSqlThing) WriteExt(src interface{}) (dst []byte) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
switch obj := src.(type) {
|
||||
case sql.Thing:
|
||||
enc := gob.NewEncoder(buf)
|
||||
enc.Encode(obj)
|
||||
case *sql.Thing:
|
||||
enc := gob.NewEncoder(buf)
|
||||
enc.Encode(obj)
|
||||
}
|
||||
return buf.Bytes()
|
||||
}
|
25
util/pack/sql-void.go
Normal file
25
util/pack/sql-void.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
// 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
|
||||
|
||||
type extSqlVoid struct{}
|
||||
|
||||
func (x extSqlVoid) ReadExt(dst interface{}, src []byte) {
|
||||
return
|
||||
}
|
||||
|
||||
func (x extSqlVoid) WriteExt(src interface{}) (dst []byte) {
|
||||
return []byte("VOID")
|
||||
}
|
35
util/pack/time.go
Normal file
35
util/pack/time.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
// 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 (
|
||||
"time"
|
||||
)
|
||||
|
||||
type extTime struct{}
|
||||
|
||||
func (x extTime) ReadExt(dst interface{}, src []byte) {
|
||||
dst.(*time.Time).UnmarshalBinary(src)
|
||||
}
|
||||
|
||||
func (x extTime) WriteExt(src interface{}) (dst []byte) {
|
||||
switch obj := src.(type) {
|
||||
case time.Time:
|
||||
dst, _ = obj.MarshalBinary()
|
||||
case *time.Time:
|
||||
dst, _ = obj.MarshalBinary()
|
||||
}
|
||||
return
|
||||
}
|
Loading…
Reference in a new issue