From 0fb56932bc4ddcaee95113960603099e0b03f194 Mon Sep 17 00:00:00 2001 From: Blake Miner Date: Wed, 23 Jan 2019 12:53:27 -0500 Subject: [PATCH] Support decoding multiple series (closes #7) --- client.go | 3 +-- decode.go | 29 +++++++++++++++-------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/client.go b/client.go index 1bdfb9e..e8bc062 100644 --- a/client.go +++ b/client.go @@ -185,8 +185,7 @@ func (c *helperClient) DecodeQuery(q string, result interface{}) (err error) { return } - series := results[0].Series[0] - err = decode(series, result) + err = decode(results[0].Series, result) return } diff --git a/decode.go b/decode.go index 1e55d1a..c43c6d4 100644 --- a/decode.go +++ b/decode.go @@ -8,28 +8,28 @@ import ( "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 { +func decode(influxResult []influxModels.Row, result interface{}) error { influxData := make([]map[string]interface{}, 0) - for _, v := range influxResult.Values { - r := make(map[string]interface{}) - for i, c := range influxResult.Columns { - if len(v) >= i+1 { - r[c] = v[i] + 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 influxResult.Tags { - r[tag] = val - } - r["InfluxMeasurement"] = influxResult.Name + for tag, val := range series.Tags { + r[tag] = val + } + r["InfluxMeasurement"] = series.Name - influxData = append(influxData, r) + influxData = append(influxData, r) + } } config := &mapstructure.DecoderConfig{ @@ -54,3 +54,4 @@ func decode(influxResult influxModels.Row, result interface{}) error { return decoder.Decode(influxData) } +