142 lines
3.6 KiB
Go
142 lines
3.6 KiB
Go
package codec
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"time"
|
|
)
|
|
|
|
//解码器
|
|
type Decoder struct {
|
|
DefDecoder func(buf []byte, v interface{}) error //默认编码 扩展自定义编码方式
|
|
}
|
|
|
|
func (this *Decoder) Decoder(buf []byte, v interface{}) error {
|
|
switch v := v.(type) {
|
|
case nil:
|
|
return fmt.Errorf("decoder: Decoder(nil)")
|
|
case *string:
|
|
*v = BytesToString(buf)
|
|
return nil
|
|
case *[]byte:
|
|
*v = buf
|
|
return nil
|
|
case *int:
|
|
*v = BytesToInt(buf)
|
|
return nil
|
|
case *int8:
|
|
*v = BytesToInt8(buf)
|
|
return nil
|
|
case *int16:
|
|
*v = BytesToInt16(buf)
|
|
return nil
|
|
case *int32:
|
|
*v = BytesToInt32(buf)
|
|
return nil
|
|
case *int64:
|
|
*v = BytesToInt64(buf)
|
|
return nil
|
|
case *uint:
|
|
*v = BytesToUInt(buf)
|
|
return nil
|
|
case *uint8:
|
|
*v = BytesToUInt8(buf)
|
|
return nil
|
|
case *uint16:
|
|
*v = BytesToUInt16(buf)
|
|
return nil
|
|
case *uint32:
|
|
*v = BytesToUInt32(buf)
|
|
return nil
|
|
case *uint64:
|
|
*v = BytesToUInt64(buf)
|
|
return nil
|
|
case *float32:
|
|
*v = BytesToFloat32(buf)
|
|
return nil
|
|
case *float64:
|
|
*v = BytesToFloat64(buf)
|
|
return nil
|
|
case *bool:
|
|
*v = BytesToBool(buf)
|
|
return nil
|
|
case *time.Time:
|
|
var err error
|
|
*v, err = time.Parse(time.RFC3339Nano, BytesToString(buf))
|
|
return err
|
|
case *time.Duration:
|
|
*v = time.Duration(BytesToInt64(buf))
|
|
return nil
|
|
default:
|
|
if this.DefDecoder != nil {
|
|
return this.DefDecoder(buf, v)
|
|
} else {
|
|
return fmt.Errorf(
|
|
"decoder: can't marshal %T (implement decoder.DefDecoder)", v)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *Decoder) DecoderMap(data map[string]string, v interface{}) error {
|
|
vof := reflect.ValueOf(v)
|
|
if !vof.IsValid() {
|
|
return fmt.Errorf("Decoder: DecoderMap(nil)")
|
|
}
|
|
if vof.Kind() != reflect.Ptr && vof.Kind() != reflect.Map && vof.Kind() != reflect.Slice {
|
|
return fmt.Errorf("Decoder: DecoderMap(non-pointer %T)", v)
|
|
}
|
|
if vof.Kind() == reflect.Map {
|
|
kt, vt := vof.Type().Key(), vof.Type().Elem()
|
|
for k, v := range data {
|
|
key := reflect.New(kt).Elem()
|
|
if key.Kind() != reflect.Ptr {
|
|
if err := this.Decoder(StringToBytes(k), key.Addr().Interface()); err != nil {
|
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", key, err)
|
|
}
|
|
} else {
|
|
if err := this.Decoder(StringToBytes(k), key.Addr()); err != nil {
|
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", key, err)
|
|
}
|
|
}
|
|
value := reflect.New(vt).Elem()
|
|
if value.Kind() != reflect.Ptr {
|
|
if err := this.Decoder(StringToBytes(v), value.Addr().Interface()); err != nil {
|
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
|
}
|
|
} else {
|
|
value.Interface()
|
|
if err := this.Decoder(StringToBytes(v), value.Addr().Interface()); err != nil {
|
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
|
}
|
|
}
|
|
vof.SetMapIndex(key, value)
|
|
}
|
|
} else if vof.Kind() == reflect.Ptr {
|
|
elem := vof.Elem()
|
|
relType := elem.Type()
|
|
for i := 0; i < relType.NumField(); i++ {
|
|
fieldInfo := relType.Field(i)
|
|
tag := fieldInfo.Tag
|
|
name := tag.Get("json")
|
|
if len(name) == 0 {
|
|
name = fieldInfo.Name
|
|
}
|
|
if value, ok := data[name]; ok {
|
|
v := reflect.New(fieldInfo.Type).Elem()
|
|
if fieldInfo.Type.Kind() != reflect.Ptr {
|
|
if err := this.Decoder(StringToBytes(value), v.Addr().Interface()); err != nil {
|
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
|
}
|
|
elem.FieldByName(fieldInfo.Name).Set(v)
|
|
} else {
|
|
if err := this.Decoder(StringToBytes(value), v); err != nil {
|
|
return fmt.Errorf("Decoder: Decoder(non-pointer %T) err:%v", value, err)
|
|
}
|
|
elem.FieldByName(fieldInfo.Name).Set(v)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|