Skip to content

Commit

Permalink
fix: if multiple jq results exist return all (#306)
Browse files Browse the repository at this point in the history
* fix: if multiple jq resultsexist return all

* feat: add jq test
  • Loading branch information
Kav91 authored Jul 21, 2021
1 parent ab040dc commit 0a36a1f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
6 changes: 4 additions & 2 deletions internal/processor/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand All @@ -112,5 +114,5 @@ func runJq(dataSets interface{}, api load.API) []interface{} {

}

return []interface{}{}
return tempDataSets
}
29 changes: 29 additions & 0 deletions internal/processor/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/newrelic/nri-flex/internal/load"
"github.com/stretchr/testify/assert"
)

func TestRunJq(t *testing.T) {
Expand Down Expand Up @@ -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)
}

0 comments on commit 0a36a1f

Please sign in to comment.