Repository: diegoholiveira/jsonlogic (go)

Issue:
Non-syntactic sugar version of {"var": [""]} not working?
On the playground, running something like:

data: 

```
{"integers": [1,2,3,4,5,6,7,8]}
```

```
{"map":[
	{"var":"integers"},
	{"*":[{"var":[""]},2]}
]}
```

should be equivalent to:

```
{"map":[
	{"var":"integers"},
	{"*":[{"var":""},2]}
]}
```

But it looks like `{"var": [""]}` actually returns nil?


Pull request description:
Ensure empty slices have the same behavior as empty strings
closes #96 

Patch:
diff --git a/helpers.go b/helpers.go
index 166bab0..05a8b63 100644
--- a/helpers.go
+++ b/helpers.go
@@ -38,6 +38,20 @@ func isSlice(obj any) bool {
 	return is(obj, reflect.Slice)
 }
 
+func isEmptySlice(obj any) bool {
+	if !isSlice(obj) {
+		return false
+	}
+
+	for _, v := range obj.([]any) {
+		if isTrue(v) {
+			return false
+		}
+	}
+
+	return true
+}
+
 func isTrue(obj any) bool {
 	if isBool(obj) {
 		return obj.(bool)
diff --git a/vars.go b/vars.go
index 44ae962..429d720 100644
--- a/vars.go
+++ b/vars.go
@@ -17,6 +17,11 @@ func solveVars(values, data any) any {
 					continue
 				}
 
+				if isEmptySlice(value) {
+					logic["var"] = ""
+					continue
+				}
+
 				val := getVar(value, data)
 				if val != nil {
 					return val


Test patch:
diff --git a/issues_test.go b/issues_test.go
index b61fdcf..8490345 100644
--- a/issues_test.go
+++ b/issues_test.go
@@ -306,7 +306,6 @@ func TestIssue81(t *testing.T) {
 	var result bytes.Buffer
 
 	err := jsonlogic.Apply(strings.NewReader(rule), strings.NewReader(data), &result)
-
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -314,3 +313,22 @@ func TestIssue81(t *testing.T) {
 	expected := `true`
 	assert.JSONEq(t, expected, result.String())
 }
+
+func TestIssue96(t *testing.T) {
+	rule := `{"map":[
+      {"var":"integers"},
+	  {"*":[{"var":[""]},2]}
+    ]}`
+
+	data := `{"integers": [1,2,3]}`
+
+	var result bytes.Buffer
+
+	err := jsonlogic.Apply(strings.NewReader(rule), strings.NewReader(data), &result)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	expected := `[2, 4, 6]`
+	assert.JSONEq(t, expected, result.String())
+}