Refactoring: re-organize project intro multiple packages (folders)
This commit is contained in:
54
serializer/json.go
Normal file
54
serializer/json.go
Normal file
@ -0,0 +1,54 @@
|
||||
package serializer
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type JsonSerializer struct {
|
||||
types map[string]reflect.Type
|
||||
}
|
||||
|
||||
func NewJsonSerializer(types ...interface{}) *JsonSerializer {
|
||||
s := &JsonSerializer{make(map[string]reflect.Type)}
|
||||
for _, t := range types {
|
||||
s.RegisterType(t)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (me *JsonSerializer) RegisterType(t interface{}) {
|
||||
type_ := reflect.TypeOf(t)
|
||||
if type_.Kind() == reflect.Ptr || type_.Kind() == reflect.Interface {
|
||||
type_ = type_.Elem()
|
||||
}
|
||||
me.types[type_.String()] = type_
|
||||
}
|
||||
|
||||
func (me *JsonSerializer) Serialize(obj interface{}) ([]byte, string, error) {
|
||||
type_ := reflect.TypeOf(obj)
|
||||
if (type_.Kind() == reflect.Interface || type_.Kind() == reflect.Ptr) {
|
||||
return nil, "", errors.New("Trying to serialize a Ptr type.")
|
||||
}
|
||||
typeId := type_.String()
|
||||
data, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return data, typeId, nil
|
||||
}
|
||||
|
||||
func (me *JsonSerializer) Deserialize(serialized []byte, typeId string) (interface{}, error) {
|
||||
type_ := me.types[typeId]
|
||||
if type_ == nil {
|
||||
return nil, errors.New("type not registered in serializer")
|
||||
}
|
||||
objPtr := reflect.New(type_).Interface()
|
||||
err := json.Unmarshal(serialized, objPtr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
obj := reflect.Indirect(reflect.ValueOf(objPtr)).Interface()
|
||||
return obj, nil
|
||||
}
|
38
serializer/passthru.go
Normal file
38
serializer/passthru.go
Normal file
@ -0,0 +1,38 @@
|
||||
package serializer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type PassthruSerializer struct {}
|
||||
|
||||
func NewPassthruSerializer() *PassthruSerializer {
|
||||
return &PassthruSerializer{}
|
||||
}
|
||||
|
||||
func (me PassthruSerializer) Serialize(input interface{}) (output []byte, typeId string, err error) {
|
||||
content, ok := input.([]byte)
|
||||
if !ok {
|
||||
err = errors.New("input should be []byte")
|
||||
return
|
||||
}
|
||||
|
||||
sep := bytes.IndexByte(content, ' ')
|
||||
if sep == -1 {
|
||||
err = errors.New("missing split char.")
|
||||
return
|
||||
}
|
||||
|
||||
output = content[sep+1:]
|
||||
typeId = string(content[0:sep])
|
||||
return
|
||||
}
|
||||
|
||||
func (me PassthruSerializer) Deserialize(input []byte, typeId string) (interface{}, error) {
|
||||
output := []byte(typeId)
|
||||
output = append(output, ' ')
|
||||
output = append(output, input...)
|
||||
|
||||
return output, nil
|
||||
}
|
6
serializer/serializer.go
Normal file
6
serializer/serializer.go
Normal file
@ -0,0 +1,6 @@
|
||||
package serializer
|
||||
|
||||
type Serializer interface {
|
||||
Serialize(interface{}) ([]byte, string, error)
|
||||
Deserialize([]byte, string) (interface{}, error)
|
||||
}
|
Reference in New Issue
Block a user