From 9047fab68c6a015ce68846c9c4a31eed0f66fbc9 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Fri, 19 Mar 2021 09:45:31 +0200 Subject: [PATCH] Updated EventCost FieldAsInterface function. Fixes #2743 --- engine/account.go | 2 +- engine/balances.go | 2 +- engine/eventcost.go | 6 ++++++ engine/eventcost_test.go | 46 +++++++++++++++++++++++++++++++++++++++- engine/libeventcost.go | 42 ++++++++++++++++++------------------ engine/rateinterval.go | 2 +- 6 files changed, 75 insertions(+), 25 deletions(-) diff --git a/engine/account.go b/engine/account.go index 89d1f0f16..6844859c6 100644 --- a/engine/account.go +++ b/engine/account.go @@ -1188,7 +1188,7 @@ func (acc *Account) GetBalanceWithID(blcType, blcID string) (blc *Balance) { // FieldAsInterface func to help EventCost FieldAsInterface func (as *AccountSummary) FieldAsInterface(fldPath []string) (val interface{}, err error) { - if len(fldPath) == 0 { + if as == nil || len(fldPath) == 0 { return nil, utils.ErrNotFound } switch fldPath[0] { diff --git a/engine/balances.go b/engine/balances.go index 4b8b7a99c..52ea3b2fa 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -840,7 +840,7 @@ func (bs BalanceSummaries) BalanceSummaryWithUUD(bsUUID string) (b *BalanceSumma // FieldAsInterface func to help EventCost FieldAsInterface func (bl *BalanceSummary) FieldAsInterface(fldPath []string) (val interface{}, err error) { - if len(fldPath) != 1 { + if bl == nil || len(fldPath) != 1 { return nil, utils.ErrNotFound } switch fldPath[0] { diff --git a/engine/eventcost.go b/engine/eventcost.go index a7c82e2c5..e6e781739 100644 --- a/engine/eventcost.go +++ b/engine/eventcost.go @@ -1090,6 +1090,9 @@ func (ec *EventCost) getChargesForPath(fldPath []string, chr *ChargingInterval) } return chr.Increments, nil } + if len(chr.Increments) <= *indx { + return nil, utils.ErrNotFound + } incr := chr.Increments[*indx] if len(fldPath) == 1 { return incr, nil @@ -1119,6 +1122,9 @@ func (ec *EventCost) getRatingForPath(fldPath []string, rating *RatingUnit) (val return nil, utils.ErrNotFound } if indx != nil { + if len(rts) <= *indx { + return nil, utils.ErrNotFound + } rt := rts[*indx] if len(fldPath) == 1 { return rt, nil diff --git a/engine/eventcost_test.go b/engine/eventcost_test.go index 384c0c120..9b5649c3b 100644 --- a/engine/eventcost_test.go +++ b/engine/eventcost_test.go @@ -266,7 +266,6 @@ func TestECClone(t *testing.T) { if _, has := testEC.Accounting["a012888"]; !has { t.Error("Key removed from testEC") } - } func TestECComputeAndReset(t *testing.T) { @@ -3747,3 +3746,48 @@ func TestECAsCallCost3(t *testing.T) { } } + +func TestECAsDataProvider2(t *testing.T) { + ecDP := testEC + if data, err := ecDP.FieldAsInterface([]string{"RunID"}); err != nil { + t.Error(err) + } else if data != utils.MetaDefault { + t.Errorf("Expecting: <%s> \nreceived: <%s>", utils.MetaDefault, data) + } + if data, err := ecDP.FieldAsInterface([]string{"AccountSummary", "ID"}); err != nil { + t.Error(err) + } else if data != "dan" { + t.Errorf("Expecting: <%s> \nreceived: <%s>", "data", data) + } + if data, err := ecDP.FieldAsInterface([]string{"AccountSummary", "BalanceSummaries[1]", "UUID"}); err != nil { + t.Error(err) + } else if data != "7a54a9e9-d610-4c82-bcb5-a315b9a65010" { + t.Errorf("Expecting: <%s> \nreceived: <%s>", "4b8b53d7-c1a1-4159-b845-4623a00a0165", data) + } + if data, err := ecDP.FieldAsInterface([]string{"AccountSummary", "BalanceSummaries[2]", "Type"}); err != nil { + t.Error(err) + } else if data != "*voice" { + t.Errorf("Expecting: <%s> \nreceived: <%s>", "*voice", data) + } + ecDP = &EventCost{AccountSummary: &AccountSummary{}} + ecDP.initCache() + if _, err := ecDP.FieldAsInterface([]string{"AccountSummary", "BalanceSummaries[0]", "Type"}); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Unexpected error:%v", err) + } + if _, err := ecDP.FieldAsInterface([]string{"Charges[0]", "Increments"}); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Unexpected error:%v", err) + } + ecDP.Charges = []*ChargingInterval{{}} + if _, err := ecDP.FieldAsInterface([]string{"Charges[0]", "Increments[0]"}); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Unexpected error:%v", err) + } + ecDP.Rating = Rating{"": {}} + ecDP.Rates = ChargedRates{"": {}, "b": {}} + if _, err := ecDP.FieldAsInterface([]string{"Charges[0]", "Rating", "Rates[0]"}); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Unexpected error:%v", err) + } + + if _, err := ecDP.FieldAsInterface([]string{"Rates", "b[0]"}); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Unexpected error:%v", err) + } +} diff --git a/engine/libeventcost.go b/engine/libeventcost.go index 926edae5c..788b5668d 100644 --- a/engine/libeventcost.go +++ b/engine/libeventcost.go @@ -194,7 +194,7 @@ type BalanceCharge struct { // FieldAsInterface func to help EventCost FieldAsInterface func (bc *BalanceCharge) FieldAsInterface(fldPath []string) (val interface{}, err error) { - if len(fldPath) != 1 { + if bc == nil || len(fldPath) != 1 { return nil, utils.ErrNotFound } switch fldPath[0] { @@ -263,11 +263,11 @@ func (rf RatingMatchedFilters) Clone() (cln map[string]interface{}) { } // FieldAsInterface func to help EventCost FieldAsInterface -func (rf *RatingMatchedFilters) FieldAsInterface(fldPath []string) (val interface{}, err error) { - if len(fldPath) != 1 { +func (rf RatingMatchedFilters) FieldAsInterface(fldPath []string) (val interface{}, err error) { + if rf == nil || len(fldPath) != 1 { return nil, utils.ErrNotFound } - ct, has := (*rf)[fldPath[0]] + ct, has := rf[fldPath[0]] if !has || ct == nil { return nil, utils.ErrNotFound } @@ -300,7 +300,7 @@ func (ct *ChargedTiming) Clone() (cln *ChargedTiming) { } // FieldAsInterface func to help EventCost FieldAsInterface -func (ct *ChargedTiming) FieldAsInterface(fldPath []string) (val interface{}, err error) { +func (ct ChargedTiming) FieldAsInterface(fldPath []string) (val interface{}, err error) { if len(fldPath) != 1 { return nil, utils.ErrNotFound } @@ -352,7 +352,7 @@ func (ru *RatingUnit) Clone() (cln *RatingUnit) { } // FieldAsInterface func to help EventCost FieldAsInterface -func (ru *RatingUnit) FieldAsInterface(fldPath []string) (val interface{}, err error) { +func (ru RatingUnit) FieldAsInterface(fldPath []string) (val interface{}, err error) { if len(fldPath) != 1 { return nil, utils.ErrNotFound } @@ -407,11 +407,11 @@ func (rfs RatingFilters) Clone() (cln RatingFilters) { } // FieldAsInterface func to help EventCost FieldAsInterface -func (rfs *RatingFilters) FieldAsInterface(fldPath []string) (val interface{}, err error) { - if len(fldPath) == 0 { +func (rfs RatingFilters) FieldAsInterface(fldPath []string) (val interface{}, err error) { + if rfs == nil || len(fldPath) == 0 { return nil, utils.ErrNotFound } - ct, has := (*rfs)[fldPath[0]] + ct, has := rfs[fldPath[0]] if !has || ct == nil { return nil, utils.ErrNotFound } @@ -450,11 +450,11 @@ func (crus Rating) Clone() (cln Rating) { } // FieldAsInterface func to help EventCost FieldAsInterface -func (crus *Rating) FieldAsInterface(fldPath []string) (val interface{}, err error) { - if len(fldPath) == 0 { +func (crus Rating) FieldAsInterface(fldPath []string) (val interface{}, err error) { + if crus == nil || len(fldPath) == 0 { return nil, utils.ErrNotFound } - rt, has := (*crus)[fldPath[0]] + rt, has := crus[fldPath[0]] if !has || rt == nil { return nil, utils.ErrNotFound } @@ -468,12 +468,12 @@ func (crus *Rating) FieldAsInterface(fldPath []string) (val interface{}, err err type ChargedRates map[string]RateGroups // FieldAsInterface func to help EventCost FieldAsInterface -func (crs *ChargedRates) FieldAsInterface(fldPath []string) (val interface{}, err error) { - if len(fldPath) == 0 { +func (crs ChargedRates) FieldAsInterface(fldPath []string) (val interface{}, err error) { + if crs == nil || len(fldPath) == 0 { return nil, utils.ErrNotFound } opath, indx := utils.GetPathIndex(fldPath[0]) - cr, has := (*crs)[opath] + cr, has := crs[opath] if !has || cr == nil { return nil, utils.ErrNotFound } @@ -522,11 +522,11 @@ func (crs ChargedRates) Clone() (cln ChargedRates) { type ChargedTimings map[string]*ChargedTiming // FieldAsInterface func to help EventCost FieldAsInterface -func (cts *ChargedTimings) FieldAsInterface(fldPath []string) (val interface{}, err error) { - if len(fldPath) == 0 { +func (cts ChargedTimings) FieldAsInterface(fldPath []string) (val interface{}, err error) { + if cts == nil || len(fldPath) == 0 { return nil, utils.ErrNotFound } - ct, has := (*cts)[fldPath[0]] + ct, has := cts[fldPath[0]] if !has || ct == nil { return nil, utils.ErrNotFound } @@ -590,11 +590,11 @@ func (cbs Accounting) Clone() (cln Accounting) { } // FieldAsInterface func to help EventCost FieldAsInterface -func (cbs *Accounting) FieldAsInterface(fldPath []string) (val interface{}, err error) { - if len(fldPath) == 0 { +func (cbs Accounting) FieldAsInterface(fldPath []string) (val interface{}, err error) { + if cbs == nil || len(fldPath) == 0 { return nil, utils.ErrNotFound } - ac, has := (*cbs)[fldPath[0]] + ac, has := cbs[fldPath[0]] if !has || ac == nil { return nil, utils.ErrNotFound } diff --git a/engine/rateinterval.go b/engine/rateinterval.go index 2b8f7efb7..ab2b55f94 100644 --- a/engine/rateinterval.go +++ b/engine/rateinterval.go @@ -239,7 +239,7 @@ type RGRate struct { // FieldAsInterface func to help EventCost FieldAsInterface func (r *RGRate) FieldAsInterface(fldPath []string) (val interface{}, err error) { - if len(fldPath) != 1 { + if r == nil || len(fldPath) != 1 { return nil, utils.ErrNotFound } switch fldPath[0] {