Skip to content

Commit

Permalink
fix: nil handling of jq/jsonpath/jmespath
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Dec 26, 2024
1 parent 3411c96 commit 4474982
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
25 changes: 22 additions & 3 deletions coll/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/ohler55/ojg/jp"
)

const NullValue = "NULL_VALUE"

// JQ -
func JQ(ctx context.Context, jqExpr string, in interface{}) (interface{}, error) {
query, err := gojq.Parse(jqExpr)
Expand Down Expand Up @@ -50,6 +52,9 @@ func JQ(ctx context.Context, jqExpr string, in interface{}) (interface{}, error)
} else {
out = a
}
if out == NullValue || out == nil {
out = ""
}

return out, nil
}
Expand All @@ -67,13 +72,16 @@ func JMESPath(jmesPath string, in interface{}) (interface{}, error) {
in = v
}
}
result, err := jmespath.Search(jmesPath, in)
out, err := jmespath.Search(jmesPath, in)

if err != nil {
return nil, fmt.Errorf("%+w", err)
}
if out == nil || out == NullValue || out == "" {
out = ""
}

return result, nil
return out, nil
}

func JSONPath(jsonPath string, in interface{}) (interface{}, error) {
Expand All @@ -94,7 +102,18 @@ func JSONPath(jsonPath string, in interface{}) (interface{}, error) {
if err != nil {
return nil, err
}
return x.Get(in), nil
out := x.Get(in)

if len(out) == 1 {
if out[0] == NullValue || out[0] == nil {
return "", nil
}
return out[0], nil
}
if len(out) == 0 {
return "", nil
}
return out, nil
}

func isSupportableType(in interface{}) bool {
Expand Down
6 changes: 4 additions & 2 deletions tests/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,15 @@ func TestCelJSON(t *testing.T) {
{map[string]interface{}{"i": person}, "i.toJSONPretty('\t').JSON().addresses[0].country", "Nepal"},
{nil, "{'name': 'aditya'}.toJSONPretty('\t')", "{\n\t\"name\": \"aditya\"\n}"},

{map[string]interface{}{"i": person}, "jsonpath('$.addresses[-1:].city_name', i)[0]", "New York"},
{map[string]interface{}{"i": person}, "jmespath('addresses[*].city_name', i)[0]", "Kathmandu"},
{map[string]interface{}{"i": person}, "jsonpath('$.addresses[-1:].city_name', i)", "New York"},
{map[string]interface{}{"i": person}, "jmespath('addresses[*].city_name | [0]', i)", "Kathmandu"},
//FIXME: jmespath function return a parse error
{map[string]interface{}{"i": person}, "jmespath('length(addresses)', i)", "3"},
{map[string]interface{}{"i": person}, "jmespath('ceil(`1.2`)', i)", "2"},
//FIXME: jmespath always returns a list
{map[string]interface{}{"i": person}, "jmespath('name', i)", "John Doe"},
{map[string]interface{}{"i": person}, "jmespath('Address.country', i)", ""},
{map[string]interface{}{"i": person}, "jsonpath('Address.country', i)", ""},

{map[string]interface{}{"i": person}, "jq('.Address.city_name', i)", "Kathmandu"},
{map[string]interface{}{"i": person}, "jq('.Address.country', i)", ""},
Expand Down

0 comments on commit 4474982

Please sign in to comment.