From b8d55c6a74af18f7cfda85dc2532d87e9bdc99ce Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Tue, 6 Sep 2016 12:41:03 +0100 Subject: [PATCH] Add utility package for data encoding and decoding --- util/pack/pack.go | 116 +++++++++++++++++++++++++++++++++++++++++ util/pack/sql-all.go | 25 +++++++++ util/pack/sql-asc.go | 25 +++++++++ util/pack/sql-desc.go | 25 +++++++++ util/pack/sql-empty.go | 25 +++++++++ util/pack/sql-field.go | 44 ++++++++++++++++ util/pack/sql-group.go | 44 ++++++++++++++++ util/pack/sql-ident.go | 34 ++++++++++++ util/pack/sql-null.go | 25 +++++++++ util/pack/sql-table.go | 34 ++++++++++++ util/pack/sql-thing.go | 44 ++++++++++++++++ util/pack/sql-void.go | 25 +++++++++ util/pack/time.go | 35 +++++++++++++ 13 files changed, 501 insertions(+) create mode 100644 util/pack/pack.go create mode 100644 util/pack/sql-all.go create mode 100644 util/pack/sql-asc.go create mode 100644 util/pack/sql-desc.go create mode 100644 util/pack/sql-empty.go create mode 100644 util/pack/sql-field.go create mode 100644 util/pack/sql-group.go create mode 100644 util/pack/sql-ident.go create mode 100644 util/pack/sql-null.go create mode 100644 util/pack/sql-table.go create mode 100644 util/pack/sql-thing.go create mode 100644 util/pack/sql-void.go create mode 100644 util/pack/time.go diff --git a/util/pack/pack.go b/util/pack/pack.go new file mode 100644 index 00000000..287eb909 --- /dev/null +++ b/util/pack/pack.go @@ -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 + +} diff --git a/util/pack/sql-all.go b/util/pack/sql-all.go new file mode 100644 index 00000000..83a5b8d6 --- /dev/null +++ b/util/pack/sql-all.go @@ -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("*") +} diff --git a/util/pack/sql-asc.go b/util/pack/sql-asc.go new file mode 100644 index 00000000..84a05c22 --- /dev/null +++ b/util/pack/sql-asc.go @@ -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") +} diff --git a/util/pack/sql-desc.go b/util/pack/sql-desc.go new file mode 100644 index 00000000..dd97024e --- /dev/null +++ b/util/pack/sql-desc.go @@ -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") +} diff --git a/util/pack/sql-empty.go b/util/pack/sql-empty.go new file mode 100644 index 00000000..6d15688b --- /dev/null +++ b/util/pack/sql-empty.go @@ -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") +} diff --git a/util/pack/sql-field.go b/util/pack/sql-field.go new file mode 100644 index 00000000..b00c0328 --- /dev/null +++ b/util/pack/sql-field.go @@ -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() +} diff --git a/util/pack/sql-group.go b/util/pack/sql-group.go new file mode 100644 index 00000000..523b8adc --- /dev/null +++ b/util/pack/sql-group.go @@ -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() +} diff --git a/util/pack/sql-ident.go b/util/pack/sql-ident.go new file mode 100644 index 00000000..e532ea3a --- /dev/null +++ b/util/pack/sql-ident.go @@ -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 +} diff --git a/util/pack/sql-null.go b/util/pack/sql-null.go new file mode 100644 index 00000000..759edad6 --- /dev/null +++ b/util/pack/sql-null.go @@ -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") +} diff --git a/util/pack/sql-table.go b/util/pack/sql-table.go new file mode 100644 index 00000000..9c19d73e --- /dev/null +++ b/util/pack/sql-table.go @@ -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 +} diff --git a/util/pack/sql-thing.go b/util/pack/sql-thing.go new file mode 100644 index 00000000..69d1c889 --- /dev/null +++ b/util/pack/sql-thing.go @@ -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() +} diff --git a/util/pack/sql-void.go b/util/pack/sql-void.go new file mode 100644 index 00000000..d688be2f --- /dev/null +++ b/util/pack/sql-void.go @@ -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") +} diff --git a/util/pack/time.go b/util/pack/time.go new file mode 100644 index 00000000..e40dab3d --- /dev/null +++ b/util/pack/time.go @@ -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 +}