-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvalue.go
More file actions
346 lines (319 loc) · 8.61 KB
/
value.go
File metadata and controls
346 lines (319 loc) · 8.61 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package lineprotocol
import (
"bytes"
"errors"
"fmt"
"math"
"strconv"
"unicode/utf8"
)
// ErrValueOutOfRange signals that a value is out of the acceptable numeric range.
var ErrValueOutOfRange = errors.New("line-protocol value out of range")
// Value holds one of the possible line-protocol field values.
type Value struct {
// number covers:
// - signed integer
// - unsigned integer
// - bool
// - float
number uint64
// bytes holds the string bytes or a sentinel (see below)
// when the value's not holding a string.
bytes []byte
}
var (
intSentinel = [1]byte{'i'}
uintSentinel = [1]byte{'u'}
floatSentinel = [1]byte{'f'}
boolSentinel = [1]byte{'b'}
)
// MustNewValue is like NewValue except that it panics on failure.
func MustNewValue(x interface{}) Value {
v, ok := NewValue(x)
if !ok {
panic(fmt.Errorf("invalid value for NewValue: %T (%#v)", x, x))
}
return v
}
// Equal reports whether v1 is equal to v2.
func (v1 Value) Equal(v2 Value) bool {
k := v1.Kind()
if v2.Kind() != k {
return false
}
if k != Float {
return v1.number == v2.number && bytes.Equal(v1.bytes, v2.bytes)
}
// Floats can't be compared bitwise.
return v1.FloatV() == v2.FloatV()
}
// NewValueFromBytes creates a value of the given kind with the
// given data, as returned from Decoder.NextFieldBytes.
//
// If the value is out of range, errors.Is(err, ErrValueOutOfRange) will return true.
//
// The data for Int and Uint kinds should not include
// the type suffixes present in the line-protocol field values.
// For example, the data for the zero Int should be "0" not "0i".
//
// The data for String should not include the surrounding quotes,
// should be unescaped already and should not contain invalid
// utf-8. The returned value will contain a reference to data - it does not make a copy.
func NewValueFromBytes(kind ValueKind, data []byte) (Value, error) {
return newValueFromBytes(kind, data, true)
}
func newValueFromBytes(kind ValueKind, data []byte, checkUTF8 bool) (Value, error) {
switch kind {
case Int:
x, err := parseIntBytes(data, 10, 64)
if err != nil {
return Value{}, maybeOutOfRange(err, "invalid integer value syntax")
}
return Value{
number: uint64(x),
bytes: intSentinel[:],
}, nil
case Uint:
x, err := parseUintBytes(data, 10, 64)
if err != nil {
return Value{}, maybeOutOfRange(err, "invalid unsigned integer value syntax")
}
return Value{
number: x,
bytes: uintSentinel[:],
}, nil
case Float:
x, err := parseFloatBytes(data, 64)
if err != nil {
return Value{}, maybeOutOfRange(err, "invalid float value syntax")
}
if math.IsInf(x, 0) || math.IsNaN(x) {
return Value{}, fmt.Errorf("non-number %q cannot be represented as a line-protocol field value", data)
}
return Value{
number: math.Float64bits(x),
bytes: floatSentinel[:],
}, nil
case Bool:
x, err := parseBoolBytes(data)
if err != nil {
return Value{}, fmt.Errorf("invalid bool value %q", data)
}
return Value{
number: uint64(x),
bytes: boolSentinel[:],
}, nil
case String:
if checkUTF8 && !utf8.Valid(data) {
return Value{}, fmt.Errorf("invalid utf-8 found in value %q", data)
}
return Value{
bytes: data,
}, nil
case Unknown:
return Value{}, fmt.Errorf("cannot parse value %q with unknown kind", data)
default:
return Value{}, fmt.Errorf("unexpected value kind %d (value %q)", kind, data)
}
}
// NewValue returns a Value containing the value of x, which must
// be of type int64 (Int), uint64 (Uint), float64 (Float), bool (Bool),
// string (String) or []byte (String).
//
// Unlike NewValueFromBytes, NewValue will make a copy of the byte
// slice if x is []byte - use NewValueFromBytes if you require zero-copy
// semantics.
//
// NewValue will fail and return false if x isn't a recognized
// type or if it's a non-finite float64, or if a string or byte slice contains
// invalid utf-8.
func NewValue(x interface{}) (Value, bool) {
switch x := x.(type) {
case int64:
return IntValue(x), true
case uint64:
return UintValue(x), true
case float64:
return FloatValue(x)
case bool:
return BoolValue(x), true
case string:
return StringValue(x)
case []byte:
return StringValueFromBytes(x)
}
return Value{}, false
}
// IntValue returns a Value containing the value of x.
func IntValue(x int64) Value {
return Value{
number: uint64(x),
bytes: intSentinel[:],
}
}
// UintValue returns a Value containing the value of x.
func UintValue(x uint64) Value {
return Value{
number: uint64(x),
bytes: uintSentinel[:],
}
}
// FloatValue returns a Value containing the value of x.
//
// FloatValue will fail and return false if x is non-finite.
func FloatValue(x float64) (Value, bool) {
if math.IsInf(x, 0) || math.IsNaN(x) {
return Value{}, false
}
return Value{
number: math.Float64bits(x),
bytes: floatSentinel[:],
}, true
}
// BoolValue returns a Value containing the value of x.
func BoolValue(x bool) Value {
n := uint64(0)
if x {
n = 1
}
return Value{
number: uint64(n),
bytes: boolSentinel[:],
}
}
// StringValue returns a Value containing the value of x.
//
// StringValue will fail and return false if x contains invalid utf-8.
func StringValue(x string) (Value, bool) {
if !utf8.ValidString(x) {
return Value{}, false
}
return Value{
bytes: []byte(x),
}, true
}
// StringValueFromBytes returns a Value containing the value of x.
//
// StringValueFromBytes will fail and return false if x contains invalid utf-8.
//
// Unlike NewValueFromBytes, StringValueFromBytes will make a copy of the byte
// slice - use NewValueFromBytes if you require zero-copy semantics.
func StringValueFromBytes(x []byte) (Value, bool) {
if !utf8.Valid(x) {
return Value{}, false
}
return Value{
bytes: append([]byte(nil), x...),
}, true
}
// IntV returns the value as an int64. It panics if v.Kind is not Int.
func (v Value) IntV() int64 {
v.mustBe(Int)
return int64(v.number)
}
// UintV returns the value as a uint64. It panics if v.Kind is not Uint.
func (v Value) UintV() uint64 {
v.mustBe(Uint)
return v.number
}
// FloatV returns the value as a float64. It panics if v.Kind is not Float.
func (v Value) FloatV() float64 {
v.mustBe(Float)
return math.Float64frombits(v.number)
}
// StringV returns the value as a string. It panics if v.Kind is not String.
func (v Value) StringV() string {
v.mustBe(String)
return string(v.bytes)
}
// BytesV returns the value as a []byte. It panics if v.Kind is not String.
// Note that this may return a direct reference to the byte slice within the
// value - modifying the returned byte slice may mutate the contents
// of the Value.
func (v Value) BytesV() []byte {
v.mustBe(String)
return v.bytes
}
// BoolV returns the value as a bool. It panics if v.Kind is not Bool.
func (v Value) BoolV() bool {
v.mustBe(Bool)
return v.number != 0
}
// Interface returns the value as an interface. The returned value
// will have a different dynamic type depending on the value kind;
// one of int64 (Int), uint64 (Uint), float64 (Float), string (String), bool (Bool).
func (v Value) Interface() interface{} {
switch v.Kind() {
case Int:
return v.IntV()
case Uint:
return v.UintV()
case String:
return v.StringV()
case Bool:
return v.BoolV()
case Float:
return v.FloatV()
default:
// Shouldn't be able to happen.
panic("unknown value kind")
}
}
func (v Value) mustBe(k ValueKind) {
if v.Kind() != k {
panic(fmt.Errorf("value has unexpected kind; got %v want %v", v.Kind(), k))
}
}
func (v Value) Kind() ValueKind {
if len(v.bytes) != 1 {
return String
}
switch &v.bytes[0] {
case &intSentinel[0]:
return Int
case &uintSentinel[0]:
return Uint
case &floatSentinel[0]:
return Float
case &boolSentinel[0]:
return Bool
}
return String
}
// String returns the value as it would be encoded in a line-protocol entry.
func (v Value) String() string {
return string(v.AppendBytes(nil))
}
// AppendTo appends the encoded value of v to buf.
func (v Value) AppendBytes(dst []byte) []byte {
switch v.Kind() {
case Float:
return strconv.AppendFloat(dst, v.FloatV(), 'g', -1, 64)
case Int:
dst = strconv.AppendInt(dst, v.IntV(), 10)
dst = append(dst, 'i')
return dst
case Uint:
dst = strconv.AppendUint(dst, v.UintV(), 10)
dst = append(dst, 'u')
return dst
case Bool:
if v.BoolV() {
return append(dst, "true"...)
}
return append(dst, "false"...)
case String:
dst = append(dst, '"')
dst = fieldStringValEscapes.appendEscaped(dst, unsafeBytesToString(v.bytes))
dst = append(dst, '"')
return dst
default:
panic("unknown kind")
}
}
func maybeOutOfRange(err error, s string) error {
if err, ok := err.(*strconv.NumError); ok && err.Err == strconv.ErrRange {
return ErrValueOutOfRange
}
return errors.New(s)
}