From 59340fd4a4e345f32c1f270bb8f8c0258c813289 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Thu, 25 Mar 2021 11:13:25 +0200 Subject: [PATCH] Updated sessions tests --- engine/eventrequest.go | 132 ++++++++++++++++++++++---------- sessions/sessions.go | 144 +++++++++++++++++------------------ sessions/sessions_test.go | 84 ++++++++++---------- utils/orderednavigablemap.go | 2 +- 4 files changed, 206 insertions(+), 156 deletions(-) diff --git a/engine/eventrequest.go b/engine/eventrequest.go index 3734bf5d9..8fbfcab93 100644 --- a/engine/eventrequest.go +++ b/engine/eventrequest.go @@ -44,12 +44,8 @@ func NewEventRequest(req utils.DataProvider, dc, opts utils.MapStorage, OrdNavMP: oNM, Cfg: config.CgrConfig().GetDataProvider(), } - if tntTpl != nil { - if tntIf, err := eeR.ParseField( - &config.FCTemplate{Type: utils.MetaComposed, - Value: tntTpl}); err == nil && tntIf.(string) != "" { - eeR.Tenant = tntIf.(string) - } + if tnt, err := tntTpl.ParseDataProvider(eeR); err == nil && tnt != utils.EmptyString { + eeR.Tenant = tnt } return } @@ -82,14 +78,17 @@ func (eeR *EventRequest) RemoteHost() net.Addr { func (eeR *EventRequest) FieldAsInterface(fldPath []string) (val interface{}, err error) { switch fldPath[0] { default: - return nil, fmt.Errorf("unsupported field prefix: <%s>", fldPath[0]) + dp, has := eeR.OrdNavMP[fldPath[0]] + if !has { + return nil, fmt.Errorf("unsupported field prefix: <%s>", fldPath[0]) + } + val, err = dp.FieldAsInterface(fldPath[1:]) case utils.MetaReq: val, err = eeR.req.FieldAsInterface(fldPath[1:]) case utils.MetaUCH: - if cacheVal, ok := Cache.Get(utils.CacheUCH, strings.Join(fldPath[1:], utils.NestingSep)); !ok { - err = utils.ErrNotFound - } else { - val = cacheVal + var ok bool + if val, ok = Cache.Get(utils.CacheUCH, strings.Join(fldPath[1:], utils.NestingSep)); !ok { + return nil, utils.ErrNotFound } case utils.MetaDC: val, err = eeR.dc.FieldAsInterface(fldPath[1:]) @@ -101,21 +100,15 @@ func (eeR *EventRequest) FieldAsInterface(fldPath []string) (val interface{}, er if err != nil { return } - if nmItems, isNMItems := val.(*utils.NMSlice); isNMItems { // special handling of NMItems, take the last value out of it - val = (*nmItems)[len(*nmItems)-1].Interface() + if nmItems, isNMItems := val.(*utils.DataNode); isNMItems && nmItems.Type == utils.NMSliceType { // special handling of NMItems, take the last value out of it + el := nmItems.Slice[len(nmItems.Slice)-1] + if el.Type == utils.NMDataType { + val = el.Value.Data + } } return } -// Field implements utils.NMInterface -func (eeR *EventRequest) Field(fldPath utils.PathItems) (val utils.NMInterface, err error) { - nm, has := eeR.OrdNavMP[fldPath[0].Field] - if !has { - return nil, fmt.Errorf("unsupported field prefix: <%s>", fldPath[0]) - } - return nm.Field(fldPath[1:]) -} - // FieldAsString implements utils.DataProvider func (eeR *EventRequest) FieldAsString(fldPath []string) (val string, err error) { var iface interface{} @@ -153,21 +146,21 @@ func (eeR *EventRequest) SetFields(tplFlds []*config.FCTemplate) (err error) { return } else if fullPath == nil { // no dynamic path fullPath = &utils.FullPath{ - PathItems: tplFld.GetPathItems().Clone(), // need to clone so me do not modify the template + PathItems: utils.CloneStringSlice(tplFld.GetPathItems()), // need to clone so me do not modify the template Path: tplFld.Path, } itmPath = tplFld.GetPathSlice()[1:] } else { - itmPath = fullPath.PathItems.Slice()[1:] + itmPath = fullPath.PathItems } - nMItm := &config.NMItem{Data: out, Path: itmPath, Config: tplFld} + nMItm := &utils.DataLeaf{Data: out, Path: itmPath, NewBranch: tplFld.NewBranch, AttributeID: tplFld.AttributeID} switch tplFld.Type { case utils.MetaComposed: - err = utils.ComposeNavMapVal(eeR, fullPath, nMItm) + err = eeR.Compose(fullPath, nMItm) case utils.MetaGroup: // in case of *group type simply append to valSet - err = utils.AppendNavMapVal(eeR, fullPath, nMItm) + err = eeR.Append(fullPath, nMItm) default: - _, err = eeR.Set(fullPath, &utils.NMSlice{nMItm}) + err = eeR.SetAsSlice(fullPath, nMItm) } if err != nil { return @@ -181,19 +174,22 @@ func (eeR *EventRequest) SetFields(tplFlds []*config.FCTemplate) (err error) { } // Set implements utils.NMInterface -func (eeR *EventRequest) Set(fullPath *utils.FullPath, nm utils.NMInterface) (added bool, err error) { - if fullPath.PathItems[0].Field == utils.MetaUCH { - err = Cache.Set(utils.CacheUCH, fullPath.Path[5:], nm, nil, true, utils.NonTransactional) - return +func (eeR *EventRequest) SetAsSlice(fullPath *utils.FullPath, val *utils.DataLeaf) (err error) { + switch prfx := fullPath.PathItems[0]; prfx { + case utils.MetaUCH: + return Cache.Set(utils.CacheUCH, fullPath.Path[5:], val.Data, nil, true, utils.NonTransactional) + case utils.MetaOpts: + return eeR.opts.Set(fullPath.PathItems[1:], val.Data) + default: + oNM, has := eeR.OrdNavMP[prfx] + if !has { + return fmt.Errorf("unsupported field prefix: <%s> when set field", prfx) + } + return oNM.SetAsSlice(&utils.FullPath{ + PathItems: fullPath.PathItems[1:], + Path: fullPath.Path[len(prfx):], + }, []*utils.DataNode{{Type: utils.NMDataType, Value: val}}) } - oNM, has := eeR.OrdNavMP[fullPath.PathItems[0].Field] - if !has { - return false, fmt.Errorf("unsupported field prefix: <%s> when set field", fullPath.PathItems[0].Field) - } - return oNM.Set(&utils.FullPath{ - PathItems: fullPath.PathItems[1:], - Path: fullPath.Path[len(fullPath.PathItems[0].Field):], - }, nm) } // ParseField outputs the value based on the template item @@ -365,3 +361,59 @@ func (eeR *EventRequest) ParseField( } return } + +// Set sets the value at the given path +// this used with full path and the processed path to not calculate them for every set +func (eeR *EventRequest) Append(fullPath *utils.FullPath, val *utils.DataLeaf) (err error) { + switch prfx := fullPath.PathItems[0]; prfx { + case utils.MetaUCH: + return Cache.Set(utils.CacheUCH, fullPath.Path[5:], val.Data, nil, true, utils.NonTransactional) + case utils.MetaOpts: + return eeR.opts.Set(fullPath.PathItems[1:], val.Data) + default: + oNM, has := eeR.OrdNavMP[prfx] + if !has { + return fmt.Errorf("unsupported field prefix: <%s> when set field", prfx) + } + return oNM.Append(&utils.FullPath{ + PathItems: fullPath.PathItems[1:], + Path: fullPath.Path[len(prfx):], + }, val) + } +} + +// Set sets the value at the given path +// this used with full path and the processed path to not calculate them for every set +func (eeR *EventRequest) Compose(fullPath *utils.FullPath, val *utils.DataLeaf) (err error) { + switch prfx := fullPath.PathItems[0]; prfx { + case utils.MetaUCH: + path := fullPath.Path[5:] + var prv interface{} + if prvI, ok := Cache.Get(utils.CacheUCH, path); !ok { + prv = val.Data + } else { + prv = utils.IfaceAsString(prvI) + utils.IfaceAsString(val.Data) + } + return Cache.Set(utils.CacheUCH, path, prv, nil, true, utils.NonTransactional) + case utils.MetaOpts: + var prv interface{} + if prv, err = eeR.opts.FieldAsInterface(fullPath.PathItems[1:]); err != nil { + if err != utils.ErrNotFound { + return + } + prv = val.Data + } else { + prv = utils.IfaceAsString(prv) + utils.IfaceAsString(val.Data) + } + return eeR.opts.Set(fullPath.PathItems[1:], prv) + default: + oNM, has := eeR.OrdNavMP[prfx] + if !has { + return fmt.Errorf("unsupported field prefix: <%s> when set field", prfx) + } + return oNM.Compose(&utils.FullPath{ + PathItems: fullPath.PathItems[1:], + Path: fullPath.Path[len(prfx):], + }, val) + } +} diff --git a/sessions/sessions.go b/sessions/sessions.go index 076ecf78f..6e807ae0c 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -1873,44 +1873,44 @@ func (v1AuthReply *V1AuthorizeReply) SetMaxUsageNeeded(getMaxUsage bool) { } // AsNavigableMap is part of engine.NavigableMapper interface -func (v1AuthReply *V1AuthorizeReply) AsNavigableMap() utils.NavigableMap { - cgrReply := make(utils.NavigableMap) +func (v1AuthReply *V1AuthorizeReply) AsNavigableMap() map[string]*utils.DataNode { + cgrReply := make(map[string]*utils.DataNode) if v1AuthReply.Attributes != nil { - attrs := make(utils.NavigableMap) + attrs := &utils.DataNode{Type: utils.NMMapType, Map: make(map[string]*utils.DataNode)} for _, fldName := range v1AuthReply.Attributes.AlteredFields { fldName = strings.TrimPrefix(fldName, utils.MetaReq+utils.NestingSep) if v1AuthReply.Attributes.CGREvent.HasField(fldName) { - attrs[fldName] = utils.NewNMData(v1AuthReply.Attributes.CGREvent.Event[fldName]) + attrs.Map[fldName] = utils.NewLeafNode(v1AuthReply.Attributes.CGREvent.Event[fldName]) } } cgrReply[utils.CapAttributes] = attrs } if v1AuthReply.ResourceAllocation != nil { - cgrReply[utils.CapResourceAllocation] = utils.NewNMData(*v1AuthReply.ResourceAllocation) + cgrReply[utils.CapResourceAllocation] = utils.NewLeafNode(*v1AuthReply.ResourceAllocation) } if v1AuthReply.MaxUsage != nil { - cgrReply[utils.CapMaxUsage] = utils.NewNMData(*v1AuthReply.MaxUsage) + cgrReply[utils.CapMaxUsage] = utils.NewLeafNode(*v1AuthReply.MaxUsage) } else if v1AuthReply.needsMaxUsage { - cgrReply[utils.CapMaxUsage] = utils.NewNMData(0) + cgrReply[utils.CapMaxUsage] = utils.NewLeafNode(0) } if v1AuthReply.RouteProfiles != nil { nm := v1AuthReply.RouteProfiles.AsNavigableMap() - cgrReply[utils.CapRouteProfiles] = &nm + cgrReply[utils.CapRouteProfiles] = nm } if v1AuthReply.ThresholdIDs != nil { - thIDs := make(utils.NMSlice, len(*v1AuthReply.ThresholdIDs)) + thIDs := &utils.DataNode{Type: utils.NMSliceType, Slice: make([]*utils.DataNode, len(*v1AuthReply.ThresholdIDs))} for i, v := range *v1AuthReply.ThresholdIDs { - thIDs[i] = utils.NewNMData(v) + thIDs.Slice[i] = utils.NewLeafNode(v) } - cgrReply[utils.CapThresholds] = &thIDs + cgrReply[utils.CapThresholds] = thIDs } if v1AuthReply.StatQueueIDs != nil { - stIDs := make(utils.NMSlice, len(*v1AuthReply.StatQueueIDs)) + stIDs := &utils.DataNode{Type: utils.NMSliceType, Slice: make([]*utils.DataNode, len(*v1AuthReply.StatQueueIDs))} for i, v := range *v1AuthReply.StatQueueIDs { - stIDs[i] = utils.NewNMData(v) + stIDs.Slice[i] = utils.NewLeafNode(v) } - cgrReply[utils.CapStatQueues] = &stIDs + cgrReply[utils.CapStatQueues] = stIDs } return cgrReply } @@ -2159,40 +2159,40 @@ func (v1Rply *V1InitSessionReply) SetMaxUsageNeeded(getMaxUsage bool) { } // AsNavigableMap is part of engine.NavigableMapper interface -func (v1Rply *V1InitSessionReply) AsNavigableMap() utils.NavigableMap { - cgrReply := make(utils.NavigableMap) +func (v1Rply *V1InitSessionReply) AsNavigableMap() map[string]*utils.DataNode { + cgrReply := make(map[string]*utils.DataNode) if v1Rply.Attributes != nil { - attrs := make(utils.NavigableMap) + attrs := &utils.DataNode{Type: utils.NMMapType, Map: make(map[string]*utils.DataNode)} for _, fldName := range v1Rply.Attributes.AlteredFields { fldName = strings.TrimPrefix(fldName, utils.MetaReq+utils.NestingSep) if v1Rply.Attributes.CGREvent.HasField(fldName) { - attrs[fldName] = utils.NewNMData(v1Rply.Attributes.CGREvent.Event[fldName]) + attrs.Map[fldName] = utils.NewLeafNode(v1Rply.Attributes.CGREvent.Event[fldName]) } } cgrReply[utils.CapAttributes] = attrs } if v1Rply.ResourceAllocation != nil { - cgrReply[utils.CapResourceAllocation] = utils.NewNMData(*v1Rply.ResourceAllocation) + cgrReply[utils.CapResourceAllocation] = utils.NewLeafNode(*v1Rply.ResourceAllocation) } if v1Rply.MaxUsage != nil { - cgrReply[utils.CapMaxUsage] = utils.NewNMData(*v1Rply.MaxUsage) + cgrReply[utils.CapMaxUsage] = utils.NewLeafNode(*v1Rply.MaxUsage) } else if v1Rply.needsMaxUsage { - cgrReply[utils.CapMaxUsage] = utils.NewNMData(0) + cgrReply[utils.CapMaxUsage] = utils.NewLeafNode(0) } if v1Rply.ThresholdIDs != nil { - thIDs := make(utils.NMSlice, len(*v1Rply.ThresholdIDs)) + thIDs := &utils.DataNode{Type: utils.NMSliceType, Slice: make([]*utils.DataNode, len(*v1Rply.ThresholdIDs))} for i, v := range *v1Rply.ThresholdIDs { - thIDs[i] = utils.NewNMData(v) + thIDs.Slice[i] = utils.NewLeafNode(v) } - cgrReply[utils.CapThresholds] = &thIDs + cgrReply[utils.CapThresholds] = thIDs } if v1Rply.StatQueueIDs != nil { - stIDs := make(utils.NMSlice, len(*v1Rply.StatQueueIDs)) + stIDs := &utils.DataNode{Type: utils.NMSliceType, Slice: make([]*utils.DataNode, len(*v1Rply.StatQueueIDs))} for i, v := range *v1Rply.StatQueueIDs { - stIDs[i] = utils.NewNMData(v) + stIDs.Slice[i] = utils.NewLeafNode(v) } - cgrReply[utils.CapStatQueues] = &stIDs + cgrReply[utils.CapStatQueues] = stIDs } return cgrReply } @@ -2409,22 +2409,22 @@ func (v1Rply *V1UpdateSessionReply) SetMaxUsageNeeded(getMaxUsage bool) { } // AsNavigableMap is part of engine.NavigableMapper interface -func (v1Rply *V1UpdateSessionReply) AsNavigableMap() utils.NavigableMap { - cgrReply := make(utils.NavigableMap) +func (v1Rply *V1UpdateSessionReply) AsNavigableMap() map[string]*utils.DataNode { + cgrReply := make(map[string]*utils.DataNode) if v1Rply.Attributes != nil { - attrs := make(utils.NavigableMap) + attrs := &utils.DataNode{Type: utils.NMMapType, Map: make(map[string]*utils.DataNode)} for _, fldName := range v1Rply.Attributes.AlteredFields { fldName = strings.TrimPrefix(fldName, utils.MetaReq+utils.NestingSep) if v1Rply.Attributes.CGREvent.HasField(fldName) { - attrs[fldName] = utils.NewNMData(v1Rply.Attributes.CGREvent.Event[fldName]) + attrs.Map[fldName] = utils.NewLeafNode(v1Rply.Attributes.CGREvent.Event[fldName]) } } cgrReply[utils.CapAttributes] = attrs } if v1Rply.MaxUsage != nil { - cgrReply[utils.CapMaxUsage] = utils.NewNMData(*v1Rply.MaxUsage) + cgrReply[utils.CapMaxUsage] = utils.NewLeafNode(*v1Rply.MaxUsage) } else if v1Rply.needsMaxUsage { - cgrReply[utils.CapMaxUsage] = utils.NewNMData(0) + cgrReply[utils.CapMaxUsage] = utils.NewLeafNode(0) } return cgrReply } @@ -2842,43 +2842,42 @@ func (v1Rply *V1ProcessMessageReply) SetMaxUsageNeeded(getMaxUsage bool) { } // AsNavigableMap is part of engine.NavigableMapper interface -func (v1Rply *V1ProcessMessageReply) AsNavigableMap() utils.NavigableMap { - cgrReply := make(utils.NavigableMap) +func (v1Rply *V1ProcessMessageReply) AsNavigableMap() map[string]*utils.DataNode { + cgrReply := make(map[string]*utils.DataNode) if v1Rply.MaxUsage != nil { - cgrReply[utils.CapMaxUsage] = utils.NewNMData(*v1Rply.MaxUsage) + cgrReply[utils.CapMaxUsage] = utils.NewLeafNode(*v1Rply.MaxUsage) } else if v1Rply.needsMaxUsage { - cgrReply[utils.CapMaxUsage] = utils.NewNMData(0) + cgrReply[utils.CapMaxUsage] = utils.NewLeafNode(0) } if v1Rply.ResourceAllocation != nil { - cgrReply[utils.CapResourceAllocation] = utils.NewNMData(*v1Rply.ResourceAllocation) + cgrReply[utils.CapResourceAllocation] = utils.NewLeafNode(*v1Rply.ResourceAllocation) } if v1Rply.Attributes != nil { - attrs := make(utils.NavigableMap) + attrs := &utils.DataNode{Type: utils.NMMapType, Map: make(map[string]*utils.DataNode)} for _, fldName := range v1Rply.Attributes.AlteredFields { fldName = strings.TrimPrefix(fldName, utils.MetaReq+utils.NestingSep) if v1Rply.Attributes.CGREvent.HasField(fldName) { - attrs[fldName] = utils.NewNMData(v1Rply.Attributes.CGREvent.Event[fldName]) + attrs.Map[fldName] = utils.NewLeafNode(v1Rply.Attributes.CGREvent.Event[fldName]) } } cgrReply[utils.CapAttributes] = attrs } if v1Rply.RouteProfiles != nil { - nm := v1Rply.RouteProfiles.AsNavigableMap() - cgrReply[utils.CapRouteProfiles] = &nm + cgrReply[utils.CapRouteProfiles] = v1Rply.RouteProfiles.AsNavigableMap() } if v1Rply.ThresholdIDs != nil { - thIDs := make(utils.NMSlice, len(*v1Rply.ThresholdIDs)) + thIDs := &utils.DataNode{Type: utils.NMSliceType, Slice: make([]*utils.DataNode, len(*v1Rply.ThresholdIDs))} for i, v := range *v1Rply.ThresholdIDs { - thIDs[i] = utils.NewNMData(v) + thIDs.Slice[i] = utils.NewLeafNode(v) } - cgrReply[utils.CapThresholds] = &thIDs + cgrReply[utils.CapThresholds] = thIDs } if v1Rply.StatQueueIDs != nil { - stIDs := make(utils.NMSlice, len(*v1Rply.StatQueueIDs)) + stIDs := &utils.DataNode{Type: utils.NMSliceType, Slice: make([]*utils.DataNode, len(*v1Rply.StatQueueIDs))} for i, v := range *v1Rply.StatQueueIDs { - stIDs[i] = utils.NewNMData(v) + stIDs.Slice[i] = utils.NewLeafNode(v) } - cgrReply[utils.CapStatQueues] = &stIDs + cgrReply[utils.CapStatQueues] = stIDs } return cgrReply } @@ -3013,76 +3012,75 @@ type V1ProcessEventReply struct { } // AsNavigableMap is part of engine.NavigableMapper interface -func (v1Rply *V1ProcessEventReply) AsNavigableMap() utils.NavigableMap { - cgrReply := make(utils.NavigableMap) +func (v1Rply *V1ProcessEventReply) AsNavigableMap() map[string]*utils.DataNode { + cgrReply := make(map[string]*utils.DataNode) if v1Rply.MaxUsage != nil { - usage := make(utils.NavigableMap) + usage := &utils.DataNode{Type: utils.NMMapType, Map: make(map[string]*utils.DataNode)} for k, v := range v1Rply.MaxUsage { - usage[k] = utils.NewNMData(v) + usage.Map[k] = utils.NewLeafNode(v) } cgrReply[utils.CapMaxUsage] = usage } if v1Rply.ResourceAllocation != nil { - res := make(utils.NavigableMap) + res := &utils.DataNode{Type: utils.NMMapType, Map: make(map[string]*utils.DataNode)} for k, v := range v1Rply.ResourceAllocation { - res[k] = utils.NewNMData(v) + res.Map[k] = utils.NewLeafNode(v) } cgrReply[utils.CapResourceAllocation] = res } if v1Rply.Attributes != nil { - atts := make(utils.NavigableMap) + atts := &utils.DataNode{Type: utils.NMMapType, Map: make(map[string]*utils.DataNode)} for k, att := range v1Rply.Attributes { - attrs := make(utils.NavigableMap) + attrs := &utils.DataNode{Type: utils.NMMapType, Map: make(map[string]*utils.DataNode)} for _, fldName := range att.AlteredFields { fldName = strings.TrimPrefix(fldName, utils.MetaReq+utils.NestingSep) if att.CGREvent.HasField(fldName) { - attrs[fldName] = utils.NewNMData(att.CGREvent.Event[fldName]) + attrs.Map[fldName] = utils.NewLeafNode(att.CGREvent.Event[fldName]) } } - atts[k] = attrs + atts.Map[k] = attrs } cgrReply[utils.CapAttributes] = atts } if v1Rply.RouteProfiles != nil { - routes := make(utils.NavigableMap) + routes := &utils.DataNode{Type: utils.NMMapType, Map: make(map[string]*utils.DataNode)} for k, route := range v1Rply.RouteProfiles { - nm := route.AsNavigableMap() - routes[k] = &nm + routes.Map[k] = route.AsNavigableMap() } cgrReply[utils.CapRouteProfiles] = routes } if v1Rply.ThresholdIDs != nil { - th := make(utils.NavigableMap) + th := &utils.DataNode{Type: utils.NMMapType, Map: make(map[string]*utils.DataNode)} for k, thr := range v1Rply.ThresholdIDs { - thIDs := make(utils.NMSlice, len(thr)) + thIDs := &utils.DataNode{Type: utils.NMSliceType, Slice: make([]*utils.DataNode, len(thr))} for i, v := range thr { - thIDs[i] = utils.NewNMData(v) + thIDs.Slice[i] = utils.NewLeafNode(v) } - th[k] = &thIDs + th.Map[k] = thIDs } cgrReply[utils.CapThresholds] = th } if v1Rply.StatQueueIDs != nil { - st := make(utils.NavigableMap) + st := &utils.DataNode{Type: utils.NMMapType, Map: make(map[string]*utils.DataNode)} for k, sts := range v1Rply.StatQueueIDs { - stIDs := make(utils.NMSlice, len(sts)) + stIDs := &utils.DataNode{Type: utils.NMSliceType, Slice: make([]*utils.DataNode, len(sts))} for i, v := range sts { - stIDs[i] = utils.NewNMData(v) + stIDs.Slice[i] = utils.NewLeafNode(v) } - st[k] = &stIDs + st.Map[k] = stIDs } cgrReply[utils.CapStatQueues] = st } if v1Rply.Cost != nil { - costs := make(utils.NavigableMap) + costs := &utils.DataNode{Type: utils.NMMapType, Map: make(map[string]*utils.DataNode)} for k, cost := range v1Rply.Cost { - costs[k] = utils.NewNMData(cost) + costs.Map[k] = utils.NewLeafNode(cost) } } if v1Rply.STIRIdentity != nil { - stir := make(utils.NavigableMap) + stir := &utils.DataNode{Type: utils.NMMapType, Map: make(map[string]*utils.DataNode)} for k, v := range v1Rply.STIRIdentity { - stir[k] = utils.NewNMData(v) + stir.Map[k] = utils.NewLeafNode(v) } cgrReply[utils.OptsStirIdentity] = stir } diff --git a/sessions/sessions_test.go b/sessions/sessions_test.go index 2fee9ac45..a1f498f53 100644 --- a/sessions/sessions_test.go +++ b/sessions/sessions_test.go @@ -1305,22 +1305,22 @@ func TestSessionSV1AuthorizeReplyAsNavigableMap(t *testing.T) { thIDs := &[]string{"THD_RES_1", "THD_STATS_1", "THD_STATS_2", "THD_CDRS_1"} statIDs := &[]string{"Stats2", "Stats1", "Stats3"} v1AuthRpl := new(V1AuthorizeReply) - expected := utils.NavigableMap{} + expected := map[string]*utils.DataNode{} if rply := v1AuthRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } v1AuthRpl.Attributes = attrs - expected[utils.CapAttributes] = utils.NavigableMap{"OfficeGroup": utils.NewNMData("Marketing")} + expected[utils.CapAttributes] = &utils.DataNode{Type: utils.NMMapType, Map: map[string]*utils.DataNode{"OfficeGroup": utils.NewLeafNode("Marketing")}} if rply := v1AuthRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } v1AuthRpl.needsMaxUsage = true - expected[utils.CapMaxUsage] = utils.NewNMData(0) + expected[utils.CapMaxUsage] = utils.NewLeafNode(0) if rply := v1AuthRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } v1AuthRpl.MaxUsage = utils.DurationPointer(5 * time.Minute) - expected[utils.CapMaxUsage] = utils.NewNMData(5 * time.Minute) + expected[utils.CapMaxUsage] = utils.NewLeafNode(5 * time.Minute) if rply := v1AuthRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } @@ -1333,13 +1333,13 @@ func TestSessionSV1AuthorizeReplyAsNavigableMap(t *testing.T) { StatQueueIDs: statIDs, } nm := splrs.AsNavigableMap() - expected = utils.NavigableMap{ - utils.CapAttributes: utils.NavigableMap{"OfficeGroup": utils.NewNMData("Marketing")}, - utils.CapResourceAllocation: utils.NewNMData("ResGr1"), - utils.CapMaxUsage: utils.NewNMData(5 * time.Minute), - utils.CapRouteProfiles: &nm, - utils.CapThresholds: &utils.NMSlice{utils.NewNMData("THD_RES_1"), utils.NewNMData("THD_STATS_1"), utils.NewNMData("THD_STATS_2"), utils.NewNMData("THD_CDRS_1")}, - utils.CapStatQueues: &utils.NMSlice{utils.NewNMData("Stats2"), utils.NewNMData("Stats1"), utils.NewNMData("Stats3")}, + expected = map[string]*utils.DataNode{ + utils.CapAttributes: {Type: utils.NMMapType, Map: map[string]*utils.DataNode{"OfficeGroup": utils.NewLeafNode("Marketing")}}, + utils.CapResourceAllocation: utils.NewLeafNode("ResGr1"), + utils.CapMaxUsage: utils.NewLeafNode(5 * time.Minute), + utils.CapRouteProfiles: nm, + utils.CapThresholds: {Type: utils.NMSliceType, Slice: []*utils.DataNode{utils.NewLeafNode("THD_RES_1"), utils.NewLeafNode("THD_STATS_1"), utils.NewLeafNode("THD_STATS_2"), utils.NewLeafNode("THD_CDRS_1")}}, + utils.CapStatQueues: {Type: utils.NMSliceType, Slice: []*utils.DataNode{utils.NewLeafNode("Stats2"), utils.NewLeafNode("Stats1"), utils.NewLeafNode("Stats3")}}, } if rply := v1AuthRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) @@ -1350,22 +1350,22 @@ func TestSessionSV1InitSessionReplyAsNavigableMap(t *testing.T) { thIDs := &[]string{"THD_RES_1", "THD_STATS_1", "THD_STATS_2", "THD_CDRS_1"} statIDs := &[]string{"Stats2", "Stats1", "Stats3"} v1InitRpl := new(V1InitSessionReply) - expected := utils.NavigableMap{} + expected := map[string]*utils.DataNode{} if rply := v1InitRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } v1InitRpl.Attributes = attrs - expected[utils.CapAttributes] = utils.NavigableMap{"OfficeGroup": utils.NewNMData("Marketing")} + expected[utils.CapAttributes] = &utils.DataNode{Type: utils.NMMapType, Map: map[string]*utils.DataNode{"OfficeGroup": utils.NewLeafNode("Marketing")}} if rply := v1InitRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } v1InitRpl.needsMaxUsage = true - expected[utils.CapMaxUsage] = utils.NewNMData(0) + expected[utils.CapMaxUsage] = utils.NewLeafNode(0) if rply := v1InitRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } v1InitRpl.MaxUsage = utils.DurationPointer(5 * time.Minute) - expected[utils.CapMaxUsage] = utils.NewNMData(5 * time.Minute) + expected[utils.CapMaxUsage] = utils.NewLeafNode(5 * time.Minute) if rply := v1InitRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } @@ -1376,12 +1376,12 @@ func TestSessionSV1InitSessionReplyAsNavigableMap(t *testing.T) { ThresholdIDs: thIDs, StatQueueIDs: statIDs, } - expected = utils.NavigableMap{ - utils.CapAttributes: utils.NavigableMap{"OfficeGroup": utils.NewNMData("Marketing")}, - utils.CapResourceAllocation: utils.NewNMData("ResGr1"), - utils.CapMaxUsage: utils.NewNMData(5 * time.Minute), - utils.CapThresholds: &utils.NMSlice{utils.NewNMData("THD_RES_1"), utils.NewNMData("THD_STATS_1"), utils.NewNMData("THD_STATS_2"), utils.NewNMData("THD_CDRS_1")}, - utils.CapStatQueues: &utils.NMSlice{utils.NewNMData("Stats2"), utils.NewNMData("Stats1"), utils.NewNMData("Stats3")}, + expected = map[string]*utils.DataNode{ + utils.CapAttributes: {Type: utils.NMMapType, Map: map[string]*utils.DataNode{"OfficeGroup": utils.NewLeafNode("Marketing")}}, + utils.CapResourceAllocation: utils.NewLeafNode("ResGr1"), + utils.CapMaxUsage: utils.NewLeafNode(5 * time.Minute), + utils.CapThresholds: {Type: utils.NMSliceType, Slice: []*utils.DataNode{utils.NewLeafNode("THD_RES_1"), utils.NewLeafNode("THD_STATS_1"), utils.NewLeafNode("THD_STATS_2"), utils.NewLeafNode("THD_CDRS_1")}}, + utils.CapStatQueues: {Type: utils.NMSliceType, Slice: []*utils.DataNode{utils.NewLeafNode("Stats2"), utils.NewLeafNode("Stats1"), utils.NewLeafNode("Stats3")}}, } if rply := v1InitRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) @@ -1390,24 +1390,24 @@ func TestSessionSV1InitSessionReplyAsNavigableMap(t *testing.T) { func TestSessionSV1UpdateSessionReplyAsNavigableMap(t *testing.T) { v1UpdtRpl := new(V1UpdateSessionReply) - expected := utils.NavigableMap{} + expected := map[string]*utils.DataNode{} if rply := v1UpdtRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } v1UpdtRpl.Attributes = attrs - expected[utils.CapAttributes] = utils.NavigableMap{"OfficeGroup": utils.NewNMData("Marketing")} + expected[utils.CapAttributes] = &utils.DataNode{Type: utils.NMMapType, Map: map[string]*utils.DataNode{"OfficeGroup": utils.NewLeafNode("Marketing")}} if rply := v1UpdtRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } v1UpdtRpl.needsMaxUsage = true - expected[utils.CapMaxUsage] = utils.NewNMData(0) + expected[utils.CapMaxUsage] = utils.NewLeafNode(0) if rply := v1UpdtRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } v1UpdtRpl.MaxUsage = utils.DurationPointer(5 * time.Minute) - expected[utils.CapMaxUsage] = utils.NewNMData(5 * time.Minute) + expected[utils.CapMaxUsage] = utils.NewLeafNode(5 * time.Minute) if rply := v1UpdtRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } @@ -1467,31 +1467,31 @@ func TestSetMaxUsageNeededAuthorizeReply(t *testing.T) { func TestSessionSV1ProcessMessageReplyAsNavigableMap(t *testing.T) { v1PrcEvRpl := new(V1ProcessMessageReply) - expected := utils.NavigableMap{} + expected := map[string]*utils.DataNode{} if rply := v1PrcEvRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } v1PrcEvRpl.Attributes = attrs - expected[utils.CapAttributes] = utils.NavigableMap{"OfficeGroup": utils.NewNMData("Marketing")} + expected[utils.CapAttributes] = &utils.DataNode{Type: utils.NMMapType, Map: map[string]*utils.DataNode{"OfficeGroup": utils.NewLeafNode("Marketing")}} if rply := v1PrcEvRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } v1PrcEvRpl.needsMaxUsage = true - expected[utils.CapMaxUsage] = utils.NewNMData(0) + expected[utils.CapMaxUsage] = utils.NewLeafNode(0) if rply := v1PrcEvRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } v1PrcEvRpl.MaxUsage = utils.DurationPointer(5 * time.Minute) - expected[utils.CapMaxUsage] = utils.NewNMData(5 * time.Minute) + expected[utils.CapMaxUsage] = utils.NewLeafNode(5 * time.Minute) if rply := v1PrcEvRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } v1PrcEvRpl.ResourceAllocation = utils.StringPointer("ResGr1") - expected[utils.CapResourceAllocation] = utils.NewNMData("ResGr1") + expected[utils.CapResourceAllocation] = utils.NewLeafNode("ResGr1") if rply := v1PrcEvRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } @@ -1505,11 +1505,11 @@ func TestSessionSV1ProcessMessageReplyAsNavigableMap(t *testing.T) { v1PrcEvRpl.RouteProfiles = tmpRoutes v1PrcEvRpl.ThresholdIDs = &tmpTresholdIDs v1PrcEvRpl.StatQueueIDs = &tmpStatQueueIDs - expected[utils.CapResourceAllocation] = utils.NewNMData("ResGr1") + expected[utils.CapResourceAllocation] = utils.NewLeafNode("ResGr1") nm := tmpRoutes.AsNavigableMap() - expected[utils.CapRouteProfiles] = &nm - expected[utils.CapThresholds] = &utils.NMSlice{utils.NewNMData("ID1"), utils.NewNMData("ID2")} - expected[utils.CapStatQueues] = &utils.NMSlice{utils.NewNMData("Que1"), utils.NewNMData("Que2")} + expected[utils.CapRouteProfiles] = nm + expected[utils.CapThresholds] = &utils.DataNode{Type: utils.NMSliceType, Slice: []*utils.DataNode{utils.NewLeafNode("ID1"), utils.NewLeafNode("ID2")}} + expected[utils.CapStatQueues] = &utils.DataNode{Type: utils.NMSliceType, Slice: []*utils.DataNode{utils.NewLeafNode("Que1"), utils.NewLeafNode("Que2")}} if rply := v1PrcEvRpl.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } @@ -1518,26 +1518,26 @@ func TestSessionSV1ProcessMessageReplyAsNavigableMap(t *testing.T) { func TestV1ProcessEventReplyAsNavigableMap(t *testing.T) { //empty check v1per := new(V1ProcessEventReply) - expected := utils.NavigableMap{} + expected := map[string]*utils.DataNode{} if rply := v1per.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } //max usage check v1per.MaxUsage = map[string]time.Duration{utils.MetaDefault: 5 * time.Minute} - expected[utils.CapMaxUsage] = utils.NavigableMap{utils.MetaDefault: utils.NewNMData(5 * time.Minute)} + expected[utils.CapMaxUsage] = &utils.DataNode{Type: utils.NMMapType, Map: map[string]*utils.DataNode{utils.MetaDefault: utils.NewLeafNode(5 * time.Minute)}} if rply := v1per.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } //resource message check v1per.ResourceAllocation = map[string]string{utils.MetaDefault: "Resource"} - expected[utils.CapResourceAllocation] = utils.NavigableMap{utils.MetaDefault: utils.NewNMData("Resource")} + expected[utils.CapResourceAllocation] = &utils.DataNode{Type: utils.NMMapType, Map: map[string]*utils.DataNode{utils.MetaDefault: utils.NewLeafNode("Resource")}} if rply := v1per.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } //attributes check v1per.Attributes = make(map[string]*engine.AttrSProcessEventReply) v1per.Attributes[utils.MetaRaw] = attrs - expected[utils.CapAttributes] = utils.NavigableMap{utils.MetaRaw: utils.NavigableMap{"OfficeGroup": utils.NewNMData("Marketing")}} + expected[utils.CapAttributes] = &utils.DataNode{Type: utils.NMMapType, Map: map[string]*utils.DataNode{utils.MetaRaw: {Type: utils.NMMapType, Map: map[string]*utils.DataNode{"OfficeGroup": utils.NewLeafNode("Marketing")}}}} if rply := v1per.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } @@ -1548,7 +1548,7 @@ func TestV1ProcessEventReplyAsNavigableMap(t *testing.T) { nm := tmpRoutes.AsNavigableMap() v1per.RouteProfiles = make(map[string]engine.SortedRoutesList) v1per.RouteProfiles[utils.MetaRaw] = tmpRoutes - expected[utils.CapRouteProfiles] = utils.NavigableMap{utils.MetaRaw: &nm} + expected[utils.CapRouteProfiles] = &utils.DataNode{Type: utils.NMMapType, Map: map[string]*utils.DataNode{utils.MetaRaw: nm}} if rply := v1per.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } @@ -1556,7 +1556,7 @@ func TestV1ProcessEventReplyAsNavigableMap(t *testing.T) { tmpTresholdIDs := []string{"ID1", "ID2"} v1per.ThresholdIDs = map[string][]string{} v1per.ThresholdIDs[utils.MetaRaw] = tmpTresholdIDs - expected[utils.CapThresholds] = utils.NavigableMap{utils.MetaRaw: &utils.NMSlice{utils.NewNMData("ID1"), utils.NewNMData("ID2")}} + expected[utils.CapThresholds] = &utils.DataNode{Type: utils.NMMapType, Map: map[string]*utils.DataNode{utils.MetaRaw: {Type: utils.NMSliceType, Slice: []*utils.DataNode{utils.NewLeafNode("ID1"), utils.NewLeafNode("ID2")}}}} if rply := v1per.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } @@ -1564,7 +1564,7 @@ func TestV1ProcessEventReplyAsNavigableMap(t *testing.T) { tmpStatQueueIDs := []string{"Que1", "Que2"} v1per.StatQueueIDs = make(map[string][]string) v1per.StatQueueIDs[utils.MetaRaw] = tmpStatQueueIDs - expected[utils.CapStatQueues] = utils.NavigableMap{utils.MetaRaw: &utils.NMSlice{utils.NewNMData("Que1"), utils.NewNMData("Que2")}} + expected[utils.CapStatQueues] = &utils.DataNode{Type: utils.NMMapType, Map: map[string]*utils.DataNode{utils.MetaRaw: {Type: utils.NMSliceType, Slice: []*utils.DataNode{utils.NewLeafNode("Que1"), utils.NewLeafNode("Que2")}}}} if rply := v1per.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply) } @@ -1578,7 +1578,7 @@ func TestV1ProcessEventReplyAsNavigableMap(t *testing.T) { v1per.STIRIdentity = make(map[string]string) v1per.STIRIdentity[utils.MetaRaw] = utils.EmptyString - expected[utils.OptsStirIdentity] = utils.NavigableMap{utils.MetaRaw: utils.NewNMData(utils.EmptyString)} + expected[utils.OptsStirIdentity] = &utils.DataNode{Type: utils.NMMapType, Map: map[string]*utils.DataNode{utils.MetaRaw: utils.NewLeafNode(utils.EmptyString)}} if rply := v1per.AsNavigableMap(); !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting \n%+v\n, received: \n%+v", utils.ToJSON(expected), utils.ToJSON(rply)) } diff --git a/utils/orderednavigablemap.go b/utils/orderednavigablemap.go index edbca3876..204aa78a7 100644 --- a/utils/orderednavigablemap.go +++ b/utils/orderednavigablemap.go @@ -209,7 +209,7 @@ func (onm *OrderedNavigableMap) OrderedFieldsAsStrings() (flds []string) { flds = make([]string, 0, len(onm.nm.Map)) for el := onm.GetFirstElement(); el != nil; el = el.Next() { fld, _ := onm.Field(el.Value) - flds = append(flds, IfaceAsString(fld)) + flds = append(flds, fld.String()) } return }