-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
75 lines (65 loc) · 1.71 KB
/
decode.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
package senml
import (
"fmt"
"time"
)
// AutoTime toggles the automatic setting of zero timestamps to now.
// Disabling this option results in timestamps relative to zero time when no exact time is given.
var AutoTime = true
// Decode decodes a list of Measurement records into measurement values.
func Decode(records []Record) (list []Measurement, err error) {
list = make([]Measurement, len(records))
var now time.Time
if AutoTime {
now = time.Now()
}
var baseName string
var baseTime Numeric
var baseUnit Unit
var baseValue Numeric
var baseSum Numeric
for i, o := range records {
if o.BaseName != "" {
baseName = o.BaseName
}
if o.BaseTime != nil {
baseTime = o.BaseTime
}
if o.BaseUnit != "" {
baseUnit = Unit(o.BaseUnit)
}
if o.BaseValue != nil {
baseValue = o.BaseValue
}
if o.BaseSum != nil {
baseSum = o.BaseSum
}
var unit Unit
if o.Unit != "" {
unit = Unit(o.Unit)
} else {
unit = baseUnit
}
m := Attributes{
Name: baseName + o.Name,
Unit: unit,
Time: parseTime(baseTime, o.Time, now),
UpdateTime: numericToDuration(o.UpdateTime),
}
switch {
case o.Value != nil:
list[i] = &Value{Attributes: m, Value: numericToFloat64(sumNumeric(baseValue, o.Value))}
case o.Sum != nil:
list[i] = &Sum{Attributes: m, Value: numericToFloat64(sumNumeric(baseSum, o.Sum))}
case o.StringValue != "":
list[i] = &String{Attributes: m, Value: o.StringValue}
case len(o.DataValue) > 0:
list[i] = &Data{Attributes: m, Value: o.DataValue}
case o.BooleanValue != nil:
list[i] = &Boolean{Attributes: m, Value: *o.BooleanValue}
default:
return nil, fmt.Errorf("record has no value attribute set: %#v", o)
}
}
return list, nil
}