Skip to content

Commit

Permalink
Ensure getVars only returns primitive values when value == <nil>
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoholiveira committed Apr 16, 2024
1 parent 0926afe commit a2c65db
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
4 changes: 2 additions & 2 deletions jsonlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ func some(values, data interface{}) interface{} {
for _, value := range subject.([]interface{}) {
v := apply(
solveVars(
solveVars(parsed[1], value),
data,
solveVars(parsed[1], data),
value,
),
value,
)
Expand Down
25 changes: 25 additions & 0 deletions jsonlogic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,3 +907,28 @@ func TestIssue79(t *testing.T) {
expected := `true`
assert.JSONEq(t, expected, result.String())
}

func TestIssue81(t *testing.T) {
rule := `{
"some": [
{"var": "A"},
{"!=": [
{"var": ".B"},
{"var": "B"}
]}
]}
`

data := `{"A":[{"B":1}], "B":2}`

var result bytes.Buffer

err := Apply(strings.NewReader(rule), strings.NewReader(data), &result)

if err != nil {
t.Fatal(err)
}

expected := `true`
assert.JSONEq(t, expected, result.String())
}
9 changes: 8 additions & 1 deletion vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ func solveVars(values, data interface{}) interface{} {
}

func getVar(value, data interface{}) interface{} {
if value == nil || (isString(value) && toString(value) == "") {
if value == nil {
if !isPrimitive(data) {
return nil
}
return data
}

if isString(value) && toString(value) == "" {
return data
}

Expand Down

0 comments on commit a2c65db

Please sign in to comment.