From 01ae1f8dc77d37c639abcd6740d154d41d00aa4d Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Wed, 5 May 2021 17:05:54 +0300 Subject: [PATCH] FInished Equals for ExtEventChargers + little fixes in some equals --- utils/account.go | 202 ++++++++++++++++++++++++++++++++++++++++-- utils/eventcharges.go | 28 ++++-- utils/librates.go | 150 ++++++++++++++++++++++++++----- 3 files changed, 347 insertions(+), 33 deletions(-) diff --git a/utils/account.go b/utils/account.go index b92e3345e..c16629d31 100644 --- a/utils/account.go +++ b/utils/account.go @@ -110,6 +110,7 @@ type ExtAccount struct { ThresholdIDs []string } +// AsExtAccount converts Account to ExtAccount func (aC *Account) AsExtAccount() (eAc *ExtAccount, err error) { eAc = &ExtAccount{ Tenant: aC.Tenant, @@ -152,6 +153,72 @@ func (aC *Account) AsExtAccount() (eAc *ExtAccount, err error) { return } +// Equals returns the equality between two ExtAccount +func (eAc *ExtAccount) Equals(extAc *ExtAccount) (eq bool) { + if eAc.ID != extAc.ID || eAc.Tenant != extAc.Tenant { + return + } + if eAc.FilterIDs == nil && extAc.FilterIDs != nil || + eAc.FilterIDs != nil && extAc.FilterIDs == nil || + len(eAc.FilterIDs) != len(extAc.FilterIDs) { + return + } + for idx, val := range eAc.FilterIDs { + if val != extAc.FilterIDs[idx] { + return + } + } + if eAc.ActivationInterval == nil && extAc.ActivationInterval != nil || + eAc.ActivationInterval != nil && extAc.ActivationInterval == nil { + return + } + if eAc.ActivationInterval != nil && extAc.ActivationInterval != nil && + !eAc.ActivationInterval.Equals(extAc.ActivationInterval) { + return + } + if eAc.Weights == nil && extAc.Weights != nil || + eAc.Weights != nil && extAc.Weights == nil || + len(eAc.Weights) != len(extAc.Weights) { + return + } + for idx, val := range eAc.Weights { + if ok := val.Equals(extAc.Weights[idx]); !ok { + return + } + } + if eAc.Opts == nil && extAc.Opts != nil || + eAc.Opts != nil && extAc.Opts == nil || + len(eAc.Opts) != len(extAc.Opts) { + return + } + for key, val := range eAc.Opts { + if val != extAc.Opts[key] { + return + } + } + if eAc.Balances == nil && extAc.Balances != nil || + eAc.Balances != nil && extAc.Balances == nil || + len(eAc.Balances) != len(extAc.Balances) { + return + } + for key, val := range eAc.Balances { + if ok := val.Equals(extAc.Balances[key]); !ok { + return + } + } + if eAc.ThresholdIDs == nil && extAc.ThresholdIDs != nil || + eAc.ThresholdIDs != nil && extAc.ThresholdIDs == nil || + len(eAc.ThresholdIDs) != len(extAc.ThresholdIDs) { + return + } + for idx, val := range eAc.ThresholdIDs { + if val != extAc.ThresholdIDs[idx] { + return + } + } + return true +} + // Balance represents one Balance inside an Account type Balance struct { ID string // Balance identificator, unique within an Account @@ -179,6 +246,7 @@ type ExtBalance struct { RateProfileIDs []string } +// AsExtBalance converts Balance to ExtBalance func (bL *Balance) AsExtBalance() (eBl *ExtBalance, err error) { eBl = &ExtBalance{ ID: bL.ID, @@ -241,6 +309,90 @@ func (bL *Balance) AsExtBalance() (eBl *ExtBalance, err error) { return } +// Equals returns the equality between two ExtBalance +func (eBL *ExtBalance) Equals(extBl *ExtBalance) (eq bool) { + if eBL.ID != extBl.ID || eBL.Type != extBl.Type { + return + } + if eBL.FilterIDs == nil && extBl.FilterIDs != nil || + eBL.FilterIDs != nil && extBl.FilterIDs == nil || + len(eBL.FilterIDs) != len(extBl.FilterIDs) { + return + } + for i, val := range eBL.FilterIDs { + if val != extBl.FilterIDs[i] { + return + } + } + if eBL.Weights == nil && extBl.Weights != nil || + eBL.Weights != nil && extBl.Weights == nil || + len(eBL.Weights) != len(extBl.Weights) { + return + } + for idx, val := range eBL.Weights { + if ok := val.Equals(extBl.Weights[idx]); !ok { + return + } + } + if eBL.Units == nil && extBl.Units != nil || + eBL.Units != nil && extBl.Units == nil || + eBL.Units != extBl.Units { + return + } + if eBL.UnitFactors == nil && extBl.UnitFactors != nil || + eBL.UnitFactors != nil && extBl.UnitFactors == nil || + len(eBL.UnitFactors) != len(extBl.UnitFactors) { + return + } + for idx, val := range eBL.UnitFactors { + if ok := val.Equals(extBl.UnitFactors[idx]); !ok { + return + } + } + if eBL.Opts == nil && extBl.Opts != nil || + eBL.Opts != nil && extBl.Opts == nil || + len(eBL.Opts) != len(extBl.Opts) { + return + } + for key, val := range eBL.Opts { + if val != extBl.Opts[key] { + return + } + } + if eBL.CostIncrements == nil && extBl.CostIncrements != nil || + eBL.CostIncrements != nil && extBl.CostIncrements == nil || + len(eBL.CostIncrements) != len(extBl.CostIncrements) { + return + } + for idx, val := range eBL.CostIncrements { + if ok := val.Equals(extBl.CostIncrements[idx]); !ok { + return + } + } + if eBL.AttributeIDs == nil && extBl.AttributeIDs != nil || + eBL.AttributeIDs != nil && extBl.AttributeIDs == nil || + len(eBL.AttributeIDs) != len(extBl.AttributeIDs) { + return + } + for i, val := range eBL.AttributeIDs { + if val != extBl.AttributeIDs[i] { + return + } + } + if eBL.RateProfileIDs == nil && extBl.RateProfileIDs != nil || + eBL.RateProfileIDs != nil && extBl.RateProfileIDs == nil || + len(eBL.RateProfileIDs) != len(extBl.RateProfileIDs) { + return + } + for i, val := range eBL.RateProfileIDs { + if val != extBl.RateProfileIDs[i] { + return + } + } + return true +} + +// Equals returns the equality between two Balance func (bL *Balance) Equals(bal *Balance) (eq bool) { if bL.ID != bal.ID || bL.Type != bal.Type { return @@ -338,6 +490,30 @@ type ExtCostIncrement struct { RecurrentFee *float64 } +// Equals returns the equality between two ExtCostIncrement +func (eCi *ExtCostIncrement) Equals(extCi *ExtCostIncrement) (eq bool) { + if eCi.FilterIDs == nil && extCi.FilterIDs != nil || + eCi.FilterIDs != nil && extCi.FilterIDs == nil || + len(eCi.FilterIDs) != len(extCi.FilterIDs) { + return + } + for idx, val := range eCi.FilterIDs { + if val != extCi.FilterIDs[idx] { + return + } + } + if eCi.Increment == nil && extCi.Increment != nil || + eCi.Increment != nil && extCi.Increment == nil || + eCi.FixedFee == nil && extCi.FixedFee != nil || + eCi.FixedFee != nil && extCi.FixedFee == nil || + eCi.RecurrentFee == nil && extCi.RecurrentFee != nil || + eCi.RecurrentFee != nil && extCi.RecurrentFee == nil { + return + } + return true +} + +// AsExtCostIncrement converts CostIncrement to ExtCostIncrement func (cI *CostIncrement) AsExtCostIncrement() (eCi *ExtCostIncrement, err error) { eCi = new(ExtCostIncrement) if cI.FilterIDs != nil { @@ -390,9 +566,19 @@ func (cI *CostIncrement) Equals(ctIn *CostIncrement) (eq bool) { cI.FixedFee != nil && ctIn.FixedFee == nil { return } - return cI.Increment.Compare(ctIn.Increment) == 0 && - cI.FixedFee.Compare(ctIn.FixedFee) == 0 && - cI.RecurrentFee.Compare(ctIn.RecurrentFee) == 0 + if cI.Increment != nil && ctIn.Increment != nil && + cI.Increment.Compare(ctIn.Increment) != 0 { + return + } + if cI.FixedFee != nil && ctIn.FixedFee != nil && + cI.FixedFee.Compare(ctIn.FixedFee) != 0 { + return + } + if cI.RecurrentFee != nil && ctIn.RecurrentFee != nil && + cI.RecurrentFee.Compare(ctIn.RecurrentFee) != 0 { + return + } + return true } // Clone returns a copy of the CostIncrement @@ -421,6 +607,7 @@ type ExtUnitFactor struct { Factor *float64 } +// AsExtUnitFactor converts UnitFactor to ExtUnitFactor func (uF *UnitFactor) AsExtUnitFactor() (eUf *ExtUnitFactor, err error) { eUf = new(ExtUnitFactor) if uF.FilterIDs != nil { @@ -482,8 +669,8 @@ func (uF *UnitFactor) Equals(nUf *UnitFactor) (eq bool) { len(uF.FilterIDs) != len(nUf.FilterIDs) { return } - for i := range uF.FilterIDs { - if uF.FilterIDs[i] != nUf.FilterIDs[i] { + for idx, val := range uF.FilterIDs { + if val != nUf.FilterIDs[idx] { return } } @@ -519,10 +706,11 @@ func (aC *Account) Equals(acnt *Account) (eq bool) { } } if aC.ActivationInterval == nil && acnt.ActivationInterval != nil || - aC.ActivationInterval != nil && acnt.ActivationInterval != nil { + aC.ActivationInterval != nil && acnt.ActivationInterval == nil { return } - if ok := aC.ActivationInterval.Equals(acnt.ActivationInterval); !ok { + if aC.ActivationInterval != nil && acnt.ActivationInterval != nil && + !aC.ActivationInterval.Equals(acnt.ActivationInterval) { return } if aC.Weights == nil && acnt.Weights != nil || diff --git a/utils/eventcharges.go b/utils/eventcharges.go index 60d5c7a4b..133d83f43 100644 --- a/utils/eventcharges.go +++ b/utils/eventcharges.go @@ -218,6 +218,7 @@ func (ec *EventCharges) AsExtEventCharges() (eEc *ExtEventCharges, err error) { return } +// Equals returns the equality between two ExtEventCharges func (eEc *ExtEventCharges) Equals(exCh *ExtEventCharges) (eq bool) { if eEc.Abstracts != exCh.Abstracts || eEc.Concretes != exCh.Concretes { @@ -263,16 +264,32 @@ func (eEc *ExtEventCharges) Equals(exCh *ExtEventCharges) (eq bool) { return } } + if eEc.Accounts == nil && exCh.Accounts != nil || + eEc.Accounts != nil && exCh.Accounts == nil || + len(eEc.Rating) != len(exCh.Rating) { + return + } + for key, val := range eEc.Accounts { + if ok := val.Equals(exCh.Accounts[key]); !ok { + return + } + } return true } -// Equals returns the equality between two EventChargers +// Equals returns the equality between two EventCharges func (eC *EventCharges) Equals(evCh *EventCharges) (eq bool) { if eC.Abstracts == nil && evCh.Abstracts != nil || eC.Abstracts != nil && evCh.Abstracts == nil || eC.Concretes == nil && evCh.Concretes != nil || - eC.Concretes != nil && evCh.Concretes == nil || - eC.Abstracts.Compare(evCh.Abstracts) != 0 || + eC.Concretes != nil && evCh.Concretes == nil { + return + } + if eC.Abstracts != nil && evCh.Abstracts != nil && + eC.Abstracts.Compare(evCh.Abstracts) != 0 { + return + } + if eC.Concretes != nil && evCh.Concretes != nil && eC.Concretes.Compare(evCh.Concretes) != 0 { return } @@ -401,6 +418,7 @@ type ExtAccountCharge struct { JoinedChargeIDs []string // identificator of extra account charges } +// AsExtAccountCharge converts AccountCharge to ExtAccountCharge func (aC *AccountCharge) AsExtAccountCharge() (eAc *ExtAccountCharge, err error) { eAc = &ExtAccountCharge{ AccountID: aC.AccountID, @@ -487,8 +505,8 @@ func (ac *AccountCharge) Equals(nAc *AccountCharge) (eq bool) { len(ac.AttributeIDs) != len(nAc.AttributeIDs) { return } - for i := range ac.AttributeIDs { - if ac.AttributeIDs[i] != nAc.AttributeIDs[i] { + for idx, val := range ac.AttributeIDs { + if val != nAc.AttributeIDs[idx] { return } } diff --git a/utils/librates.go b/utils/librates.go index 28534d049..3a8496394 100644 --- a/utils/librates.go +++ b/utils/librates.go @@ -80,6 +80,49 @@ type ExtRate struct { uID string } +func (rT *Rate) Equals(rte *Rate) (eq bool) { + if rT.ID != rte.ID || + rT.ActivationTimes != rte.ActivationTimes || + rT.Blocker != rte.Blocker { + return + } + if rT.FilterIDs == nil && rte.FilterIDs != nil || + rT.FilterIDs != nil && rte.FilterIDs == nil { + return + } + for idx, val := range rT.FilterIDs { + if val != rte.FilterIDs[idx] { + return + } + } + if rT.Weights == nil && rte.Weights != nil || + rT.Weights != nil && rte.Weights == nil || + len(rT.Weights) != len(rte.Weights) { + return + } + if rT.Weights != nil && rte.Weights != nil { + for idx, val := range rT.Weights { + if ok := val.Equals(rte.Weights[idx]); !ok { + return + } + } + } + if rT.IntervalRates == nil && rte.IntervalRates != nil || + rT.IntervalRates != nil && rte.IntervalRates == nil || + len(rT.IntervalRates) != len(rte.IntervalRates) { + return + } + if rT.IntervalRates != nil && rte.IntervalRates != nil { + for idx, val := range rT.IntervalRates { + if ok := val.Equals(rte.IntervalRates[idx]); !ok { + return + } + } + } + return true +} + +// AsExtRate converts Rate to ExtRate func (rT *Rate) AsExtRate() (eRt *ExtRate, err error) { eRt = &ExtRate{ ID: rT.ID, @@ -131,6 +174,7 @@ type ExtIntervalRate struct { Increment *float64 // RateIncrement } +// AsExtIntervalRate converts IntervalRate to ExtIntervalRate func (iR *IntervalRate) AsExtIntervalRate() (eIr *ExtIntervalRate, err error) { eIr = new(ExtIntervalRate) if iR.IntervalStart != nil { @@ -171,6 +215,42 @@ func (iR *IntervalRate) AsExtIntervalRate() (eIr *ExtIntervalRate, err error) { return } +func (iR *IntervalRate) Equals(inRt *IntervalRate) (eq bool) { + if iR.RecurrentFee == nil && inRt.RecurrentFee != nil || + iR.RecurrentFee != nil && inRt.RecurrentFee == nil || + iR.FixedFee == nil && inRt.FixedFee != nil || + iR.FixedFee != nil && inRt.FixedFee == nil || + iR.Increment == nil && inRt.Increment != nil || + iR.Increment != nil && inRt.Increment == nil || + iR.Unit == nil && inRt.Unit != nil || + iR.Unit != nil && inRt.Unit == nil || + iR.IntervalStart == nil && inRt.IntervalStart != nil || + iR.IntervalStart != nil && inRt.IntervalStart == nil { + return + } + if iR.RecurrentFee != nil && inRt.RecurrentFee != nil && + iR.RecurrentFee.Compare(inRt.RecurrentFee) != 0 { + return + } + if iR.FixedFee != nil && inRt.FixedFee != nil && + iR.FixedFee.Compare(inRt.FixedFee) != 0 { + return + } + if iR.Increment != nil && inRt.Increment != nil && + iR.Increment.Compare(inRt.Increment) != 0 { + return + } + if iR.Unit != nil && inRt.Unit != nil && + iR.Unit.Compare(inRt.Unit) != 0 { + return + } + if iR.IntervalStart != nil && inRt.IntervalStart != nil && + iR.IntervalStart.Compare(inRt.IntervalStart) != 0 { + return + } + return true +} + func (rt *Rate) Compile() (err error) { aTime := rt.ActivationTimes if aTime == EmptyString { @@ -229,6 +309,7 @@ type ExtRateSInterval struct { cost *float64 // unexported total interval cost } +// AsExtRateSInterval converts RateSInterval to ExtRateSInterval func (rI *RateSInterval) AsExtRateSInterval() (eRi *ExtRateSInterval, err error) { eRi = &ExtRateSInterval{ CompressFactor: rI.CompressFactor, @@ -263,15 +344,16 @@ func (rI *RateSInterval) AsExtRateSInterval() (eRi *ExtRateSInterval, err error) // Equals compares two ExtRateSInterval func (rIl *ExtRateSInterval) Equals(nRil *ExtRateSInterval) (eq bool) { if rIl.IntervalStart == nil && nRil.IntervalStart != nil || - rIl.IntervalStart != nil && nRil.IntervalStart == nil || - rIl.cost == nil && nRil.cost != nil || - rIl.cost != nil && nRil.cost == nil || - len(rIl.Increments) != len(nRil.Increments) { + rIl.IntervalStart != nil && nRil.IntervalStart == nil { return } if rIl.IntervalStart != nRil.IntervalStart || - rIl.CompressFactor != nRil.CompressFactor || - rIl.cost != nRil.cost { + rIl.CompressFactor != nRil.CompressFactor { + return + } + if rIl.Increments == nil && nRil.Increments != nil || + rIl.Increments != nil && nRil.Increments == nil || + len(rIl.Increments) != len(nRil.Increments) { return } for i, rtIn := range rIl.Increments { @@ -302,6 +384,7 @@ type ExtRateSIncrement struct { cost *float64 // unexported total increment cost } +// AsExtRateSIncrement converts RateSIncrement to ExtRateSIncrement func (rI *RateSIncrement) AsExtRateSIncrement() (eRi *ExtRateSIncrement, err error) { eRi = &ExtRateSIncrement{ IntervalRateIndex: rI.IntervalRateIndex, @@ -357,17 +440,23 @@ func (eRI *ExtRateSIncrement) Equals(extRI *ExtRateSIncrement) (eq bool) { // Equals compares two RateSIntervals func (rIl *RateSInterval) Equals(nRil *RateSInterval) (eq bool) { if rIl.IntervalStart == nil && nRil.IntervalStart != nil || - rIl.IntervalStart != nil && nRil.IntervalStart == nil || + rIl.IntervalStart != nil && nRil.IntervalStart == nil { + return + } + if rIl.IntervalStart != nil && nRil.IntervalStart != nil && + rIl.IntervalStart.Compare(nRil.IntervalStart) != 0 { + return + } + if rIl.Increments != nil && rIl.Increments == nil || + rIl.Increments == nil && nRil.Increments != nil || len(rIl.Increments) != len(nRil.Increments) { return } - if rIl.IntervalStart.Compare(nRil.IntervalStart) != 0 || - rIl.CompressFactor != nRil.CompressFactor { - return - } - for i, rtIn := range rIl.Increments { - if !rtIn.Equals(nRil.Increments[i]) { - return + if rIl.Increments != nil && nRil.Increments != nil { + for i, rtIn := range rIl.Increments { + if !rtIn.Equals(nRil.Increments[i]) { + return + } } } return true @@ -376,16 +465,32 @@ func (rIl *RateSInterval) Equals(nRil *RateSInterval) (eq bool) { // Equals returns the equality between two RateSIncrement func (rI *RateSIncrement) Equals(rtIn *RateSIncrement) (eq bool) { if rI.Usage == nil && rtIn.Usage != nil || - rI.Usage != nil && rtIn.Usage == nil || - rI.IncrementStart == nil && rtIn.IncrementStart != nil || + rI.Usage != nil && rtIn.Usage == nil { + return + } + if rI.Usage != nil && rtIn.Usage != nil && + rI.Usage.Compare(rtIn.Usage) != 0 { + return + } + if rI.IncrementStart == nil && rtIn.IncrementStart != nil || rI.IncrementStart != nil && rtIn.IncrementStart == nil { return } - return rI.Usage.Compare(rtIn.Usage) == 0 && - rI.IncrementStart.Compare(rtIn.IncrementStart) == 0 && - rI.CompressFactor == rtIn.CompressFactor && - rI.IntervalRateIndex == rtIn.IntervalRateIndex && - rI.Rate.UID() == rtIn.Rate.UID() + if rI.IncrementStart != nil && rtIn.IncrementStart != nil && + rI.IncrementStart.Compare(rtIn.IncrementStart) != 0 { + return + } + if rI.Rate == nil && rtIn.Rate != nil || + rI.Rate != nil && rtIn.Rate == nil { + return + } + if rI.Rate != nil && rtIn.Rate != nil { + if ok := rI.Rate.Equals(rtIn.Rate); !ok { + return + } + } + return rI.CompressFactor == rtIn.CompressFactor && + rI.IntervalRateIndex == rtIn.IntervalRateIndex } // RateProfileCost is the cost returned by RateS at cost queries @@ -491,6 +596,7 @@ func CostForIntervals(rtIvls []*RateSInterval) (cost *decimal.Big) { func CompressIntervals(rtIvls []*RateSInterval) { } +// AsRateProfile converts APIRateProfile to RateProfile func (ext *APIRateProfile) AsRateProfile() (rp *RateProfile, err error) { rp = &RateProfile{ Tenant: ext.Tenant, @@ -537,6 +643,7 @@ type APIRateProfile struct { APIOpts map[string]interface{} } +// AsRate converts APIRate to Rate func (ext *APIRate) AsRate() (rate *Rate, err error) { rate = &Rate{ ID: ext.ID, @@ -569,6 +676,7 @@ type APIRate struct { IntervalRates []*APIIntervalRate } +// AsIntervalRate converts APIIntervalRate to IntervalRate func (ext *APIIntervalRate) AsIntervalRate() (iRate *IntervalRate, err error) { iRate = new(IntervalRate) if iRate.IntervalStart, err = NewDecimalFromUsage(ext.IntervalStart); err != nil {