diff --git a/agents/agentreq.go b/agents/agentreq.go index d398849c0..4009ca7e0 100644 --- a/agents/agentreq.go +++ b/agents/agentreq.go @@ -20,6 +20,7 @@ package agents import ( "fmt" + "math" "net" "strconv" "strings" @@ -258,6 +259,30 @@ func (aReq *AgentRequest) ParseField( iFaceVals[i] = utils.StringToInterface(strVal) } out, err = utils.Sum(iFaceVals...) + case utils.MetaValueExponent: + if len(cfgFld.Value) != 2 { + return nil, fmt.Errorf("invalid arguments <%s> to %s", + utils.ToJSON(cfgFld.Value), utils.MetaValueExponent) + } + strVal1, err := cfgFld.Value[0].ParseDataProvider(aReq, utils.NestingSep) // String Value + if err != nil { + return "", err + } + val, err := strconv.ParseFloat(strVal1, 64) + if err != nil { + return "", fmt.Errorf("invalid value <%s> to %s", + strVal1, utils.MetaValueExponent) + } + strVal2, err := cfgFld.Value[1].ParseDataProvider(aReq, utils.NestingSep) // String Exponent + if err != nil { + return "", err + } + exp, err := strconv.Atoi(strVal2) + if err != nil { + return "", err + } + out = strconv.FormatFloat(utils.Round(val*math.Pow10(exp), + config.CgrConfig().GeneralCfg().RoundingDecimals, utils.ROUNDING_MIDDLE), 'f', -1, 64) } if err != nil && diff --git a/agents/agentreq_test.go b/agents/agentreq_test.go index 835fd7bed..41a85a5ba 100644 --- a/agents/agentreq_test.go +++ b/agents/agentreq_test.go @@ -408,3 +408,29 @@ func TestAgReqEmptyFilter(t *testing.T) { t.Errorf("expecting: %+v, received: %+v", eMp, mpOut) } } + +func TestAgReqMetaExponent(t *testing.T) { + data, _ := engine.NewMapStorage() + dm := engine.NewDataManager(data) + cfg, _ := config.NewDefaultCGRConfig() + filterS := engine.NewFilterS(cfg, nil, dm) + agReq := newAgentRequest(nil, nil, nil, nil, "cgrates.org", "", filterS) + agReq.CGRRequest.Set([]string{"Value"}, "2", false, false) + agReq.CGRRequest.Set([]string{"Exponent"}, "2", false, false) + + tplFlds := []*config.FCTemplate{ + &config.FCTemplate{Tag: "TestExpo", Filters: []string{}, + FieldId: "TestExpo", Type: utils.MetaValueExponent, + Value: config.NewRSRParsersMustCompile("~*cgreq.Value;~*cgreq.Exponent", true, utils.INFIELD_SEP)}, + } + eMp := config.NewNavigableMap(nil) + eMp.Set([]string{"TestExpo"}, []*config.NMItem{ + &config.NMItem{Data: "200", Path: []string{"TestExpo"}, + Config: tplFlds[0]}}, false, true) + + if mpOut, err := agReq.AsNavigableMap(tplFlds); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eMp, mpOut) { + t.Errorf("expecting: %+v, \n received: %+v", eMp, mpOut) + } +} diff --git a/utils/consts.go b/utils/consts.go index 52746c49f..324d7f9c7 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -318,6 +318,7 @@ const ( META_USAGE_DIFFERENCE = "*usage_difference" MetaVariable = "*variable" MetaCCUsage = "*cc_usage" + MetaValueExponent = "*value_exponent" MetaString = "*string" NegativePrefix = "!" MatchStartPrefix = "^"