Skip to content

Commit

Permalink
json.exists now supports plain keys and jsonpath
Browse files Browse the repository at this point in the history
  • Loading branch information
awakchau-tibco committed Oct 29, 2021
1 parent 547c628 commit ef28540
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
5 changes: 4 additions & 1 deletion function/json/exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ func (fnExists) Eval(params ...interface{}) (interface{}, error) {
if strings.HasPrefix(strings.TrimSpace(expression), "$loop.") {
expression = strings.Replace(expression, "$loop", "$", -1)
}
if !strings.HasPrefix(expression, "$.") {
expression = "$." + expression
}
_, err := jsonpath.JsonPathLookup(params[0], expression)
if err != nil {
return false, err
return false, nil
}
return true, nil
}
31 changes: 21 additions & 10 deletions function/json/exists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
)

const inputCheckExists = `{
const inputExists = `{
"store": {
"book": [
{
Expand All @@ -35,7 +35,7 @@ const inputCheckExists = `{

func TestFnCheckExists(t *testing.T) {
var inputJSON interface{}
err := json.Unmarshal([]byte(inputCheckExists), &inputJSON)
err := json.Unmarshal([]byte(inputExists), &inputJSON)
assert.Nil(t, err)

f := &fnExists{}
Expand All @@ -44,35 +44,46 @@ func TestFnCheckExists(t *testing.T) {
assert.Equal(t, true, v)
}

func TestFnCheckExistsLoop(t *testing.T) {
func TestFnExistsLoop(t *testing.T) {
var inputJSON interface{}
err := json.Unmarshal([]byte(inputCheckExists), &inputJSON)
err := json.Unmarshal([]byte(inputExists), &inputJSON)
assert.Nil(t, err)

f := &fnExists{}
v, err := function.Eval(f, inputJSON, "$loop.store.book[?(@.price == 22.99)].price[0]")
assert.NotNil(t, err)
assert.Nil(t, err)
assert.Equal(t, false, v)
}

func TestFnCheckExistsNegative(t *testing.T) {
func TestFnExistsNegative(t *testing.T) {
var inputJSON interface{}
err := json.Unmarshal([]byte(inputCheckExists), &inputJSON)
err := json.Unmarshal([]byte(inputExists), &inputJSON)
assert.Nil(t, err)

f := &fnExists{}
v, err := function.Eval(f, inputJSON, "$.store.abc")
assert.NotNil(t, err)
assert.Nil(t, err)
assert.Equal(t, false, v)
}

func TestFnCheckExistsEmpty(t *testing.T) {
func TestFnExistsEmpty(t *testing.T) {
var inputJSON interface{}
err := json.Unmarshal([]byte(inputCheckExists), &inputJSON)
err := json.Unmarshal([]byte(inputExists), &inputJSON)
assert.Nil(t, err)

f := &fnExists{}
v, err := function.Eval(f, inputJSON, "$.emptyString")
assert.Nil(t, err)
assert.Equal(t, true, v)
}

func TestFnExistsWithoutJSONPath(t *testing.T) {
var inputJSON interface{}
err := json.Unmarshal([]byte(inputExists), &inputJSON)
assert.Nil(t, err)

f := &fnExists{}
v, err := function.Eval(f, inputJSON, "expensive")
assert.Nil(t, err)
assert.Equal(t, true, v)
}

0 comments on commit ef28540

Please sign in to comment.