mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-15 13:19:53 +05:00
Move config/objdp.go to utils package
and rename the file to objectdp.go
This commit is contained in:
committed by
Dan Christian Bogos
parent
3254e0d35f
commit
deaf5f4918
123
utils/objectdp.go
Normal file
123
utils/objectdp.go
Normal file
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
||||
Copyright (C) ITsysCOM GmbH
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NewObjectDP constructs a DataProvider
|
||||
func NewObjectDP(obj any) DataProvider {
|
||||
return &ObjectDP{
|
||||
obj: obj,
|
||||
cache: make(map[string]any),
|
||||
}
|
||||
}
|
||||
|
||||
// ObjectDP implements the DataProvider for any any
|
||||
type ObjectDP struct {
|
||||
obj any
|
||||
cache map[string]any
|
||||
}
|
||||
|
||||
func (objDP *ObjectDP) setCache(path string, val any) {
|
||||
objDP.cache[path] = val
|
||||
}
|
||||
|
||||
func (objDP *ObjectDP) getCache(path string) (val any, has bool) {
|
||||
val, has = objDP.cache[path]
|
||||
return
|
||||
}
|
||||
|
||||
// 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 ToJSON(objDP.obj)
|
||||
}
|
||||
|
||||
// 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, NestingSep)); has {
|
||||
if data == nil {
|
||||
err = ErrNotFound
|
||||
}
|
||||
return
|
||||
}
|
||||
data = obj // in case the fldPath is empty we need to return the whole object
|
||||
var prevFld string
|
||||
for _, fld := range fldPath {
|
||||
var slctrStr string
|
||||
if splt := strings.Split(fld, IdxStart); len(splt) != 1 { // check if we have selector
|
||||
fld = splt[0]
|
||||
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 == EmptyString {
|
||||
prevFld += fld
|
||||
} else {
|
||||
prevFld += NestingSep + fld
|
||||
}
|
||||
// check if we take the current path from cache
|
||||
if data, has = objDP.getCache(prevFld); !has {
|
||||
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
|
||||
}
|
||||
// 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
|
||||
obj = data
|
||||
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 = 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
|
||||
}
|
||||
// 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
|
||||
obj = data
|
||||
}
|
||||
|
||||
}
|
||||
//add in cache the initial path
|
||||
objDP.setCache(strings.Join(fldPath, NestingSep), data)
|
||||
return
|
||||
}
|
||||
|
||||
// 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 IfaceAsString(valIface), nil
|
||||
}
|
||||
222
utils/objectdp_test.go
Normal file
222
utils/objectdp_test.go
Normal file
@@ -0,0 +1,222 @@
|
||||
/*
|
||||
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
||||
Copyright (C) ITsysCOM GmbH
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewObjectDP(t *testing.T) {
|
||||
object := "cgrates.org"
|
||||
objDp := &ObjectDP{
|
||||
obj: "cgrates.org",
|
||||
cache: make(map[string]any),
|
||||
}
|
||||
if received := NewObjectDP(object); !reflect.DeepEqual(objDp, received) {
|
||||
t.Errorf("Expected %+v, received %+v", objDp, received)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringObjDP(t *testing.T) {
|
||||
objDp := &ObjectDP{
|
||||
obj: "cgrates.org",
|
||||
cache: make(map[string]any),
|
||||
}
|
||||
expected := `"cgrates.org"`
|
||||
if received := objDp.String(); !reflect.DeepEqual(expected, received) {
|
||||
t.Errorf("Expected %+v, received %+v", expected, received)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldAsInterfaceObjDPSliceOfInt(t *testing.T) {
|
||||
object := []string{"1"}
|
||||
objDp := &ObjectDP{
|
||||
obj: []int{12, 13},
|
||||
cache: make(map[string]any),
|
||||
}
|
||||
expected := 13
|
||||
if received, err := objDp.FieldAsInterface(object); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(received, expected) {
|
||||
t.Errorf("Expected %+v, received %+v", expected, received)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldAsInterfaceObjDPInvalidSyntax(t *testing.T) {
|
||||
object := []string{"1]"}
|
||||
objDp := &ObjectDP{
|
||||
obj: []int{12, 13},
|
||||
cache: make(map[string]any),
|
||||
}
|
||||
expected := "strconv.Atoi: parsing \"1]\": invalid syntax"
|
||||
if _, err := objDp.FieldAsInterface(object); err == nil || err.Error() != expected {
|
||||
t.Errorf("Expected %+v, received %+v", expected, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldAsInterfaceObjDPInvalidFormat(t *testing.T) {
|
||||
object := []string{"invalid[path"}
|
||||
objDp := &ObjectDP{
|
||||
obj: []int{12, 13},
|
||||
cache: make(map[string]any),
|
||||
}
|
||||
expected := "filter rule <path> needs to end in ]"
|
||||
if _, err := objDp.FieldAsInterface(object); err == nil || err.Error() != expected {
|
||||
t.Errorf("Expected %+v, received %+v", expected, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldAsInterfaceObjDPCache(t *testing.T) {
|
||||
object := []string{"validPath"}
|
||||
objDp := &ObjectDP{
|
||||
cache: map[string]any{
|
||||
"validPath": "cgrates.org",
|
||||
},
|
||||
}
|
||||
expected := "cgrates.org"
|
||||
if rcv, err := objDp.FieldAsInterface(object); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, rcv) {
|
||||
t.Errorf("Expected %+v, received %+v", expected, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldAsInterfaceObjDPChangedObject(t *testing.T) {
|
||||
object := []string{"0[1]"}
|
||||
objDp := &ObjectDP{
|
||||
obj: []int{1},
|
||||
cache: map[string]any{},
|
||||
}
|
||||
expected := "unsupported field kind: int"
|
||||
if _, err := objDp.FieldAsInterface(object); err == nil || err.Error() != expected {
|
||||
t.Errorf("Expected %+v, received %+v", expected, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldAsInterfaceObjDPValid1(t *testing.T) {
|
||||
object := []string{"0[1]"}
|
||||
objDp := &ObjectDP{
|
||||
obj: []map[string]any{
|
||||
{
|
||||
"1": 1,
|
||||
"2": 2,
|
||||
},
|
||||
},
|
||||
cache: map[string]any{},
|
||||
}
|
||||
if rcv, err := objDp.FieldAsInterface(object); err != nil {
|
||||
t.Error(err)
|
||||
} else if rcv != 1 {
|
||||
t.Errorf("Expected %+v, received %+v", 1, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldAsStringObjDP(t *testing.T) {
|
||||
object := []string{"0[1]"}
|
||||
objDp := &ObjectDP{
|
||||
obj: []map[string]any{
|
||||
{
|
||||
"1": 1,
|
||||
"2": 2,
|
||||
},
|
||||
},
|
||||
cache: map[string]any{},
|
||||
}
|
||||
if rcv, err := objDp.FieldAsString(object); err != nil {
|
||||
t.Error(err)
|
||||
} else if rcv != "1" {
|
||||
t.Errorf("Expected %+v, received %+v", 1, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldAsStringError(t *testing.T) {
|
||||
object := []string{"0[1]"}
|
||||
objDp := &ObjectDP{
|
||||
obj: []int{1},
|
||||
cache: map[string]any{},
|
||||
}
|
||||
expected := "unsupported field kind: int"
|
||||
if _, err := objDp.FieldAsString(object); err == nil || err.Error() != expected {
|
||||
t.Errorf("Expected %+v, received %+v", expected, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldAsInterfaceObjDPMultiplePaths(t *testing.T) {
|
||||
type aNewStruct struct {
|
||||
Field1 int
|
||||
Field2 int
|
||||
}
|
||||
type pNewStruct struct {
|
||||
Field3 aNewStruct
|
||||
Field4 int
|
||||
Field5 []string
|
||||
}
|
||||
objDp := &ObjectDP{
|
||||
obj: pNewStruct{
|
||||
Field3: aNewStruct{
|
||||
Field1: 2,
|
||||
Field2: 4,
|
||||
},
|
||||
Field4: 2,
|
||||
Field5: []string{"1", "2"},
|
||||
},
|
||||
cache: map[string]any{},
|
||||
}
|
||||
if rcv, err := objDp.FieldAsInterface([]string{"Field3", "Field2"}); err != nil {
|
||||
t.Error(err)
|
||||
} else if rcv != 4 {
|
||||
t.Errorf("Expected %+v, received %+v", 4, rcv)
|
||||
}
|
||||
|
||||
if rcv, err := objDp.FieldAsInterface([]string{"Field5[0]"}); err != nil {
|
||||
t.Error(err)
|
||||
} else if rcv != "1" {
|
||||
t.Errorf("Expected %+v, received %+v", "1", rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObjectDPFieldAsInterface(t *testing.T) {
|
||||
type aNewStruct struct {
|
||||
Field1 int
|
||||
Field2 int
|
||||
}
|
||||
type pNewStruct struct {
|
||||
Field3 aNewStruct
|
||||
Field4 int
|
||||
Field5 []string
|
||||
}
|
||||
objDp := &ObjectDP{
|
||||
obj: pNewStruct{
|
||||
Field3: aNewStruct{
|
||||
Field1: 2,
|
||||
Field2: 4,
|
||||
},
|
||||
Field4: 2,
|
||||
Field5: []string{""},
|
||||
},
|
||||
cache: map[string]any{
|
||||
"field1": nil,
|
||||
},
|
||||
}
|
||||
_, err := objDp.FieldAsInterface([]string{"field1"})
|
||||
if err == nil || err != ErrNotFound {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user