-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgpack.go
53 lines (37 loc) · 872 Bytes
/
msgpack.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
package encoding
import (
"bytes"
"github.com/vmihailenco/msgpack/v5"
)
type MsgPackCodec struct {
codec
}
func (c *MsgPackCodec) Marshal(value interface{}) ([]byte, error) {
if primitive, err := c.marshalPrimitive(value); err != errNotPrimitive {
return primitive, err
}
var b bytes.Buffer
enc := msgpack.GetEncoder()
enc.Reset(&b)
enc.UseCompactInts(true)
err := enc.Encode(value)
msgpack.PutEncoder(enc)
if err != nil {
return nil, err
}
return c.compress(b.Bytes())
}
func (c *MsgPackCodec) Unmarshal(byt []byte, ptr interface{}) (err error) {
if err := c.unmarshalPrimitive(byt, ptr); err != errNotPrimitive {
return err
}
if byt, err = c.decompress(byt); err != nil {
return
}
return msgpack.Unmarshal(byt, ptr)
}
func NewMsgPackCodec(compressor Compressor) *MsgPackCodec {
return &MsgPackCodec{
codec{compressor},
}
}