diff --git a/config/objdp.go b/config/objdp.go index 7fb4ad9b4..10ed91fb0 100644 --- a/config/objdp.go +++ b/config/objdp.go @@ -54,6 +54,7 @@ func (objDP *ObjectDP) String() string { // FieldAsInterface is part of engine.DataProvider interface func (objDP *ObjectDP) FieldAsInterface(fldPath []string) (data interface{}, err error) { + obj := objDP.obj // []string{ BalanceMap *monetary[0] Value } var has bool if data, has = objDP.getCache(strings.Join(fldPath, utils.NestingSep)); has { @@ -75,10 +76,9 @@ func (objDP *ObjectDP) FieldAsInterface(fldPath []string) (data interface{}, err } else { prevFld += utils.NestingSep + fld } - // check if we take the current path from cache if data, has = objDP.getCache(prevFld); !has { - if data, err = utils.ReflectFieldMethodInterface(objDP.obj, fld); err != nil { // take the object the field for current path + if data, err = utils.ReflectFieldMethodInterface(obj, fld); err != nil { // take the object the field for current path // in case of error set nil for the current path and return err objDP.setCache(prevFld, nil) return nil, err @@ -86,14 +86,13 @@ func (objDP *ObjectDP) FieldAsInterface(fldPath []string) (data interface{}, err // add the current field in prevFld so we can set in cache the full path with it's data objDP.setCache(prevFld, data) } - // change the obj to be the current data and continue the processing - objDP.obj = data + obj = data if slctrStr != utils.EmptyString { //we have selector so we need to do an aditional get prevFld += utils.IdxStart + slctrStr + utils.IdxEnd // check if we take the current path from cache if data, has = objDP.getCache(prevFld); !has { - if data, err = utils.ReflectFieldMethodInterface(objDP.obj, slctrStr); err != nil { // take the object the field for current path + if data, err = utils.ReflectFieldMethodInterface(obj, slctrStr); err != nil { // take the object the field for current path // in case of error set nil for the current path and return err objDP.setCache(prevFld, nil) return nil, err @@ -102,7 +101,7 @@ func (objDP *ObjectDP) FieldAsInterface(fldPath []string) (data interface{}, err objDP.setCache(prevFld, data) } // change the obj to be the current data and continue the processing - objDP.obj = data + obj = data } } diff --git a/engine/eventcost_test.go b/engine/eventcost_test.go index c888c9273..4a46a72a2 100644 --- a/engine/eventcost_test.go +++ b/engine/eventcost_test.go @@ -23,6 +23,7 @@ import ( "testing" "time" + "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" ) @@ -2281,3 +2282,27 @@ func TestECSyncKeys(t *testing.T) { utils.ToIJSON(eEC), utils.ToIJSON(ec)) } } + +func TestECAsDataProvider(t *testing.T) { + ecDP := config.NewObjectDP(testEC) + if data, err := ecDP.FieldAsInterface([]string{"RunID"}); err != nil { + t.Error(err) + } else if data != utils.META_DEFAULT { + t.Errorf("Expecting: <%s> \nreceived: <%s>", utils.META_DEFAULT, data) + } + if data, err := ecDP.FieldAsInterface([]string{"AccountSummary", "ID"}); err != nil { + t.Error(err) + } else if data != "dan" { + t.Errorf("Expecting: <%s> \nreceived: <%s>", "data", data) + } + if data, err := ecDP.FieldAsInterface([]string{"AccountSummary", "BalanceSummaries[1]", "ID"}); err != nil { + t.Error(err) + } else if data != "4b8b53d7-c1a1-4159-b845-4623a00a0165" { + t.Errorf("Expecting: <%s> \nreceived: <%s>", "4b8b53d7-c1a1-4159-b845-4623a00a0165", data) + } + if data, err := ecDP.FieldAsInterface([]string{"AccountSummary", "BalanceSummaries[2]", "Type"}); err != nil { + t.Error(err) + } else if data != "*voice" { + t.Errorf("Expecting: <%s> \nreceived: <%s>", "*voice", data) + } +}