Skip to content

Commit

Permalink
Merge pull request #147 from awakchau-tibco/master
Browse files Browse the repository at this point in the history
fixed function json.exists
  • Loading branch information
vijaynalawade authored Nov 10, 2021
2 parents 9ac17b8 + ef28540 commit 4a77f17
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 4a77f17

Please sign in to comment.