mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-22 15:48:44 +05:00
Add support for *value_exponent in AgentRequest
This commit is contained in:
committed by
Dan Christian Bogos
parent
4e45f56dd6
commit
666f436aa8
@@ -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 &&
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -318,6 +318,7 @@ const (
|
||||
META_USAGE_DIFFERENCE = "*usage_difference"
|
||||
MetaVariable = "*variable"
|
||||
MetaCCUsage = "*cc_usage"
|
||||
MetaValueExponent = "*value_exponent"
|
||||
MetaString = "*string"
|
||||
NegativePrefix = "!"
|
||||
MatchStartPrefix = "^"
|
||||
|
||||
Reference in New Issue
Block a user