Skip to content

Commit

Permalink
Ensure value is string before trying to convert it (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Henrique Oliveira authored Oct 18, 2023
1 parent 2fc9a0c commit f01aadf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions jsonlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"sort"
"strings"
// "runtime/debug"

"github.com/mitchellh/copystructure"
)
Expand Down
24 changes: 24 additions & 0 deletions jsonlogic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,30 @@ func TestIssue58_example(t *testing.T) {
assert.JSONEq(t, expected, result.String())
}

func TestIssue70(t *testing.T) {
data := strings.NewReader(`{"people": [
{"age":18, "name":"John"},
{"age":20, "name":"Luke"},
{"age":18, "name":"Mark"}
]}`)
logic := strings.NewReader(`{"filter": [
{"var": ["people"]},
{"==": [{"var": ["age"]}, 18]}
]}`)

var result bytes.Buffer
err := Apply(logic, data, &result)
if err != nil {
t.Fatal(err)
}

expected := `[
{"age": 18, "name": "John"},
{"age": 18, "name": "Mark"}
]`
assert.JSONEq(t, expected, result.String())
}

func TestIssue71_example_empty_min(t *testing.T) {
data := strings.NewReader(`{}`)
logic := strings.NewReader(`{"min":[]}`)
Expand Down
2 changes: 1 addition & 1 deletion vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func solveVars(values, data interface{}) interface{} {

for key, value := range values.(map[string]interface{}) {
if key == "var" {
if value == "" || strings.HasPrefix(value.(string), ".") {
if isString(value) && (value == "" || strings.HasPrefix(value.(string), ".")) {
logic["var"] = value
continue
}
Expand Down

0 comments on commit f01aadf

Please sign in to comment.