decoder.go 552 字节
Newer Older
Y
Your Name 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
package response

import "errors"

var (
	ErrorInvalidDecoder = errors.New("invalid decoder")
)
type DecodeHandle func(data []byte, v interface{}) error

type EncodeHandle func (v interface{},org []byte)([]byte,error)
type Encoder interface {
	Encode(v interface{},org []byte)([]byte,error)
	ContentType()string
}

func Decode(data []byte,handle DecodeHandle) (*Response,error) {

	if handle == nil{
		return nil,ErrorInvalidDecoder
	}

	var v interface{}
	err:=handle(data,&v)
	if err!=nil{
		return nil,err
	}
	return &Response{
		Data: v,
	},nil

}