Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions model/JsonDataAccessLayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ package model
import (
"encoding/json"
"fmt"
"github.com/hyperjumptech/grule-rule-engine/pkg"
"reflect"
"time"

"github.com/hyperjumptech/grule-rule-engine/pkg"
)

var (
Expand Down Expand Up @@ -225,9 +226,11 @@ func (vn *JSONValueNode) GetObjectValueByField(field string) (reflect.Value, err
return reflect.ValueOf(nil), fmt.Errorf("not an object or map")
}
tmap := vn.data.MapIndex(reflect.ValueOf(field))

// Return nil if the field does not exist in JSON
if tmap == reflect.ValueOf(nil) {

return reflect.ValueOf(nil), fmt.Errorf("json field '%s' is undefined", field)
return reflect.ValueOf(nil), nil
}

return tmap.Elem(), nil
Expand Down
17 changes: 16 additions & 1 deletion model/JsonDataAccessLayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
package model

import (
"github.com/stretchr/testify/assert"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

var (
Expand Down Expand Up @@ -297,3 +298,17 @@ func TestIsObject(t *testing.T) {
}
assert.Equal(t, 3, ln)
}

func TestMissingValues(t *testing.T) {
vn, err := NewJSONValueNode("{\"one\": \"1\", \"two\": 2}", "json")
if err != nil {
t.Log(err.Error())
t.Fail()
}
eval, err := vn.GetObjectValueByField("non-existent")
if err != nil {
t.Log(err.Error())
t.Fail()
}
assert.Equal(t, reflect.ValueOf(nil), eval)
}
Loading