-
Notifications
You must be signed in to change notification settings - Fork 9
/
decode.go
56 lines (47 loc) · 1.34 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
package influxdbhelper
import (
"reflect"
"time"
influxModels "github.com/influxdata/influxdb1-client/models"
"github.com/mitchellh/mapstructure"
)
// Decode is used to process data returned by an InfluxDb query and uses reflection
// to transform it into an array of structs of type result.
//
// This function is used internally by the Query function.
func decode(influxResult []influxModels.Row, result interface{}) error {
influxData := make([]map[string]interface{}, 0)
for _, series := range influxResult {
for _, v := range series.Values {
r := make(map[string]interface{})
for i, c := range series.Columns {
if len(v) >= i+1 {
r[c] = v[i]
}
}
for tag, val := range series.Tags {
r[tag] = val
}
r["InfluxMeasurement"] = series.Name
influxData = append(influxData, r)
}
}
config := &mapstructure.DecoderConfig{
Metadata: nil,
Result: result,
TagName: "influx",
WeaklyTypedInput: false,
ZeroFields: false,
DecodeHook: func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if t == reflect.TypeOf(time.Time{}) && f == reflect.TypeOf("") {
return time.Parse(time.RFC3339, data.(string))
}
return data, nil
},
}
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}
return decoder.Decode(influxData)
}