-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
entry.go
310 lines (280 loc) · 6.67 KB
/
entry.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
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
package binary
import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"math"
"strings"
"time"
"github.com/influxdata/telegraf/internal"
)
type Entry struct {
Name string `toml:"name"`
Type string `toml:"type"`
Bits uint64 `toml:"bits"`
Omit bool `toml:"omit"`
Terminator string `toml:"terminator"`
Timezone string `toml:"timezone"`
Assignment string `toml:"assignment"`
termination []byte
location *time.Location
}
func (e *Entry) check() error {
// Normalize cases
e.Assignment = strings.ToLower(e.Assignment)
e.Terminator = strings.ToLower(e.Terminator)
if e.Assignment != "time" {
e.Type = strings.ToLower(e.Type)
}
// Handle omitted fields
if e.Omit {
if e.Bits == 0 && e.Type == "" {
return errors.New("neither type nor bits given")
}
if e.Bits == 0 {
bits, err := bitsForType(e.Type)
if err != nil {
return err
}
e.Bits = bits
}
return nil
}
// Set name for global options
if e.Assignment == "measurement" || e.Assignment == "time" {
e.Name = e.Assignment
}
// Check the name
if e.Name == "" {
return errors.New("missing name")
}
// Check the assignment
var defaultType string
switch e.Assignment {
case "measurement":
defaultType = "string"
if e.Type != "string" && e.Type != "" {
return errors.New("'measurement' type has to be 'string'")
}
case "time":
bits := uint64(64)
switch e.Type {
// Make 'unix' the default
case "":
defaultType = "unix"
// Special plugin specific names
case "unix", "unix_ms", "unix_us", "unix_ns":
// Format-specification string formats
default:
bits = uint64(len(e.Type) * 8)
}
if e.Bits == 0 {
e.Bits = bits
}
switch e.Timezone {
case "", "utc":
// Make UTC the default
e.location = time.UTC
case "local":
e.location = time.Local
default:
var err error
e.location, err = time.LoadLocation(e.Timezone)
if err != nil {
return err
}
}
case "tag":
defaultType = "string"
case "", "field":
e.Assignment = "field"
default:
return fmt.Errorf("no assignment for %q", e.Name)
}
// Check type (special type for "time")
switch e.Type {
case "uint8", "int8", "uint16", "int16", "uint32", "int32", "uint64", "int64":
fallthrough
case "float32", "float64":
bits, err := bitsForType(e.Type)
if err != nil {
return err
}
if e.Bits == 0 {
e.Bits = bits
}
if bits < e.Bits {
return fmt.Errorf("type overflow for %q", e.Name)
}
case "bool":
if e.Bits == 0 {
e.Bits = 1
}
case "string":
// Check termination
switch e.Terminator {
case "", "fixed":
e.Terminator = "fixed"
if e.Bits == 0 {
return fmt.Errorf("require 'bits' for fixed-length string for %q", e.Name)
}
case "null":
e.termination = []byte{0}
if e.Bits != 0 {
return fmt.Errorf("cannot use 'bits' and 'null' terminator together for %q", e.Name)
}
default:
if e.Bits != 0 {
return fmt.Errorf("cannot use 'bits' and terminator together for %q", e.Name)
}
var err error
e.termination, err = hex.DecodeString(strings.TrimPrefix(e.Terminator, "0x"))
if err != nil {
return fmt.Errorf("decoding terminator failed for %q: %w", e.Name, err)
}
}
// We can only handle strings that adhere to byte-bounds
if e.Bits%8 != 0 {
return fmt.Errorf("non-byte length for string field %q", e.Name)
}
case "":
if defaultType == "" {
return fmt.Errorf("no type for %q", e.Name)
}
e.Type = defaultType
default:
if e.Assignment != "time" {
return fmt.Errorf("unknown type for %q", e.Name)
}
}
return nil
}
func (e *Entry) extract(in []byte, offset uint64) ([]byte, uint64, error) {
if e.Bits > 0 {
data, err := extractPart(in, offset, e.Bits)
return data, e.Bits, err
}
if e.Type != "string" {
return nil, 0, fmt.Errorf("unexpected entry: %v", e)
}
inbits := uint64(len(in)) * 8
// Read up to the termination
var found bool
var data []byte
var termOffset int
var n uint64
for offset+n+8 <= inbits {
buf, err := extractPart(in, offset+n, 8)
if err != nil {
return nil, 0, err
}
if len(buf) != 1 {
return nil, 0, fmt.Errorf("unexpected length %d", len(buf))
}
data = append(data, buf[0])
n += 8
// Check for terminator
if buf[0] == e.termination[termOffset] {
termOffset++
}
if termOffset == len(e.termination) {
found = true
break
}
}
if !found {
return nil, n, fmt.Errorf("terminator not found for %q", e.Name)
}
// Strip the terminator
return data[:len(data)-len(e.termination)], n, nil
}
func (e *Entry) convertType(in []byte, order binary.ByteOrder) (interface{}, error) {
switch e.Type {
case "uint8", "int8", "uint16", "int16", "uint32", "int32", "float32", "uint64", "int64", "float64":
return convertNumericType(in, e.Type, order)
case "bool":
return convertBoolType(in), nil
case "string":
return convertStringType(in), nil
}
return nil, fmt.Errorf("cannot handle type %q", e.Type)
}
func (e *Entry) convertTimeType(in []byte, order binary.ByteOrder) (time.Time, error) {
factor := int64(1)
switch e.Type {
case "unix":
factor *= 1000
fallthrough
case "unix_ms":
factor *= 1000
fallthrough
case "unix_us":
factor *= 1000
fallthrough
case "unix_ns":
raw, err := convertNumericType(in, "int64", order)
if err != nil {
return time.Unix(0, 0), err
}
v := raw.(int64)
return time.Unix(0, v*factor).In(e.location), nil
}
// We have a format specification (hopefully)
v := convertStringType(in)
return internal.ParseTimestamp(e.Type, v, e.location)
}
func convertStringType(in []byte) string {
return string(in)
}
func convertNumericType(in []byte, t string, order binary.ByteOrder) (interface{}, error) {
bits, err := bitsForType(t)
if err != nil {
return nil, err
}
inlen := uint64(len(in))
expected := bits / 8
if inlen > expected {
// Should never happen
return 0, fmt.Errorf("too many bytes %d vs %d", len(in), expected)
}
// Pad the data if shorter than the datatype length
buf := make([]byte, expected-inlen, expected)
buf = append(buf, in...)
switch t {
case "uint8":
return buf[0], nil
case "int8":
return int8(buf[0]), nil
case "uint16":
return order.Uint16(buf), nil
case "int16":
v := order.Uint16(buf)
return int16(v), nil
case "uint32":
return order.Uint32(buf), nil
case "int32":
v := order.Uint32(buf)
return int32(v), nil
case "uint64":
return order.Uint64(buf), nil
case "int64":
v := order.Uint64(buf)
return int64(v), nil
case "float32":
v := order.Uint32(buf)
return math.Float32frombits(v), nil
case "float64":
v := order.Uint64(buf)
return math.Float64frombits(v), nil
}
return nil, fmt.Errorf("no numeric type %q", t)
}
func convertBoolType(in []byte) bool {
for _, x := range in {
if x != 0 {
return true
}
}
return false
}