hyperjumptech/grule-rule-engine

Mutable Value issues

hieubui2409 opened this issue · 3 comments

I have a fact with struct like below:

type MyFact struct {
	Data  []*OrderInfo
	First *OrderInfo
	Last  *OrderInfo

	MutableVar1 string
        MutableVar2 []string
        MutableVar3 []*EvaluationHistoryItem
}

type EvaluationHistoryItem struct {
	EvaluatedBy  string
	EvaluatedAt  int64
	ExchangeRate float64
}

When I try to change value (assign or append value to slice) MutableVar1/MutableVar2/MutableVar3 by both builtin and custom functions, It didn't work.
But when I change my fact struct to (Put all fields need to be mutable inside another object):

type MyFact struct {
	Data  []*OrderInfo
	First *OrderInfo
	Last  *OrderInfo
	Evaluation *EvaluationInfo
}

type EvaluationInfo struct {
	MutableVar1 string
        MutableVar2 []string
        MutableVar3 []*EvaluationHistoryItem
}

type EvaluationHistoryItem struct {
	EvaluatedBy  string
	EvaluatedAt  int64
	ExchangeRate float64
}

I can update value of MutableVar1/MutableVar2/MutableVar3 normally.
Tell me if I did anything wrong. Thank you.

Meet the same problem

can you share rule, How you are trying to manipulate the values? Thanks.

Most likely you added the struct to the DataContext by value instead of by reference. If you do so and try to modify a value you'll likely see an error along the lines of got rule engine execute panic ! recovered : reflect: call of reflect.Value.Elem on struct Value.