diff --git a/engine/action.go b/engine/action.go index 824a902b6..a2fc70e3b 100644 --- a/engine/action.go +++ b/engine/action.go @@ -81,15 +81,9 @@ const ( ) func (a *Action) Clone() *Action { - return &Action{ - Id: a.Id, - ActionType: a.ActionType, - //BalanceType: a.BalanceType, - ExtraParameters: a.ExtraParameters, - ExpirationString: a.ExpirationString, - Weight: a.Weight, - Balance: a.Balance, - } + var clonedAction Action + utils.Clone(a, &clonedAction) + return &clonedAction } type actionTypeFunc func(*Account, *StatsQueueTriggered, *Action, Actions) error @@ -754,3 +748,11 @@ func (apl Actions) Less(j, i int) bool { func (apl Actions) Sort() { sort.Sort(apl) } + +func (apl *Actions) Clone() (interface{}, error) { + cln := new(Actions) + if err := utils.Clone(*apl, cln); err != nil { + return nil, err + } + return interface{}(cln), nil +} diff --git a/engine/actions_test.go b/engine/actions_test.go index e2734c52a..fa985e4e5 100644 --- a/engine/actions_test.go +++ b/engine/actions_test.go @@ -2318,6 +2318,63 @@ func TestValueFormulaDebit(t *testing.T) { } } +func TestClonedAction(t *testing.T) { + a := &Action{ + Id: "test1", + ActionType: TOPUP, + Balance: &BalanceFilter{ + ID: utils.StringPointer("*default"), + Value: &utils.ValueFormula{Static: 1}, + Type: utils.StringPointer(utils.MONETARY), + }, + Weight: float64(10), + } + + clone := a.Clone() + + if !reflect.DeepEqual(a, clone) { + t.Error("error cloning action: ", utils.ToIJSON(clone)) + } +} + +func TestClonedActions(t *testing.T) { + actions := &Actions{ + &Action{ + Id: "RECUR_FOR_V3HSILLMILLD1G", + ActionType: TOPUP, + Balance: &BalanceFilter{ + ID: utils.StringPointer("*default"), + Value: &utils.ValueFormula{Static: 1}, + Type: utils.StringPointer(utils.MONETARY), + }, + Weight: float64(30), + }, + &Action{ + Id: "RECUR_FOR_V3HSILLMILLD5G", + ActionType: DEBIT, + Balance: &BalanceFilter{ + ID: utils.StringPointer("*default"), + Value: &utils.ValueFormula{Static: 2}, + Type: utils.StringPointer(utils.MONETARY), + }, + Weight: float64(20), + }, + } + + clone, err := actions.Clone() + + if err != nil { + t.Error("error cloning actions: ", err) + } + + clonedActions := clone.(*Actions) + + if !reflect.DeepEqual(actions, clonedActions) { + t.Error("error cloning actions: ", utils.ToIJSON(clonedActions)) + } + +} + /**************** Benchmarks ********************************/ func BenchmarkUUID(b *testing.B) {