-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmarshal.go
58 lines (47 loc) · 1.17 KB
/
marshal.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package plenc
import (
"fmt"
"reflect"
"unsafe"
)
func Marshal(data []byte, value interface{}) ([]byte, error) {
return defaultPlenc.Marshal(data, value)
}
func Unmarshal(data []byte, value interface{}) error {
return defaultPlenc.Unmarshal(data, value)
}
func (p *Plenc) Marshal(data []byte, value interface{}) ([]byte, error) {
typ := reflect.TypeOf(value)
ptr := unpackEFace(value).data
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
// When marshalling we don't want a pointer to a map as a map is a
// pointer-ish type itself.
if typ.Kind() == reflect.Map {
ptr = *(*unsafe.Pointer)(ptr)
}
}
c, err := p.CodecForType(typ)
if err != nil {
return nil, err
}
if c.Omit(ptr) {
return nil, nil
}
if data == nil {
data = make([]byte, 0, c.Size(ptr, nil))
}
return c.Append(data, ptr, nil), nil
}
func (p *Plenc) Unmarshal(data []byte, value interface{}) error {
rv := reflect.ValueOf(value)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return fmt.Errorf("you must pass in a non-nil pointer")
}
c, err := p.CodecForType(rv.Type().Elem())
if err != nil {
return err
}
_, err = c.Read(data, unsafe.Pointer(rv.Pointer()), c.WireType())
return err
}