mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-16 13:49:53 +05:00
Merging old ParseRSRFields from config with new one in utils
This commit is contained in:
@@ -386,7 +386,7 @@ func loadConfig(c *conf.ConfigFile) (*CGRConfig, error) {
|
||||
}
|
||||
if hasOpt = c.HasOption("cdrs", "extra_fields"); hasOpt {
|
||||
extraFieldsStr, _ := c.GetString("cdrs", "extra_fields")
|
||||
if extraFields, err := ParseRSRFields(extraFieldsStr); err != nil {
|
||||
if extraFields, err := utils.ParseRSRFields(extraFieldsStr, utils.FIELDS_SEP); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
cfg.CDRSExtraFields = extraFields
|
||||
|
||||
@@ -39,22 +39,6 @@ func ConfigSlice(cfgVal string) ([]string, error) {
|
||||
return cfgValStrs, nil
|
||||
}
|
||||
|
||||
func ParseRSRFields(configVal string) ([]*utils.RSRField, error) { //ToDo: Unify it with the Parser inside RSRField
|
||||
cfgValStrs := strings.Split(configVal, string(utils.CSV_SEP))
|
||||
if len(cfgValStrs) == 1 && cfgValStrs[0] == "" { // Prevents returning iterable with empty value
|
||||
return []*utils.RSRField{}, nil
|
||||
}
|
||||
rsrFields := make([]*utils.RSRField, len(cfgValStrs))
|
||||
for idx, cfgValStr := range cfgValStrs {
|
||||
if rsrField, err := utils.NewRSRField(cfgValStr); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
rsrFields[idx] = rsrField
|
||||
}
|
||||
}
|
||||
return rsrFields, nil
|
||||
}
|
||||
|
||||
// Parse the configuration file and returns utils.DerivedChargers instance if no errors
|
||||
func ParseCfgDerivedCharging(c *conf.ConfigFile) (dcs utils.DerivedChargers, err error) {
|
||||
var runIds, runFilters, reqTypeFlds, directionFlds, tenantFlds, torFlds, acntFlds, subjFlds, dstFlds, sTimeFlds, aTimeFlds, durFlds []string
|
||||
|
||||
@@ -20,7 +20,6 @@ package config
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
@@ -35,19 +34,6 @@ func TestConfigSlice(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRSRFields(t *testing.T) {
|
||||
fields := `host,~sip_redirected_to:s/sip:\+49(\d+)@/0$1/,destination`
|
||||
expectParsedFields := []*utils.RSRField{&utils.RSRField{Id: "host"},
|
||||
&utils.RSRField{Id: "sip_redirected_to",
|
||||
RSRules: []*utils.ReSearchReplace{&utils.ReSearchReplace{SearchRegexp: regexp.MustCompile(`sip:\+49(\d+)@`), ReplaceTemplate: "0$1"}}},
|
||||
&utils.RSRField{Id: "destination"}}
|
||||
if parsedFields, err := ParseRSRFields(fields); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if !reflect.DeepEqual(parsedFields, expectParsedFields) {
|
||||
t.Errorf("Unexpected value of parsed fields")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCfgDerivedCharging(t *testing.T) {
|
||||
eFieldsCfg := []byte(`[derived_charging]
|
||||
run_ids = run1, run2
|
||||
|
||||
@@ -71,6 +71,7 @@ const (
|
||||
CSV_SEP = ','
|
||||
FALLBACK_SEP = ';'
|
||||
INFIELD_SEP = ";"
|
||||
FIELDS_SEP = ","
|
||||
REGEXP_PREFIX = "~"
|
||||
JSON = "json"
|
||||
MSGPACK = "msgpack"
|
||||
|
||||
@@ -35,6 +35,8 @@ func NewRSRField(fldStr string) (*RSRField, error) {
|
||||
return nil, fmt.Errorf("Invalid static header/value combination: %s", fldStr)
|
||||
}
|
||||
staticHdr, staticVal = splt[0][1:], splt[1] // Strip the / suffix
|
||||
} else if len(splt) == 2 {
|
||||
return nil, fmt.Errorf("Invalid RSRField string: %s", fldStr)
|
||||
} else {
|
||||
staticHdr, staticVal = splt[0][1:], splt[0][1:] // If no split, header will remain as original, value as header without the prefix
|
||||
}
|
||||
@@ -108,9 +110,6 @@ func ParseRSRFields(fldsStr, sep string) ([]*RSRField, error) {
|
||||
rulesSplt := strings.Split(fldsStr, sep)
|
||||
rsrFields := make([]*RSRField, len(rulesSplt))
|
||||
for idx, ruleStr := range rulesSplt {
|
||||
if !strings.HasSuffix(ruleStr, "/") {
|
||||
return nil, fmt.Errorf("Invalid RSRField string: %s", ruleStr)
|
||||
}
|
||||
if rsrField, err := NewRSRField(ruleStr); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
|
||||
@@ -153,4 +153,15 @@ func TestParseRSRFields(t *testing.T) {
|
||||
} else if !reflect.DeepEqual(eRSRFields, rsrFlds) {
|
||||
t.Errorf("Expecting: %v, received: %v", eRSRFields, rsrFlds)
|
||||
}
|
||||
fields := `host,~sip_redirected_to:s/sip:\+49(\d+)@/0$1/,destination`
|
||||
expectParsedFields := []*RSRField{
|
||||
&RSRField{Id: "host"},
|
||||
&RSRField{Id: "sip_redirected_to",
|
||||
RSRules: []*ReSearchReplace{&ReSearchReplace{SearchRegexp: regexp.MustCompile(`sip:\+49(\d+)@`), ReplaceTemplate: "0$1"}}},
|
||||
&RSRField{Id: "destination"}}
|
||||
if parsedFields, err := ParseRSRFields(fields, FIELDS_SEP); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if !reflect.DeepEqual(parsedFields, expectParsedFields) {
|
||||
t.Errorf("Unexpected value of parsed fields")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user