/
decoder.go
200 lines (179 loc) · 4.86 KB
/
decoder.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2020 Datadog, Inc.
package pb
import (
"errors"
"math"
"github.com/tinylib/msgp/msgp"
)
// parseString reads the next type in the msgpack payload and
// converts the BinType or the StrType in a valid string.
func parseString(dc *msgp.Reader) (string, error) {
// read the generic representation type without decoding
t, err := dc.NextType()
if err != nil {
return "", err
}
switch t {
case msgp.BinType:
i, err := dc.ReadBytes(nil)
if err != nil {
return "", err
}
return msgp.UnsafeString(i), nil
case msgp.StrType:
i, err := dc.ReadString()
if err != nil {
return "", err
}
return i, nil
default:
return "", msgp.TypeError{Encoded: t, Method: msgp.StrType}
}
}
// parseFloat64 parses a float64 even if the sent value is an int64 or an uint64;
// this is required because the encoding library could remove bytes from the encoded
// payload to reduce the size, if they're not needed.
func parseFloat64(dc *msgp.Reader) (float64, error) {
// read the generic representation type without decoding
t, err := dc.NextType()
if err != nil {
return 0, err
}
switch t {
case msgp.IntType:
i, err := dc.ReadInt64()
if err != nil {
return 0, err
}
return float64(i), nil
case msgp.UintType:
i, err := dc.ReadUint64()
if err != nil {
return 0, err
}
return float64(i), nil
case msgp.Float64Type:
f, err := dc.ReadFloat64()
if err != nil {
return 0, err
}
return f, nil
default:
return 0, msgp.TypeError{Encoded: t, Method: msgp.Float64Type}
}
}
// cast to int64 values that are int64 but that are sent in uint64
// over the wire. Set to 0 if they overflow the MaxInt64 size. This
// cast should be used ONLY while decoding int64 values that are
// sent as uint64 to reduce the payload size, otherwise the approach
// is not correct in the general sense.
func castInt64(v uint64) (int64, bool) {
if v > math.MaxInt64 {
return 0, false
}
return int64(v), true
}
// parseInt64 parses an int64 even if the sent value is an uint64;
// this is required because the encoding library could remove bytes from the encoded
// payload to reduce the size, if they're not needed.
func parseInt64(dc *msgp.Reader) (int64, error) {
// read the generic representation type without decoding
t, err := dc.NextType()
if err != nil {
return 0, err
}
switch t {
case msgp.IntType:
i, err := dc.ReadInt64()
if err != nil {
return 0, err
}
return i, nil
case msgp.UintType:
u, err := dc.ReadUint64()
if err != nil {
return 0, err
}
// force-cast
i, ok := castInt64(u)
if !ok {
return 0, errors.New("found uint64, overflows int64")
}
return i, nil
default:
return 0, msgp.TypeError{Encoded: t, Method: msgp.IntType}
}
}
// parseUint64 parses an uint64 even if the sent value is an int64;
// this is required because the language used for the encoding library
// may not have unsigned types. An example is early version of Java
// (and so JRuby interpreter) that encodes uint64 as int64:
// http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
func parseUint64(dc *msgp.Reader) (uint64, error) {
// read the generic representation type without decoding
t, err := dc.NextType()
if err != nil {
return 0, err
}
switch t {
case msgp.UintType:
u, err := dc.ReadUint64()
if err != nil {
return 0, err
}
return u, err
case msgp.IntType:
i, err := dc.ReadInt64()
if err != nil {
return 0, err
}
return uint64(i), nil
default:
return 0, msgp.TypeError{Encoded: t, Method: msgp.IntType}
}
}
// cast to int32 values that are int32 but that are sent in uint32
// over the wire. Set to 0 if they overflow the MaxInt32 size. This
// cast should be used ONLY while decoding int32 values that are
// sent as uint32 to reduce the payload size, otherwise the approach
// is not correct in the general sense.
func castInt32(v uint32) (int32, bool) {
if v > math.MaxInt32 {
return 0, false
}
return int32(v), true
}
// parseInt32 parses an int32 even if the sent value is an uint32;
// this is required because the encoding library could remove bytes from the encoded
// payload to reduce the size, if they're not needed.
func parseInt32(dc *msgp.Reader) (int32, error) {
// read the generic representation type without decoding
t, err := dc.NextType()
if err != nil {
return 0, err
}
switch t {
case msgp.IntType:
i, err := dc.ReadInt32()
if err != nil {
return 0, err
}
return i, nil
case msgp.UintType:
u, err := dc.ReadUint32()
if err != nil {
return 0, err
}
// force-cast
i, ok := castInt32(u)
if !ok {
return 0, errors.New("found uint32, overflows int32")
}
return i, nil
default:
return 0, msgp.TypeError{Encoded: t, Method: msgp.IntType}
}
}