diff --git a/agents/libdns.go b/agents/libdns.go index 351f2170a..bf4bd24c7 100644 --- a/agents/libdns.go +++ b/agents/libdns.go @@ -24,7 +24,6 @@ import ( "strconv" "strings" - "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" "github.com/miekg/dns" ) @@ -54,8 +53,8 @@ func dnsWriteMsg(w dns.ResponseWriter, msg *dns.Msg) (err error) { func newDnsDP(req *dns.Msg) utils.DataProvider { return &dnsDP{ - req: config.NewObjectDP(req), - opts: config.NewObjectDP(req.IsEdns0()), + req: utils.NewObjectDP(req), + opts: utils.NewObjectDP(req.IsEdns0()), } } diff --git a/engine/dynamicdp.go b/engine/dynamicdp.go index 88024f332..7b328d130 100644 --- a/engine/dynamicdp.go +++ b/engine/dynamicdp.go @@ -24,7 +24,6 @@ import ( "github.com/nyaruka/phonenumbers" "github.com/cgrates/birpc/context" - "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" ) @@ -104,7 +103,7 @@ func (dDP *dynamicDP) fieldAsInterface(fldPath []string) (val any, err error) { return } //construct dataProvider from account and set it further - dp := config.NewObjectDP(account) + dp := utils.NewObjectDP(account) dDP.cache.Set(fldPath[:2], dp) return dp.FieldAsInterface(fldPath[2:]) case utils.MetaResources: @@ -114,7 +113,7 @@ func (dDP *dynamicDP) fieldAsInterface(fldPath []string) (val any, err error) { &utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: dDP.tenant, ID: fldPath[1]}}, &reply); err != nil { return nil, err } - dp := config.NewObjectDP(&reply) + dp := utils.NewObjectDP(&reply) dDP.cache.Set(fldPath[:2], dp) return dp.FieldAsInterface(fldPath[2:]) case utils.MetaStats: @@ -136,7 +135,7 @@ func (dDP *dynamicDP) fieldAsInterface(fldPath []string) (val any, err error) { if err := connMgr.Call(context.TODO(), dDP.trdConns, utils.TrendSv1GetTrendSummary, &utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: dDP.tenant, ID: fldPath[1]}}, &trendSum); err != nil { return nil, err } - dp := config.NewObjectDP(trendSum) + dp := utils.NewObjectDP(trendSum) dDP.cache.Set(fldPath[:2], dp) return dp.FieldAsInterface(fldPath[2:]) case utils.MetaRankings: @@ -145,7 +144,7 @@ func (dDP *dynamicDP) fieldAsInterface(fldPath []string) (val any, err error) { if err := connMgr.Call(context.TODO(), dDP.rnkConns, utils.RankingSv1GetRankingSummary, &utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: dDP.tenant, ID: fldPath[1]}}, &rankingSum); err != nil { return nil, err } - dp := config.NewObjectDP(rankingSum) + dp := utils.NewObjectDP(rankingSum) dDP.cache.Set(fldPath[:2], dp) return dp.FieldAsInterface(fldPath[2:]) case utils.MetaLibPhoneNumber: diff --git a/config/objdp.go b/utils/objectdp.go similarity index 72% rename from config/objdp.go rename to utils/objectdp.go index aad5f2631..6642de557 100644 --- a/config/objdp.go +++ b/utils/objectdp.go @@ -16,17 +16,15 @@ You should have received a copy of the GNU General Public License along with this program. If not, see */ -package config +package utils import ( "fmt" "strings" - - "github.com/cgrates/cgrates/utils" ) -// NewObjectDP constructs a utils.DataProvider -func NewObjectDP(obj any) utils.DataProvider { +// NewObjectDP constructs a DataProvider +func NewObjectDP(obj any) DataProvider { return &ObjectDP{ obj: obj, cache: make(map[string]any), @@ -48,20 +46,20 @@ func (objDP *ObjectDP) getCache(path string) (val any, has bool) { return } -// String is part of engine.utils.DataProvider interface +// String is part of engine.DataProvider interface // when called, it will display the already parsed values out of cache func (objDP *ObjectDP) String() string { - return utils.ToJSON(objDP.obj) + return ToJSON(objDP.obj) } -// FieldAsInterface is part of engine.utils.DataProvider interface +// FieldAsInterface is part of engine.DataProvider interface func (objDP *ObjectDP) FieldAsInterface(fldPath []string) (data any, err error) { obj := objDP.obj // []string{ BalanceMap *monetary[0] Value } var has bool - if data, has = objDP.getCache(strings.Join(fldPath, utils.NestingSep)); has { + if data, has = objDP.getCache(strings.Join(fldPath, NestingSep)); has { if data == nil { - err = utils.ErrNotFound + err = ErrNotFound } return } @@ -69,21 +67,21 @@ func (objDP *ObjectDP) FieldAsInterface(fldPath []string) (data any, err error) var prevFld string for _, fld := range fldPath { var slctrStr string - if splt := strings.Split(fld, utils.IdxStart); len(splt) != 1 { // check if we have selector + if splt := strings.Split(fld, IdxStart); len(splt) != 1 { // check if we have selector fld = splt[0] - if splt[1][len(splt[1])-1:] != utils.IdxEnd { + if splt[1][len(splt[1])-1:] != IdxEnd { return nil, fmt.Errorf("filter rule <%s> needs to end in ]", splt[1]) } slctrStr = splt[1][:len(splt[1])-1] // also strip the last ] } - if prevFld == utils.EmptyString { + if prevFld == EmptyString { prevFld += fld } else { - prevFld += utils.NestingSep + fld + prevFld += NestingSep + fld } // check if we take the current path from cache if data, has = objDP.getCache(prevFld); !has { - if data, err = utils.ReflectFieldMethodInterface(obj, fld); err != nil { // take the object the field for current path + if data, err = 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 @@ -93,11 +91,11 @@ func (objDP *ObjectDP) FieldAsInterface(fldPath []string) (data any, err error) } // change the obj to be the current data and continue the processing obj = data - if slctrStr != utils.EmptyString { //we have selector so we need to do an aditional get - prevFld += utils.IdxStart + slctrStr + utils.IdxEnd + if slctrStr != EmptyString { //we have selector so we need to do an aditional get + prevFld += IdxStart + slctrStr + IdxEnd // check if we take the current path from cache if data, has = objDP.getCache(prevFld); !has { - if data, err = utils.ReflectFieldMethodInterface(obj, slctrStr); err != nil { // take the object the field for current path + if data, err = 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 @@ -111,15 +109,15 @@ func (objDP *ObjectDP) FieldAsInterface(fldPath []string) (data any, err error) } //add in cache the initial path - objDP.setCache(strings.Join(fldPath, utils.NestingSep), data) + objDP.setCache(strings.Join(fldPath, NestingSep), data) return } -// FieldAsString is part of engine.utils.DataProvider interface +// FieldAsString is part of engine.DataProvider interface func (objDP *ObjectDP) FieldAsString(fldPath []string) (data string, err error) { var valIface any if valIface, err = objDP.FieldAsInterface(fldPath); err != nil { return } - return utils.IfaceAsString(valIface), nil + return IfaceAsString(valIface), nil } diff --git a/config/objdp_test.go b/utils/objectdp_test.go similarity index 97% rename from config/objdp_test.go rename to utils/objectdp_test.go index 1ec34176a..d88dfe2c8 100644 --- a/config/objdp_test.go +++ b/utils/objectdp_test.go @@ -16,13 +16,11 @@ You should have received a copy of the GNU General Public License along with this program. If not, see */ -package config +package utils import ( "reflect" "testing" - - "github.com/cgrates/cgrates/utils" ) func TestNewObjectDP(t *testing.T) { @@ -194,7 +192,7 @@ func TestFieldAsInterfaceObjDPMultiplePaths(t *testing.T) { } } -func TestFieldAsInterface(t *testing.T) { +func TestObjectDPFieldAsInterface(t *testing.T) { type aNewStruct struct { Field1 int Field2 int @@ -218,7 +216,7 @@ func TestFieldAsInterface(t *testing.T) { }, } _, err := objDp.FieldAsInterface([]string{"field1"}) - if err == nil || err != utils.ErrNotFound { + if err == nil || err != ErrNotFound { t.Error(err) } }