Add support for *none as FieldID in AgentRequest

This commit is contained in:
TeoV
2019-05-07 11:13:05 +03:00
committed by Dan Christian Bogos
parent d9af358ae9
commit d9de22cb55
2 changed files with 108 additions and 29 deletions

View File

@@ -128,43 +128,45 @@ func (ar *AgentRequest) AsNavigableMap(tplFlds []*config.FCTemplate) (
} else if !pass {
continue
}
out, err := ar.ParseField(tplFld)
if err != nil {
if err == utils.ErrNotFound {
if !tplFld.Mandatory {
err = nil
continue
if tplFld.FieldId != utils.META_NONE {
out, err := ar.ParseField(tplFld)
if err != nil {
if err == utils.ErrNotFound {
if !tplFld.Mandatory {
err = nil
continue
}
err = utils.ErrPrefixNotFound(tplFld.Tag)
}
err = utils.ErrPrefixNotFound(tplFld.Tag)
}
return nil, err
}
var valSet []*config.NMItem
fldPath := strings.Split(tplFld.FieldId, utils.NestingSep)
nMItm := &config.NMItem{Data: out, Path: fldPath, Config: tplFld}
if nMFields, err := ar.CGRAReq.FieldAsInterface(fldPath); err != nil {
if err != utils.ErrNotFound {
return nil, err
}
} else {
valSet = nMFields.([]*config.NMItem) // start from previous stored fields
if tplFld.Type == utils.META_COMPOSED {
prevNMItem := valSet[len(valSet)-1] // could be we need nil protection here
prevDataStr, err := utils.IfaceAsString(prevNMItem.Data)
if err != nil {
var valSet []*config.NMItem
fldPath := strings.Split(tplFld.FieldId, utils.NestingSep)
nMItm := &config.NMItem{Data: out, Path: fldPath, Config: tplFld}
if nMFields, err := ar.CGRAReq.FieldAsInterface(fldPath); err != nil {
if err != utils.ErrNotFound {
return nil, err
}
outStr, err := utils.IfaceAsString(out)
if err != nil {
return nil, err
} else {
valSet = nMFields.([]*config.NMItem) // start from previous stored fields
if tplFld.Type == utils.META_COMPOSED {
prevNMItem := valSet[len(valSet)-1] // could be we need nil protection here
prevDataStr, err := utils.IfaceAsString(prevNMItem.Data)
if err != nil {
return nil, err
}
outStr, err := utils.IfaceAsString(out)
if err != nil {
return nil, err
}
*nMItm = *prevNMItem // inherit the particularities, ie AttributeName
nMItm.Data = prevDataStr + outStr
}
*nMItm = *prevNMItem // inherit the particularities, ie AttributeName
nMItm.Data = prevDataStr + outStr
valSet = valSet[:len(valSet)-1] // discard the last item
}
valSet = valSet[:len(valSet)-1] // discard the last item
valSet = append(valSet, nMItm)
ar.CGRAReq.Set(fldPath, valSet, false, true)
}
valSet = append(valSet, nMItm)
ar.CGRAReq.Set(fldPath, valSet, false, true)
if tplFld.Blocker { // useful in case of processing errors first
break
}

View File

@@ -477,3 +477,80 @@ func TestAgReqCGRActiveRequest(t *testing.T) {
t.Errorf("expecting: %+v,\n received: %+v", eMp, mpOut)
}
}
func TestAgReqFieldAsNone(t *testing.T) {
data, _ := engine.NewMapStorage()
dm := engine.NewDataManager(data)
cfg, _ := config.NewDefaultCGRConfig()
filterS := engine.NewFilterS(cfg, nil, nil, dm)
agReq := newAgentRequest(nil, nil, nil, nil, "cgrates.org", "", filterS)
// populate request, emulating the way will be done in HTTPAgent
agReq.CGRRequest.Set([]string{utils.ToR}, utils.VOICE, false, false)
agReq.CGRRequest.Set([]string{utils.Account}, "1001", false, false)
agReq.CGRRequest.Set([]string{utils.Destination}, "1002", false, false)
tplFlds := []*config.FCTemplate{
&config.FCTemplate{Tag: "Tenant",
FieldId: utils.Tenant, Type: utils.META_COMPOSED,
Value: config.NewRSRParsersMustCompile("cgrates.org", true, utils.INFIELD_SEP)},
&config.FCTemplate{Tag: "Account",
FieldId: utils.Account, Type: utils.META_COMPOSED,
Value: config.NewRSRParsersMustCompile("~*cgreq.Account", true, utils.INFIELD_SEP)},
&config.FCTemplate{FieldId: utils.META_NONE, Blocker: true},
&config.FCTemplate{Tag: "Destination",
FieldId: utils.Destination, Type: utils.META_COMPOSED,
Value: config.NewRSRParsersMustCompile("~*cgreq.Destination", true, utils.INFIELD_SEP)},
}
eMp := config.NewNavigableMap(nil)
eMp.Set([]string{utils.Tenant}, []*config.NMItem{
&config.NMItem{Data: "cgrates.org", Path: []string{utils.Tenant},
Config: tplFlds[0]}}, false, true)
eMp.Set([]string{utils.Account}, []*config.NMItem{
&config.NMItem{Data: "1001", Path: []string{utils.Account},
Config: tplFlds[1]}}, false, true)
if mpOut, err := agReq.AsNavigableMap(tplFlds); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eMp, mpOut) {
t.Errorf("expecting: %+v, received: %+v", eMp, mpOut)
}
}
func TestAgReqFieldAsNone2(t *testing.T) {
data, _ := engine.NewMapStorage()
dm := engine.NewDataManager(data)
cfg, _ := config.NewDefaultCGRConfig()
filterS := engine.NewFilterS(cfg, nil, nil, dm)
agReq := newAgentRequest(nil, nil, nil, nil, "cgrates.org", "", filterS)
// populate request, emulating the way will be done in HTTPAgent
agReq.CGRRequest.Set([]string{utils.ToR}, utils.VOICE, false, false)
agReq.CGRRequest.Set([]string{utils.Account}, "1001", false, false)
agReq.CGRRequest.Set([]string{utils.Destination}, "1002", false, false)
tplFlds := []*config.FCTemplate{
&config.FCTemplate{Tag: "Tenant",
FieldId: utils.Tenant, Type: utils.META_COMPOSED,
Value: config.NewRSRParsersMustCompile("cgrates.org", true, utils.INFIELD_SEP)},
&config.FCTemplate{Tag: "Account",
FieldId: utils.Account, Type: utils.META_COMPOSED,
Value: config.NewRSRParsersMustCompile("~*cgreq.Account", true, utils.INFIELD_SEP)},
&config.FCTemplate{FieldId: utils.META_NONE},
&config.FCTemplate{Tag: "Destination",
FieldId: utils.Destination, Type: utils.META_COMPOSED,
Value: config.NewRSRParsersMustCompile("~*cgreq.Destination", true, utils.INFIELD_SEP)},
}
eMp := config.NewNavigableMap(nil)
eMp.Set([]string{utils.Tenant}, []*config.NMItem{
&config.NMItem{Data: "cgrates.org", Path: []string{utils.Tenant},
Config: tplFlds[0]}}, false, true)
eMp.Set([]string{utils.Account}, []*config.NMItem{
&config.NMItem{Data: "1001", Path: []string{utils.Account},
Config: tplFlds[1]}}, false, true)
eMp.Set([]string{utils.Destination}, []*config.NMItem{
&config.NMItem{Data: "1002", Path: []string{utils.Destination},
Config: tplFlds[3]}}, false, true)
if mpOut, err := agReq.AsNavigableMap(tplFlds); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eMp, mpOut) {
t.Errorf("expecting: %+v, received: %+v", eMp, mpOut)
}
}