Skip to content

Commit

Permalink
Merge pull request #8 from bminer/master
Browse files Browse the repository at this point in the history
Support decoding of multiple series
  • Loading branch information
cbrake authored Jan 23, 2019
2 parents 1626d77 + bcc6e4e commit 855509e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 40 deletions.
3 changes: 1 addition & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
36 changes: 18 additions & 18 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@ 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{
Metadata: nil,
Result: result,
TagName: "influx",
Metadata: nil,
Result: result,
TagName: "influx",
WeaklyTypedInput: false,
ZeroFields: 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))
Expand Down
14 changes: 7 additions & 7 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestDecode(t *testing.T) {

decoded := []DecodeType{}

err := decode(data, &decoded)
err := decode([]influxModels.Row{data}, &decoded)
if err != nil {
t.Error("Error decoding: ", err)
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestDecodeMissingColumn(t *testing.T) {
expected := []DecodeType{{1, 0}}
data.Values = append(data.Values, []interface{}{1})
decoded := []DecodeType{}
err := decode(data, &decoded)
err := decode([]influxModels.Row{data}, &decoded)

if err != nil {
t.Error("UnExpected error decoding: ", data, &decoded)
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestDecodeWrongType(t *testing.T) {
expected := []DecodeType{{1, 2.0}}
data.Values = append(data.Values, []interface{}{1.0, 2})
decoded := []DecodeType{}
err := decode(data, &decoded)
err := decode([]influxModels.Row{data}, &decoded)
if err != nil {
t.Error("Unexpected error decoding: ", err, data, decoded)
}
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestDecodeTime(t *testing.T) {
expected := []DecodeType{{ti, 2.0}}
data.Values = append(data.Values, []interface{}{timeS, 2.0})
decoded := []DecodeType{}
err = decode(data, &decoded)
err = decode([]influxModels.Row{data}, &decoded)

if err != nil {
t.Error("Error decoding: ", err)
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestDecodeJsonNumber(t *testing.T) {
expected := []DecodeType{{1, 2.0}}
data.Values = append(data.Values, []interface{}{json.Number("1"), json.Number("2.0")})
decoded := []DecodeType{}
err := decode(data, &decoded)
err := decode([]influxModels.Row{data}, &decoded)

if err != nil {
t.Error("Error decoding: ", err)
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestDecodeUnsedStructValue(t *testing.T) {
expected := []DecodeType{{1, 0}}
data.Values = append(data.Values, []interface{}{1, 1.1})
decoded := []DecodeType{}
err := decode(data, &decoded)
err := decode([]influxModels.Row{data}, &decoded)

if err != nil {
t.Error("Error decoding: ", err)
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestDecodeMeasure(t *testing.T) {
expected := []DecodeType{{"bla", 1, 0}}
data.Values = append(data.Values, []interface{}{1, 1.1})
decoded := []DecodeType{}
err := decode(data, &decoded)
err := decode([]influxModels.Row{data}, &decoded)

if decoded[0].InfluxMeasurement != expected[0].InfluxMeasurement {
t.Error("Decoded Wrong measure")
Expand Down
26 changes: 13 additions & 13 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func TestEncodeSetsMesurment(t *testing.T) {

func TestEncodeUsesTimeField(t *testing.T) {
type MyType struct {
MyTimeField time.Time `influx:"my_time_field"`
Val string `influx:"val"`
MyTimeField time.Time `influx:"my_time_field"`
Val string `influx:"val"`
}

td, _ := time.Parse(time.RFC822, "27 Oct 78 15:04 PST")

d := &MyType{td,"test-data"}
d := &MyType{td, "test-data"}
tv, _, _, _, err := encode(d, &usingValue{"my_time_field", false})

if tv != td {
Expand All @@ -52,16 +52,16 @@ func TestEncodeUsesTimeField(t *testing.T) {

func TestEncode(t *testing.T) {
type MyType struct {
InfluxMeasurement Measurement
Time time.Time `influx:"time"`
TagValue string `influx:"tagValue,tag"`
TagAndFieldValue string `influx:"tagAndFieldValue,tag,field"`
IntValue int `influx:"intValue"`
FloatValue float64 `influx:"floatValue"`
BoolValue bool `influx:"boolValue"`
StringValue string `influx:"stringValue"`
StructFieldName string `influx:""`
IgnoredValue string `influx:"-"`
InfluxMeasurement Measurement
Time time.Time `influx:"time"`
TagValue string `influx:"tagValue,tag"`
TagAndFieldValue string `influx:"tagAndFieldValue,tag,field"`
IntValue int `influx:"intValue"`
FloatValue float64 `influx:"floatValue"`
BoolValue bool `influx:"boolValue"`
StringValue string `influx:"stringValue"`
StructFieldName string `influx:""`
IgnoredValue string `influx:"-"`
}

d := MyType{
Expand Down

0 comments on commit 855509e

Please sign in to comment.