mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
New method for dynamicWight converting into string
This commit is contained in:
committed by
Dan Christian Bogos
parent
7c02a8ec14
commit
28a0911832
@@ -25,7 +25,7 @@ import (
|
||||
)
|
||||
|
||||
// NewDynamicWeightsFromString creates a DynamicWeight list based on the string received from .csv/StorDB
|
||||
func NewDynamicWeightsFromString(s, dWSep, fltrSep string) (dWs []*DynamicWeight, err error) {
|
||||
func NewDynamicWeightsFromString(s, dWSep, fltrSep string) (dWs DynamicWeights, err error) {
|
||||
nrFlds := 2
|
||||
dwStrs := strings.Split(s, dWSep)
|
||||
lnDwStrs := len(dwStrs)
|
||||
@@ -49,8 +49,49 @@ func NewDynamicWeightsFromString(s, dWSep, fltrSep string) (dWs []*DynamicWeight
|
||||
return
|
||||
}
|
||||
|
||||
type DynamicWeights []*DynamicWeight
|
||||
|
||||
func (dWS DynamicWeights) String(dWSep, fltrsep string) (out string) {
|
||||
if len(dWS) == 0 {
|
||||
return
|
||||
}
|
||||
dwToString := make([]string, len(dWS))
|
||||
for i, value := range dWS {
|
||||
dwToString[i] = value.String(dWSep, fltrsep)
|
||||
}
|
||||
return strings.Join(dwToString, dWSep)
|
||||
}
|
||||
|
||||
func (dW DynamicWeight) String(dWSep, fltrsep string) (out string) {
|
||||
return strings.Join(dW.FilterIDs, fltrsep) + dWSep + strconv.FormatFloat(dW.Weight, 'f', -1, 64)
|
||||
}
|
||||
|
||||
// DynamicWeight returns Weight based on Filters
|
||||
type DynamicWeight struct {
|
||||
FilterIDs []string
|
||||
Weight float64
|
||||
}
|
||||
|
||||
func (dW *DynamicWeight) Clone() (dinWeight *DynamicWeight) {
|
||||
dinWeight = &DynamicWeight{
|
||||
Weight: dW.Weight,
|
||||
}
|
||||
if dW.FilterIDs != nil {
|
||||
dinWeight.FilterIDs = make([]string, len(dW.FilterIDs))
|
||||
for i, value := range dW.FilterIDs {
|
||||
dinWeight.FilterIDs[i] = value
|
||||
}
|
||||
}
|
||||
return dinWeight
|
||||
}
|
||||
|
||||
func (dW DynamicWeights) Clone() (dinWeight DynamicWeights) {
|
||||
if dW == nil {
|
||||
return
|
||||
}
|
||||
dinWeight = make(DynamicWeights, len(dW))
|
||||
for i, value := range dW {
|
||||
dinWeight[i] = value.Clone()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ import (
|
||||
)
|
||||
|
||||
func TestNewDynamicWeightsFromString(t *testing.T) {
|
||||
eDws := []*DynamicWeight{
|
||||
eDws := DynamicWeights{
|
||||
{
|
||||
FilterIDs: []string{"fltr1", "fltr2"},
|
||||
Weight: 20.0,
|
||||
@@ -41,9 +41,9 @@ func TestNewDynamicWeightsFromString(t *testing.T) {
|
||||
if dws, err := NewDynamicWeightsFromString(dwsStr, ";", "&"); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(eDws, dws) {
|
||||
t.Errorf("expecting: %+v, received: %+v", eDws, dws)
|
||||
t.Errorf("expecting: %+v, received: %+v", ToJSON(eDws), ToJSON(dws))
|
||||
}
|
||||
eDws = []*DynamicWeight{
|
||||
eDws = DynamicWeights{
|
||||
{
|
||||
FilterIDs: []string{"fltr1", "fltr2"},
|
||||
Weight: 20.0,
|
||||
@@ -62,7 +62,7 @@ func TestNewDynamicWeightsFromString(t *testing.T) {
|
||||
} else if !reflect.DeepEqual(eDws[1], dws[1]) {
|
||||
t.Errorf("expecting: %+v, received: %+v", eDws[1], dws[1])
|
||||
}
|
||||
eDws = []*DynamicWeight{
|
||||
eDws = DynamicWeights{
|
||||
{
|
||||
Weight: 20.0,
|
||||
},
|
||||
@@ -73,7 +73,7 @@ func TestNewDynamicWeightsFromString(t *testing.T) {
|
||||
} else if !reflect.DeepEqual(eDws, dws) {
|
||||
t.Errorf("expecting: %+v, received: %+v", eDws, dws)
|
||||
}
|
||||
eDws = []*DynamicWeight{
|
||||
eDws = DynamicWeights{
|
||||
{
|
||||
Weight: 0.0,
|
||||
},
|
||||
@@ -84,5 +84,4 @@ func TestNewDynamicWeightsFromString(t *testing.T) {
|
||||
} else if !reflect.DeepEqual(eDws, dws) {
|
||||
t.Errorf("expecting: %+v, received: %+v", eDws, dws)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user