Skip to content

Commit 4a77f17

Browse files
Merge pull request #147 from awakchau-tibco/master
fixed function json.exists
2 parents 9ac17b8 + ef28540 commit 4a77f17

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

function/json/exists.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ func (fnExists) Eval(params ...interface{}) (interface{}, error) {
3636
if strings.HasPrefix(strings.TrimSpace(expression), "$loop.") {
3737
expression = strings.Replace(expression, "$loop", "$", -1)
3838
}
39+
if !strings.HasPrefix(expression, "$.") {
40+
expression = "$." + expression
41+
}
3942
_, err := jsonpath.JsonPathLookup(params[0], expression)
4043
if err != nil {
41-
return false, err
44+
return false, nil
4245
}
4346
return true, nil
4447
}

function/json/exists_test.go

+21-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/stretchr/testify/assert"
99
)
1010

11-
const inputCheckExists = `{
11+
const inputExists = `{
1212
"store": {
1313
"book": [
1414
{
@@ -35,7 +35,7 @@ const inputCheckExists = `{
3535

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

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

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

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

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

6363
f := &fnExists{}
6464
v, err := function.Eval(f, inputJSON, "$.store.abc")
65-
assert.NotNil(t, err)
65+
assert.Nil(t, err)
6666
assert.Equal(t, false, v)
6767
}
6868

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

7474
f := &fnExists{}
7575
v, err := function.Eval(f, inputJSON, "$.emptyString")
7676
assert.Nil(t, err)
7777
assert.Equal(t, true, v)
7878
}
79+
80+
func TestFnExistsWithoutJSONPath(t *testing.T) {
81+
var inputJSON interface{}
82+
err := json.Unmarshal([]byte(inputExists), &inputJSON)
83+
assert.Nil(t, err)
84+
85+
f := &fnExists{}
86+
v, err := function.Eval(f, inputJSON, "expensive")
87+
assert.Nil(t, err)
88+
assert.Equal(t, true, v)
89+
}

0 commit comments

Comments
 (0)