-
Notifications
You must be signed in to change notification settings - Fork 52
/
custom.go
79 lines (67 loc) · 1.5 KB
/
custom.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package proto
import (
"io"
"reflect"
"unsafe"
)
func customCodecOf(t reflect.Type) *codec {
return &codec{
wire: varlen,
size: customSizeFuncOf(t),
encode: customEncodeFuncOf(t),
decode: customDecodeFuncOf(t),
}
}
func customSizeFuncOf(t reflect.Type) sizeFunc {
return func(p unsafe.Pointer, flags flags) int {
if p != nil {
if m := reflect.NewAt(t, p).Interface().(customMessage); m != nil {
size := m.Size()
if flags.has(toplevel) {
return size
}
return sizeOfVarlen(size)
}
}
return 0
}
}
func customEncodeFuncOf(t reflect.Type) encodeFunc {
return func(b []byte, p unsafe.Pointer, flags flags) (int, error) {
if p != nil {
if m := reflect.NewAt(t, p).Interface().(customMessage); m != nil {
size := m.Size()
if flags.has(toplevel) {
if len(b) < size {
return 0, io.ErrShortBuffer
}
return m.MarshalTo(b)
}
vlen := sizeOfVarlen(size)
if len(b) < vlen {
return 0, io.ErrShortBuffer
}
n1, err := encodeVarint(b, uint64(size))
if err != nil {
return n1, err
}
n2, err := m.MarshalTo(b[n1:])
return n1 + n2, err
}
}
return 0, nil
}
}
func customDecodeFuncOf(t reflect.Type) decodeFunc {
return func(b []byte, p unsafe.Pointer, flags flags) (int, error) {
m := reflect.NewAt(t, p).Interface().(customMessage)
if flags.has(toplevel) {
return len(b), m.Unmarshal(b)
}
v, n, err := decodeVarlen(b)
if err != nil {
return n, err
}
return n + len(v), m.Unmarshal(v)
}
}