Repository: diegoholiveira/jsonlogic (go)

Issue:
failing to check array items by index (from an empty array)
First of all, that's a great lib, thank you all for your hard work on this!

When we try to access an specific condition in an empty array, we get _index out of range_.
How can we check if the array is not empty before accessing their items?

[Quick playground example](https://go.dev/play/p/nxtgY9mj90L)

```go
logic := strings.NewReader(`{"if":[ false, {"var":"values.0.categories"}, "else" ]}`)
data := strings.NewReader(`{ "values": [] }`)

var result bytes.Buffer
if err := jsonlogic.Apply(logic, data, &result); err != nil {
    fmt.Printf("failed, %v", err)
}

fmt.Println(result.String())
```

In the example above, even forcing the _false_ in the `if` condition, it's still evaluating the _true_ part, causing the index out of range.  
Shouldn't return the `"else"` value , even returning the error?
This is what the [JSON Logic playground](https://jsonlogic.com/play.html) returns:

![image](https://github.com/diegoholiveira/jsonlogic/assets/21963/6bac49af-48d8-4943-b718-7412f907fb6e)

Is there any other way to get the value of a property from an array's item (like `categories` from the first position in my example)?

Thank you

Pull request description:
Verify array before try to access any position
closes #74 

Patch:
diff --git a/vars.go b/vars.go
index d619e6e..3558899 100644
--- a/vars.go
+++ b/vars.go
@@ -91,7 +91,12 @@ func getVar(value, data interface{}) interface{} {
 		}
 
 		if isSlice(data) {
-			_value = data.([]interface{})[int(toNumber(part))]
+			pos := int(toNumber(part))
+			container := data.([]interface{})
+			if pos >= len(container) {
+				return _default
+			}
+			_value = container[pos]
 		}
 
 		if _value == nil {


Test patch:
diff --git a/jsonlogic_test.go b/jsonlogic_test.go
index aa9e548..e7516fe 100644
--- a/jsonlogic_test.go
+++ b/jsonlogic_test.go
@@ -807,6 +807,16 @@ func TestIssue71_example_max(t *testing.T) {
 	assert.JSONEq(t, expected, result.String())
 }
 
+func TestIssue74(t *testing.T) {
+	logic := strings.NewReader(`{"if":[ false, {"var":"values.0.categories"}, "else" ]}`)
+	data := strings.NewReader(`{ "values": [] }`)
+
+	var result bytes.Buffer
+	_ = Apply(logic, data, &result)
+	expected := `"else"`
+	assert.JSONEq(t, expected, result.String())
+}
+
 func TestJsonLogicWithSolvedVars(t *testing.T) {
 	rule := json.RawMessage(`{
 		"or":[