You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When attempting to access a go struct that contains a slice, the engine will panic on Execute when index is out of range and will not emit the error back to caller.
To Reproduce
Example Rule
ruleSliceOORRule {
whenPriceSlice.Prices[4] >10// will cause panic if slice is emptythenLog("Price number 4 is greater than 10");
Retract("SliceOORRule");
}
Code
package examples
import (
"testing""github.com/hyperjumptech/grule-rule-engine/ast""github.com/hyperjumptech/grule-rule-engine/builder""github.com/hyperjumptech/grule-rule-engine/engine""github.com/hyperjumptech/grule-rule-engine/pkg""github.com/stretchr/testify/assert"
)
const (
SliceOORRule=` rule SliceOORRule { when PriceSlice.Prices[4] > 10 // will cause panic then Log("Price number 4 is greater than 10"); Retract("SliceOORRule"); }`
)
typeAUserSliceIssuestruct {
Prices []int
}
funcTestMethodCall_SliceOOR(t*testing.T) {
ps:=&AUserSliceIssue{
Prices: []int{1, 2, 3},
}
dataContext:=ast.NewDataContext()
err:=dataContext.Add("PriceSlice", ps)
assert.NoError(t, err)
// Prepare knowledgebase library and load it with our rule.lib:=ast.NewKnowledgeLibrary()
rb:=builder.NewRuleBuilder(lib)
err=rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(SliceOORRule)))
assert.NoError(t, err)
eng1:=&engine.GruleEngine{MaxCycle: 5}
kb:=lib.NewKnowledgeBaseInstance("Test", "0.1.1")
err=eng1.Execute(dataContext, kb)
assert.NoError(t, err)
}
Expected behavior
Would expect the engine to recover() from the panic and return the error back to the Execute caller.
Describe the bug
When attempting to access a go struct that contains a slice, the engine will panic on
Execute
when index is out of range and will not emit the error back to caller.To Reproduce
Example Rule
Code
Expected behavior
Would expect the engine to
recover()
from the panic and return the error back to theExecute
caller.Additional context
I will take a stab at a PR this week.
Stack Trace from Example Code
The text was updated successfully, but these errors were encountered: