From cb3531d61b311237de6c6ae0d41af150a03af2f8 Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Mon, 14 Feb 2022 12:03:23 +0200 Subject: [PATCH] Add clone function for EventCharges --- utils/account.go | 15 ++++---- utils/eventcharges.go | 80 +++++++++++++++++++++++++++++++++++++++++++ utils/librates.go | 62 +++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+), 9 deletions(-) diff --git a/utils/account.go b/utils/account.go index 35b553a33..eba40e62b 100644 --- a/utils/account.go +++ b/utils/account.go @@ -237,19 +237,16 @@ func (cI *CostIncrement) Clone() (cIcln *CostIncrement) { return } -// Clone return a copy of the UnitFactor -func (uF *UnitFactor) Clone() (untFct *UnitFactor) { - untFct = new(UnitFactor) +// Clone returns a copy of uF +func (uF *UnitFactor) Clone() *UnitFactor { + cln := new(UnitFactor) if uF.FilterIDs != nil { - untFct.FilterIDs = make([]string, len(uF.FilterIDs)) - for i, value := range uF.FilterIDs { - untFct.FilterIDs[i] = value - } + cln.FilterIDs = CloneStringSlice(uF.FilterIDs) } if uF.Factor != nil { - untFct.Factor = uF.Factor.Clone() + cln.Factor = uF.Factor.Clone() } - return + return cln } // UnitFactor is a multiplicator for the usage received diff --git a/utils/eventcharges.go b/utils/eventcharges.go index ff23bc1b3..9dd3787ae 100644 --- a/utils/eventcharges.go +++ b/utils/eventcharges.go @@ -44,12 +44,69 @@ type EventCharges struct { Accounts map[string]*Account } +// Clone returns a copy of ec +func (ec *EventCharges) Clone() *EventCharges { + cln := new(EventCharges) + if ec.Abstracts != nil { + cln.Abstracts = ec.Abstracts.Clone() + } + if ec.Concretes != nil { + cln.Concretes = ec.Concretes.Clone() + } + if ec.Charges != nil { + cln.Charges = make([]*ChargeEntry, len(ec.Charges)) + for i, value := range ec.Charges { + cln.Charges[i] = value.Clone() + } + } + if ec.Accounting != nil { + cln.Accounting = make(map[string]*AccountCharge) + for key, value := range ec.Accounting { + cln.Accounting[key] = value.Clone() + } + } + if ec.UnitFactors != nil { + cln.UnitFactors = make(map[string]*UnitFactor) + for key, value := range ec.UnitFactors { + cln.UnitFactors[key] = value.Clone() + } + } + if ec.Rating != nil { + cln.Rating = make(map[string]*RateSInterval) + for key, value := range ec.Rating { + cln.Rating[key] = value.Clone() + } + } + if ec.Rates != nil { + cln.Rates = make(map[string]*IntervalRate) + for key, value := range ec.Rates { + cln.Rates[key] = value.Clone() + } + } + if ec.Accounts != nil { + cln.Accounts = make(map[string]*Account) + for key, value := range ec.Accounts { + cln.Accounts[key] = value.Clone() + } + } + + return cln +} + // ChargeEntry is a reference towards Accounting or Rating ID (depending on request type) type ChargeEntry struct { ChargingID string CompressFactor int } +// Clone returns a copy of ce +func (ce *ChargeEntry) Clone() *ChargeEntry { + return &ChargeEntry{ + ChargingID: ce.ChargingID, + CompressFactor: ce.CompressFactor, + } +} + // Merge will merge the event charges into existing func (ec *EventCharges) Merge(eCs ...*EventCharges) { //ec.SyncIDs(eCs...) // so we can compare properly @@ -258,6 +315,29 @@ type AccountCharge struct { JoinedChargeIDs []string // identificator of extra account charges } +// Clone returns a copy of ac +func (ac *AccountCharge) Clone() *AccountCharge { + cln := &AccountCharge{ + AccountID: ac.AccountID, + BalanceID: ac.BalanceID, + UnitFactorID: ac.UnitFactorID, + RatingID: ac.RatingID, + } + if ac.Units != nil { + cln.Units = ac.Units.Clone() + } + if ac.BalanceLimit != nil { + cln.BalanceLimit = ac.BalanceLimit.Clone() + } + if ac.AttributeIDs != nil { + cln.AttributeIDs = CloneStringSlice(ac.AttributeIDs) + } + if ac.JoinedChargeIDs != nil { + cln.JoinedChargeIDs = CloneStringSlice(ac.JoinedChargeIDs) + } + return cln +} + // Equals compares two AccountCharges func (ac *AccountCharge) equals(nAc *AccountCharge) (eq bool) { if ac == nil && nAc == nil { diff --git a/utils/librates.go b/utils/librates.go index 0ff8c1062..1fc0b88d7 100644 --- a/utils/librates.go +++ b/utils/librates.go @@ -81,6 +81,27 @@ type IntervalRate struct { Increment *Decimal // RateIncrement } +// Clone returns a copy of iR +func (iR *IntervalRate) Clone() *IntervalRate { + cln := new(IntervalRate) + if iR.IntervalStart != nil { + cln.IntervalStart = iR.IntervalStart.Clone() + } + if iR.FixedFee != nil { + cln.FixedFee = iR.FixedFee.Clone() + } + if iR.RecurrentFee != nil { + cln.RecurrentFee = iR.RecurrentFee.Clone() + } + if iR.Unit != nil { + cln.Unit = iR.Unit.Clone() + } + if iR.Increment != nil { + cln.Increment = iR.Increment.Clone() + } + return cln +} + // Equals returns the equality between two IntervalRate func (iR *IntervalRate) Equals(inRt *IntervalRate) (eq bool) { if iR == nil && inRt == nil { @@ -165,6 +186,27 @@ type RateSInterval struct { cost *decimal.Big // unexported total interval cost } +// Clone returns a copy of rI +func (rI *RateSInterval) Clone() *RateSInterval { + cln := &RateSInterval{ + CompressFactor: rI.CompressFactor, + } + if rI.IntervalStart != nil { + cln.IntervalStart = rI.IntervalStart.Clone() + } + if rI.Increments != nil { + cln.Increments = make([]*RateSIncrement, len(rI.Increments)) + for i, value := range rI.Increments { + cln.Increments[i] = value.Clone() + } + } + if rI.cost != nil { + tmp := &decimal.Big{} + cln.cost = tmp.Copy(rI.cost) + } + return cln +} + // AsRatesIntervalsCost converts RateSInterval to RateSIntervalCost // The difference between this 2 is that RateSIntervalCost don't need IntervalStart func (rI *RateSInterval) AsRatesIntervalsCost() (rIc *RateSIntervalCost) { @@ -190,6 +232,26 @@ type RateSIncrement struct { cost *decimal.Big // unexported total increment cost } +// Clone returns a copy of rI +func (rI *RateSIncrement) Clone() *RateSIncrement { + cln := &RateSIncrement{ + RateIntervalIndex: rI.RateIntervalIndex, + RateID: rI.RateID, + CompressFactor: rI.CompressFactor, + } + if rI.IncrementStart != nil { + cln.IncrementStart = rI.IncrementStart.Clone() + } + if rI.Usage != nil { + cln.Usage = rI.Usage.Clone() + } + if rI.cost != nil { + tmp := &decimal.Big{} + cln.cost = tmp.Copy(rI.cost) + } + return cln +} + // Equals compares two RateSIntervals func (rIl *RateSInterval) Equals(nRil *RateSInterval, rIlRef, nRilRef map[string]*IntervalRate) (eq bool) { if rIl == nil && nRil == nil {