Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure getVars only returns primitive values when value == <nil> #82

Merged
merged 1 commit into from
Apr 16, 2024
Merged
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
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
Loading