-
Notifications
You must be signed in to change notification settings - Fork 2
/
decode.go
37 lines (30 loc) · 822 Bytes
/
decode.go
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
32
33
34
35
36
37
package bytecodec
import "reflect"
// An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
// (The argument to Unmarshal must be a non-nil pointer.)
type InvalidUnmarshalError struct {
Type reflect.Type
}
func (e *InvalidUnmarshalError) Error() string {
if e.Type == nil {
return "bytecodec: Unmarshal(nil)"
}
if e.Type.Kind() != reflect.Ptr {
return "bytecodec: Unmarshal(non-pointer " + e.Type.String() + ")"
}
return "bytecodec: Unmarshal(nil " + e.Type.String() + ")"
}
func Unmarshal(data []byte, v interface{}) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return &InvalidUnmarshalError{reflect.TypeOf(v)}
}
d := newCodecState()
d.Write(data)
err := d.unmarshal(rv)
if err != nil {
return err
}
encodeStatePool.Put(d)
return nil
}