-
Notifications
You must be signed in to change notification settings - Fork 0
/
compat.go
83 lines (73 loc) · 1.51 KB
/
compat.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
package tarantool
import (
"io"
"github.com/vmihailenco/msgpack"
)
const mapElemsAllocLimit = 1e4
func min(a, b int) int {
if a <= b {
return a
}
return b
}
func decodeStringMap(d *msgpack.Decoder) (interface{}, error) {
n, err := d.DecodeMapLen()
if err != nil {
return nil, err
}
if n == -1 {
return nil, nil
}
m := make(map[string]interface{}, min(n, mapElemsAllocLimit))
for i := 0; i < n; i++ {
mk, err := d.DecodeString()
if err != nil {
return nil, err
}
mv, err := d.DecodeInterface()
if err != nil {
return nil, err
}
m[mk] = mv
}
return m, nil
}
func decodeMap(d *msgpack.Decoder) (interface{}, error) {
n, err := d.DecodeMapLen()
if err != nil {
return nil, err
}
if n == -1 {
return nil, nil
}
m := make(map[interface{}]interface{}, min(n, mapElemsAllocLimit))
for i := 0; i < n; i++ {
mk, err := d.DecodeInterface()
if err != nil {
return nil, err
}
mv, err := d.DecodeInterface()
if err != nil {
return nil, err
}
m[mk] = mv
}
return m, nil
}
func newEncoder(w io.Writer) *msgpack.Encoder {
e := msgpack.NewEncoder(w)
// Compat encoding is necessary e.g. for correct unpacking of update
// operations on tarantool side.
//
// Removing this will break things in unbeknownst places.
e.UseCompactEncoding(true)
// Using JSON tags by default, because it is common to duplicate them by
// msgpack tags.
e.UseJSONTag(true)
return e
}
func newDecoder(r io.Reader) *msgpack.Decoder {
d := msgpack.NewDecoder(r)
d.UseJSONTag(true)
return d
}