-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
encode.go
270 lines (231 loc) · 5.23 KB
/
encode.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
package msgpack
import (
"bytes"
"io"
"reflect"
"sync"
"time"
"github.com/vmihailenco/msgpack/v5/msgpcode"
)
const (
sortMapKeysFlag uint32 = 1 << iota
arrayEncodedStructsFlag
useCompactIntsFlag
useCompactFloatsFlag
useInternedStringsFlag
omitEmptyFlag
)
type writer interface {
io.Writer
WriteByte(byte) error
}
type byteWriter struct {
io.Writer
}
func newByteWriter(w io.Writer) byteWriter {
return byteWriter{
Writer: w,
}
}
func (bw byteWriter) WriteByte(c byte) error {
_, err := bw.Write([]byte{c})
return err
}
//------------------------------------------------------------------------------
var encPool = sync.Pool{
New: func() interface{} {
return NewEncoder(nil)
},
}
func GetEncoder() *Encoder {
return encPool.Get().(*Encoder)
}
func PutEncoder(enc *Encoder) {
enc.w = nil
encPool.Put(enc)
}
// Marshal returns the MessagePack encoding of v.
func Marshal(v interface{}) ([]byte, error) {
enc := GetEncoder()
var buf bytes.Buffer
enc.Reset(&buf)
err := enc.Encode(v)
b := buf.Bytes()
PutEncoder(enc)
if err != nil {
return nil, err
}
return b, err
}
type Encoder struct {
w writer
dict map[string]int
structTag string
buf []byte
timeBuf []byte
flags uint32
}
// NewEncoder returns a new encoder that writes to w.
func NewEncoder(w io.Writer) *Encoder {
e := &Encoder{
buf: make([]byte, 9),
}
e.Reset(w)
return e
}
// Writer returns the Encoder's writer.
func (e *Encoder) Writer() io.Writer {
return e.w
}
// Reset discards any buffered data, resets all state, and switches the writer to write to w.
func (e *Encoder) Reset(w io.Writer) {
e.ResetDict(w, nil)
}
// ResetDict is like Reset, but also resets the dict.
func (e *Encoder) ResetDict(w io.Writer, dict map[string]int) {
e.ResetWriter(w)
e.flags = 0
e.structTag = ""
e.dict = dict
}
func (e *Encoder) WithDict(dict map[string]int, fn func(*Encoder) error) error {
oldDict := e.dict
e.dict = dict
err := fn(e)
e.dict = oldDict
return err
}
func (e *Encoder) ResetWriter(w io.Writer) {
e.dict = nil
if bw, ok := w.(writer); ok {
e.w = bw
} else if w == nil {
e.w = nil
} else {
e.w = newByteWriter(w)
}
}
// SetSortMapKeys causes the Encoder to encode map keys in increasing order.
// Supported map types are:
// - map[string]string
// - map[string]bool
// - map[string]interface{}
func (e *Encoder) SetSortMapKeys(on bool) *Encoder {
if on {
e.flags |= sortMapKeysFlag
} else {
e.flags &= ^sortMapKeysFlag
}
return e
}
// SetCustomStructTag causes the Encoder to use a custom struct tag as
// fallback option if there is no msgpack tag.
func (e *Encoder) SetCustomStructTag(tag string) {
e.structTag = tag
}
// SetOmitEmpty causes the Encoder to omit empty values by default.
func (e *Encoder) SetOmitEmpty(on bool) {
if on {
e.flags |= omitEmptyFlag
} else {
e.flags &= ^omitEmptyFlag
}
}
// UseArrayEncodedStructs causes the Encoder to encode Go structs as msgpack arrays.
func (e *Encoder) UseArrayEncodedStructs(on bool) {
if on {
e.flags |= arrayEncodedStructsFlag
} else {
e.flags &= ^arrayEncodedStructsFlag
}
}
// UseCompactEncoding causes the Encoder to chose the most compact encoding.
// For example, it allows to encode small Go int64 as msgpack int8 saving 7 bytes.
func (e *Encoder) UseCompactInts(on bool) {
if on {
e.flags |= useCompactIntsFlag
} else {
e.flags &= ^useCompactIntsFlag
}
}
// UseCompactFloats causes the Encoder to chose a compact integer encoding
// for floats that can be represented as integers.
func (e *Encoder) UseCompactFloats(on bool) {
if on {
e.flags |= useCompactFloatsFlag
} else {
e.flags &= ^useCompactFloatsFlag
}
}
// UseInternedStrings causes the Encoder to intern strings.
func (e *Encoder) UseInternedStrings(on bool) {
if on {
e.flags |= useInternedStringsFlag
} else {
e.flags &= ^useInternedStringsFlag
}
}
func (e *Encoder) Encode(v interface{}) error {
switch v := v.(type) {
case nil:
return e.EncodeNil()
case string:
return e.EncodeString(v)
case []byte:
return e.EncodeBytes(v)
case int:
return e.EncodeInt(int64(v))
case int64:
return e.encodeInt64Cond(v)
case uint:
return e.EncodeUint(uint64(v))
case uint64:
return e.encodeUint64Cond(v)
case bool:
return e.EncodeBool(v)
case float32:
return e.EncodeFloat32(v)
case float64:
return e.EncodeFloat64(v)
case time.Duration:
return e.encodeInt64Cond(int64(v))
case time.Time:
return e.EncodeTime(v)
}
return e.EncodeValue(reflect.ValueOf(v))
}
func (e *Encoder) EncodeMulti(v ...interface{}) error {
for _, vv := range v {
if err := e.Encode(vv); err != nil {
return err
}
}
return nil
}
func (e *Encoder) EncodeValue(v reflect.Value) error {
fn := getEncoder(v.Type())
return fn(e, v)
}
func (e *Encoder) EncodeNil() error {
return e.writeCode(msgpcode.Nil)
}
func (e *Encoder) EncodeBool(value bool) error {
if value {
return e.writeCode(msgpcode.True)
}
return e.writeCode(msgpcode.False)
}
func (e *Encoder) EncodeDuration(d time.Duration) error {
return e.EncodeInt(int64(d))
}
func (e *Encoder) writeCode(c byte) error {
return e.w.WriteByte(c)
}
func (e *Encoder) write(b []byte) error {
_, err := e.w.Write(b)
return err
}
func (e *Encoder) writeString(s string) error {
_, err := e.w.Write(stringToBytes(s))
return err
}