From 0a36a1fc2e15583ce8d7a5d9dda162d2ce077837 Mon Sep 17 00:00:00 2001 From: Kavashen Pather Date: Wed, 21 Jul 2021 17:58:08 +1000 Subject: [PATCH] fix: if multiple jq results exist return all (#306) * fix: if multiple jq resultsexist return all * feat: add jq test --- internal/processor/data.go | 6 ++++-- internal/processor/data_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/internal/processor/data.go b/internal/processor/data.go index 463c303c..e16c838c 100644 --- a/internal/processor/data.go +++ b/internal/processor/data.go @@ -90,6 +90,8 @@ func runJq(dataSets interface{}, api load.API) []interface{} { return []interface{}{} } + tempDataSets := []interface{}{} + iter := query.Run(dataSets) for { v, ok := iter.Next() @@ -101,7 +103,7 @@ func runJq(dataSets interface{}, api load.API) []interface{} { case []interface{}: return value case map[string]interface{}: - return []interface{}{value} + tempDataSets = append(tempDataSets, value) case error: load.Logrus.WithFields(logrus.Fields{ "api": api.Name, @@ -112,5 +114,5 @@ func runJq(dataSets interface{}, api load.API) []interface{} { } - return []interface{}{} + return tempDataSets } diff --git a/internal/processor/data_test.go b/internal/processor/data_test.go index ba32b940..1acef719 100644 --- a/internal/processor/data_test.go +++ b/internal/processor/data_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/newrelic/nri-flex/internal/load" + "github.com/stretchr/testify/assert" ) func TestRunJq(t *testing.T) { @@ -41,3 +42,31 @@ func TestRunJq(t *testing.T) { } } + +func TestRunJqMultipleResults(t *testing.T) { + + input := []interface{}{map[string]interface{}{ + "foo": map[string]interface{}{ + "data": map[string]interface{}{ + "abc": 1, + "def": 2, + }, + "more_data": map[string]interface{}{ + "ghi": 3, + "jkl": 4, + }, + }, + }} + + api := load.API{ + Jq: ".foo.data,.[0].foo.more_data", + } + + var dataSets = runJq(input, api) + var expectedResult = []interface{}{ + map[string]interface{}{"abc": 1, "def": 2}, + map[string]interface{}{"ghi": 3, "jkl": 4}, + } + + assert.Equal(t, expectedResult, dataSets) +}