mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
using destination contains prefix method
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user