using destination contains prefix method

This commit is contained in:
Radu Ioan Fericean
2013-10-28 18:56:23 +02:00
parent 566447a0a0
commit d9b0461746
4 changed files with 12 additions and 10 deletions

View File

@@ -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

View File

@@ -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)
}
}

View File

@@ -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)

View File

@@ -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
}
}