From 270d4c1ca8545d15cdd05a59bade5af869fd0f5d Mon Sep 17 00:00:00 2001 From: Diego Henrique Oliveira Date: Wed, 18 Oct 2023 10:21:39 -0300 Subject: [PATCH] Ensure value is string before trying to convert it --- jsonlogic.go | 1 + jsonlogic_test.go | 24 ++++++++++++++++++++++++ vars.go | 2 +- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/jsonlogic.go b/jsonlogic.go index babe7f8..48a0320 100644 --- a/jsonlogic.go +++ b/jsonlogic.go @@ -6,6 +6,7 @@ import ( "io" "sort" "strings" + // "runtime/debug" "github.com/mitchellh/copystructure" ) diff --git a/jsonlogic_test.go b/jsonlogic_test.go index 07b0b02..aa9e548 100644 --- a/jsonlogic_test.go +++ b/jsonlogic_test.go @@ -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":[]}`) diff --git a/vars.go b/vars.go index 46294d5..d619e6e 100644 --- a/vars.go +++ b/vars.go @@ -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 }