diegoholiveira / jsonlogic

Go Lang implementation of JsonLogic
MIT License
159 stars 48 forks source link

failing to check array items by index (from an empty array) #74

Closed denisazevedo closed 11 months ago

denisazevedo commented 11 months ago

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

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 returns:

image

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

diegoholiveira commented 11 months ago

Thanks for reporting it :)