From d9b04617462058e290ec27934d2c80b27cf211d5 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 28 Oct 2013 18:56:23 +0200 Subject: [PATCH] using destination contains prefix method --- engine/destinations.go | 3 +-- engine/destinations_test.go | 12 ++++++------ engine/ratingprofile.go | 5 ++++- engine/storage_map.go | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/engine/destinations.go b/engine/destinations.go index 7ecdf9f70..c9b97308b 100644 --- a/engine/destinations.go +++ b/engine/destinations.go @@ -30,14 +30,13 @@ type Destination struct { Prefixes []string } -func (d *Destination) containsPrefix(prefix string) (precision int, ok bool) { +func (d *Destination) containsPrefix(prefix string) (precision int) { if d == nil { return } for _, p := range d.Prefixes { if strings.Index(prefix, p) == 0 && len(p) > precision { precision = len(p) - ok = true } } return diff --git a/engine/destinations_test.go b/engine/destinations_test.go index 90b7ebc14..09979939e 100644 --- a/engine/destinations_test.go +++ b/engine/destinations_test.go @@ -51,24 +51,24 @@ func TestDestinationStorageStore(t *testing.T) { func TestDestinationContainsPrefix(t *testing.T) { nationale := &Destination{Id: "nat", Prefixes: []string{"0257", "0256", "0723"}} - precision, ok := nationale.containsPrefix("0256") - if !ok || precision != len("0256") { + precision := nationale.containsPrefix("0256") + if precision != len("0256") { t.Error("Should contain prefix: ", nationale) } } func TestDestinationContainsPrefixLong(t *testing.T) { nationale := &Destination{Id: "nat", Prefixes: []string{"0257", "0256", "0723"}} - precision, ok := nationale.containsPrefix("0256723045") - if !ok || precision != len("0256") { + precision := nationale.containsPrefix("0256723045") + if precision != len("0256") { t.Error("Should contain prefix: ", nationale) } } func TestDestinationContainsPrefixWrong(t *testing.T) { nationale := &Destination{Id: "nat", Prefixes: []string{"0257", "0256", "0723"}} - precision, ok := nationale.containsPrefix("01234567") - if ok || precision != 0 { + precision := nationale.containsPrefix("01234567") + if precision != 0 { t.Error("Should not contain prefix: ", nationale) } } diff --git a/engine/ratingprofile.go b/engine/ratingprofile.go index c5b45863c..a1cb9d9db 100644 --- a/engine/ratingprofile.go +++ b/engine/ratingprofile.go @@ -65,6 +65,7 @@ type RatingInfo struct { RateIntervals RateIntervalList } +// TODO: what happens if there is no match for part of the call func (rp *RatingProfile) GetRatingPlansForPrefix(cd *CallDescriptor) (foundPrefixes []string, ris []*RatingInfo, err error) { rp.RatingPlanActivations.Sort() for _, rpa := range rp.RatingPlanActivations { @@ -77,11 +78,13 @@ func (rp *RatingProfile) GetRatingPlansForPrefix(cd *CallDescriptor) (foundPrefi bestPrecision := 0 var rps RateIntervalList for dId, _ := range rpl.DestinationRates { - precision, err := storageGetter.DestinationContainsPrefix(dId, cd.Destination) + //precision, err := storageGetter.DestinationContainsPrefix(dId, cd.Destination) + d, err := storageGetter.GetDestination(dId) if err != nil { Logger.Err(fmt.Sprintf("Error checking destination: %v", err)) continue } + precision := d.containsPrefix(cd.Destination) if precision > bestPrecision { bestPrecision = precision rps = rpl.RateIntervalList(dId) diff --git a/engine/storage_map.go b/engine/storage_map.go index 830d6bc33..d2d12b2b2 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -105,7 +105,7 @@ func (ms *MapStorage) DestinationContainsPrefix(key string, prefix string) (prec return 0, err } else { for _, p := range utils.SplitPrefixInterface(prefix) { - if precision, ok := d.containsPrefix(p.(string)); ok { + if precision := d.containsPrefix(p.(string)); precision > 0 { return precision, nil } }