67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
|
package cache
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/gob"
|
||
|
"reflect"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
func serialize(value interface{}) ([]byte, error) {
|
||
|
if bytes, ok := value.([]byte); ok {
|
||
|
return bytes, nil
|
||
|
}
|
||
|
|
||
|
switch v := reflect.ValueOf(value); v.Kind() {
|
||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||
|
return []byte(strconv.FormatInt(v.Int(), 10)), nil
|
||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||
|
return []byte(strconv.FormatUint(v.Uint(), 10)), nil
|
||
|
}
|
||
|
|
||
|
var b bytes.Buffer
|
||
|
encoder := gob.NewEncoder(&b)
|
||
|
if err := encoder.Encode(value); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return b.Bytes(), nil
|
||
|
}
|
||
|
|
||
|
func deserialize(byt []byte, ptr interface{}) (err error) {
|
||
|
if bytes, ok := ptr.(*[]byte); ok {
|
||
|
*bytes = byt
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
if v := reflect.ValueOf(ptr); v.Kind() == reflect.Ptr {
|
||
|
switch p := v.Elem(); p.Kind() {
|
||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||
|
var i int64
|
||
|
i, err = strconv.ParseInt(string(byt), 10, 64)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
} else {
|
||
|
p.SetInt(i)
|
||
|
}
|
||
|
return nil
|
||
|
|
||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||
|
var i uint64
|
||
|
i, err = strconv.ParseUint(string(byt), 10, 64)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
} else {
|
||
|
p.SetUint(i)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
b := bytes.NewBuffer(byt)
|
||
|
decoder := gob.NewDecoder(b)
|
||
|
if err = decoder.Decode(ptr); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|