-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
ext.go
303 lines (266 loc) · 6.13 KB
/
ext.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package msgpack
import (
"fmt"
"math"
"reflect"
"github.com/vmihailenco/msgpack/v5/msgpcode"
)
type extInfo struct {
Type reflect.Type
Decoder func(d *Decoder, v reflect.Value, extLen int) error
}
var extTypes = make(map[int8]*extInfo)
type MarshalerUnmarshaler interface {
Marshaler
Unmarshaler
}
func RegisterExt(extID int8, value MarshalerUnmarshaler) {
RegisterExtEncoder(extID, value, func(e *Encoder, v reflect.Value) ([]byte, error) {
marshaler := v.Interface().(Marshaler)
return marshaler.MarshalMsgpack()
})
RegisterExtDecoder(extID, value, func(d *Decoder, v reflect.Value, extLen int) error {
b, err := d.readN(extLen)
if err != nil {
return err
}
return v.Interface().(Unmarshaler).UnmarshalMsgpack(b)
})
}
func UnregisterExt(extID int8) {
unregisterExtEncoder(extID)
unregisterExtDecoder(extID)
}
func RegisterExtEncoder(
extID int8,
value interface{},
encoder func(enc *Encoder, v reflect.Value) ([]byte, error),
) {
unregisterExtEncoder(extID)
typ := reflect.TypeOf(value)
extEncoder := makeExtEncoder(extID, typ, encoder)
typeEncMap.Store(extID, typ)
typeEncMap.Store(typ, extEncoder)
if typ.Kind() == reflect.Ptr {
typeEncMap.Store(typ.Elem(), makeExtEncoderAddr(extEncoder))
}
}
func unregisterExtEncoder(extID int8) {
t, ok := typeEncMap.Load(extID)
if !ok {
return
}
typeEncMap.Delete(extID)
typ := t.(reflect.Type)
typeEncMap.Delete(typ)
if typ.Kind() == reflect.Ptr {
typeEncMap.Delete(typ.Elem())
}
}
func makeExtEncoder(
extID int8,
typ reflect.Type,
encoder func(enc *Encoder, v reflect.Value) ([]byte, error),
) encoderFunc {
nilable := typ.Kind() == reflect.Ptr
return func(e *Encoder, v reflect.Value) error {
if nilable && v.IsNil() {
return e.EncodeNil()
}
b, err := encoder(e, v)
if err != nil {
return err
}
if err := e.EncodeExtHeader(extID, len(b)); err != nil {
return err
}
return e.write(b)
}
}
func makeExtEncoderAddr(extEncoder encoderFunc) encoderFunc {
return func(e *Encoder, v reflect.Value) error {
if !v.CanAddr() {
return fmt.Errorf("msgpack: EncodeExt(nonaddressable %T)", v.Interface())
}
return extEncoder(e, v.Addr())
}
}
func RegisterExtDecoder(
extID int8,
value interface{},
decoder func(dec *Decoder, v reflect.Value, extLen int) error,
) {
unregisterExtDecoder(extID)
typ := reflect.TypeOf(value)
extDecoder := makeExtDecoder(extID, typ, decoder)
extTypes[extID] = &extInfo{
Type: typ,
Decoder: decoder,
}
typeDecMap.Store(extID, typ)
typeDecMap.Store(typ, extDecoder)
if typ.Kind() == reflect.Ptr {
typeDecMap.Store(typ.Elem(), makeExtDecoderAddr(extDecoder))
}
}
func unregisterExtDecoder(extID int8) {
t, ok := typeDecMap.Load(extID)
if !ok {
return
}
typeDecMap.Delete(extID)
delete(extTypes, extID)
typ := t.(reflect.Type)
typeDecMap.Delete(typ)
if typ.Kind() == reflect.Ptr {
typeDecMap.Delete(typ.Elem())
}
}
func makeExtDecoder(
wantedExtID int8,
typ reflect.Type,
decoder func(d *Decoder, v reflect.Value, extLen int) error,
) decoderFunc {
return nilAwareDecoder(typ, func(d *Decoder, v reflect.Value) error {
extID, extLen, err := d.DecodeExtHeader()
if err != nil {
return err
}
if extID != wantedExtID {
return fmt.Errorf("msgpack: got ext type=%d, wanted %d", extID, wantedExtID)
}
return decoder(d, v, extLen)
})
}
func makeExtDecoderAddr(extDecoder decoderFunc) decoderFunc {
return func(d *Decoder, v reflect.Value) error {
if !v.CanAddr() {
return fmt.Errorf("msgpack: DecodeExt(nonaddressable %T)", v.Interface())
}
return extDecoder(d, v.Addr())
}
}
func (e *Encoder) EncodeExtHeader(extID int8, extLen int) error {
if err := e.encodeExtLen(extLen); err != nil {
return err
}
if err := e.w.WriteByte(byte(extID)); err != nil {
return err
}
return nil
}
func (e *Encoder) encodeExtLen(l int) error {
switch l {
case 1:
return e.writeCode(msgpcode.FixExt1)
case 2:
return e.writeCode(msgpcode.FixExt2)
case 4:
return e.writeCode(msgpcode.FixExt4)
case 8:
return e.writeCode(msgpcode.FixExt8)
case 16:
return e.writeCode(msgpcode.FixExt16)
}
if l <= math.MaxUint8 {
return e.write1(msgpcode.Ext8, uint8(l))
}
if l <= math.MaxUint16 {
return e.write2(msgpcode.Ext16, uint16(l))
}
return e.write4(msgpcode.Ext32, uint32(l))
}
func (d *Decoder) DecodeExtHeader() (extID int8, extLen int, err error) {
c, err := d.readCode()
if err != nil {
return
}
return d.extHeader(c)
}
func (d *Decoder) extHeader(c byte) (int8, int, error) {
extLen, err := d.parseExtLen(c)
if err != nil {
return 0, 0, err
}
extID, err := d.readCode()
if err != nil {
return 0, 0, err
}
return int8(extID), extLen, nil
}
func (d *Decoder) parseExtLen(c byte) (int, error) {
switch c {
case msgpcode.FixExt1:
return 1, nil
case msgpcode.FixExt2:
return 2, nil
case msgpcode.FixExt4:
return 4, nil
case msgpcode.FixExt8:
return 8, nil
case msgpcode.FixExt16:
return 16, nil
case msgpcode.Ext8:
n, err := d.uint8()
return int(n), err
case msgpcode.Ext16:
n, err := d.uint16()
return int(n), err
case msgpcode.Ext32:
n, err := d.uint32()
return int(n), err
default:
return 0, fmt.Errorf("msgpack: invalid code=%x decoding ext len", c)
}
}
func (d *Decoder) decodeInterfaceExt(c byte) (interface{}, error) {
extID, extLen, err := d.extHeader(c)
if err != nil {
return nil, err
}
info, ok := extTypes[extID]
if !ok {
return nil, fmt.Errorf("msgpack: unknown ext id=%d", extID)
}
v := d.newValue(info.Type).Elem()
if nilable(v.Kind()) && v.IsNil() {
v.Set(d.newValue(info.Type.Elem()))
}
if err := info.Decoder(d, v, extLen); err != nil {
return nil, err
}
return v.Interface(), nil
}
func (d *Decoder) skipExt(c byte) error {
n, err := d.parseExtLen(c)
if err != nil {
return err
}
return d.skipN(n + 1)
}
func (d *Decoder) skipExtHeader(c byte) error {
// Read ext type.
_, err := d.readCode()
if err != nil {
return err
}
// Read ext body len.
for i := 0; i < extHeaderLen(c); i++ {
_, err := d.readCode()
if err != nil {
return err
}
}
return nil
}
func extHeaderLen(c byte) int {
switch c {
case msgpcode.Ext8:
return 1
case msgpcode.Ext16:
return 2
case msgpcode.Ext32:
return 4
}
return 0
}