mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-15 13:19:53 +05:00
Updated dynamic path
This commit is contained in:
committed by
Dan Christian Bogos
parent
e140eca3cf
commit
24cc53e94a
@@ -642,6 +642,12 @@ const (
|
||||
AttrValueSep = "="
|
||||
ANDSep = "&"
|
||||
PipeSep = "|"
|
||||
RSRConstSep = "`"
|
||||
RSRConstChar = '`'
|
||||
RSRDataConverterPrefix = "{*"
|
||||
RSRDataConverterSufix = "}"
|
||||
RSRDynStartChar = '<'
|
||||
RSRDynEndChar = '>'
|
||||
MetaApp = "*app"
|
||||
MetaAppID = "*appid"
|
||||
MetaCmd = "*cmd"
|
||||
|
||||
@@ -1,210 +0,0 @@
|
||||
/*
|
||||
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 (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NewDynamicDataProvider constructs a dynamic data provider
|
||||
func NewDynamicDataProvider(dp DataProvider) *DynamicDataProvider {
|
||||
return &DynamicDataProvider{
|
||||
DataProvider: dp,
|
||||
cache: make(map[string]interface{}),
|
||||
}
|
||||
}
|
||||
|
||||
// DynamicDataProvider is a data source from multiple formats
|
||||
type DynamicDataProvider struct {
|
||||
DataProvider
|
||||
cache map[string]interface{}
|
||||
}
|
||||
|
||||
// FieldAsInterface to overwrite the FieldAsInterface function from the given DataProvider
|
||||
func (ddp *DynamicDataProvider) FieldAsInterface(fldPath []string) (out interface{}, err error) {
|
||||
path := strings.Join(fldPath, NestingSep) // join the path so we can check it in cache and parse it more easy
|
||||
if val, has := ddp.cache[path]; has { // check if we have the path in cache
|
||||
return val, nil
|
||||
}
|
||||
var newPath string
|
||||
if newPath, err = ddp.processFieldPath(path); err != nil { // process the path
|
||||
return
|
||||
}
|
||||
if newPath == EmptyString { // no new path means no dynamic path so just take the value from the data provider
|
||||
return ddp.DataProvider.FieldAsInterface(fldPath)
|
||||
}
|
||||
// split the new path and get that field
|
||||
if out, err = ddp.DataProvider.FieldAsInterface(strings.Split(newPath, NestingSep)); err != nil {
|
||||
return
|
||||
}
|
||||
// if no error save in cache the path
|
||||
ddp.cache[path] = out
|
||||
return
|
||||
}
|
||||
|
||||
// GetFullFieldPath returns the full path for the
|
||||
func (ddp *DynamicDataProvider) GetFullFieldPath(fldPath string) (fpath *FullPath, err error) {
|
||||
var newPath string
|
||||
if newPath, err = ddp.processFieldPathForSet(fldPath); err != nil || newPath == EmptyString {
|
||||
return
|
||||
}
|
||||
fpath = &FullPath{
|
||||
PathItems: NewPathItems(strings.Split(newPath, NestingSep)),
|
||||
Path: newPath,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// FieldAsString returns the value from path as string
|
||||
func (ddp DynamicDataProvider) FieldAsString(fldPath []string) (str string, err error) {
|
||||
var val interface{}
|
||||
if val, err = ddp.FieldAsInterface(fldPath); err != nil {
|
||||
return
|
||||
}
|
||||
return IfaceAsString(val), nil
|
||||
}
|
||||
|
||||
// does the same thing as ... but replaces [ with . if the value between [] is dynamic
|
||||
func (ddp *DynamicDataProvider) processFieldPathForSet(fldPath string) (newPath string, err error) {
|
||||
idx := strings.Index(fldPath, IdxStart)
|
||||
if idx == -1 {
|
||||
return // no proccessing requred
|
||||
}
|
||||
var hasDyn bool // to be able to determine if the path has selector
|
||||
newPath = fldPath[:idx] // add the first path of the path without the "["
|
||||
for idx != -1 { // stop when we do not find any "["
|
||||
fldPath = fldPath[idx+1:] // move the path to the begining of the index
|
||||
nextBeginIdx := strings.Index(fldPath, IdxStart) // get the next "[" if any
|
||||
nextEndIdx := strings.Index(fldPath, IdxEnd) // get the next "]" if any
|
||||
if nextEndIdx == -1 { // no end index found so return error
|
||||
err = ErrWrongPath
|
||||
newPath = EmptyString
|
||||
return
|
||||
}
|
||||
|
||||
// parse the rest of the field path until we match the [ ]
|
||||
bIdx, eIdx := nextBeginIdx, nextEndIdx
|
||||
for nextBeginIdx != -1 && nextBeginIdx < nextEndIdx { // do this until no new [ is found or the next begining [ is after the end ]
|
||||
nextBeginIdx = strings.Index(fldPath[bIdx+1:], IdxStart) // get the next "[" if any
|
||||
nextEndIdx = strings.Index(fldPath[eIdx+1:], IdxEnd) // get the next "]" if any
|
||||
if nextEndIdx == -1 { // no end index found so return error
|
||||
err = ErrWrongPath
|
||||
newPath = EmptyString
|
||||
return
|
||||
}
|
||||
if nextBeginIdx == -1 { // if no index found do not increment but replace it
|
||||
bIdx = -1
|
||||
} else {
|
||||
bIdx += nextBeginIdx + 1
|
||||
}
|
||||
// increment the indexes
|
||||
eIdx += nextEndIdx + 1
|
||||
}
|
||||
var val string
|
||||
var isDyn bool
|
||||
for _, path := range strings.Split(fldPath[:eIdx], PipeSep) { // proccess the found path
|
||||
var iface interface{}
|
||||
if strings.HasPrefix(path, DynamicDataPrefix) {
|
||||
isDyn = true
|
||||
path2 := strings.TrimPrefix(path, DynamicDataPrefix)
|
||||
if iface, err = ddp.FieldAsInterface(strings.Split(path2, NestingSep)); err != nil {
|
||||
newPath = EmptyString
|
||||
return
|
||||
}
|
||||
val += IfaceAsString(iface) // compose the value
|
||||
} else {
|
||||
|
||||
val += IfaceAsString(path) // compose the value
|
||||
}
|
||||
}
|
||||
if isDyn {
|
||||
hasDyn = true
|
||||
val = NestingSep + val
|
||||
} else {
|
||||
val = IdxStart + val + IdxEnd
|
||||
}
|
||||
|
||||
if bIdx == -1 { // if is the last ocurence add the rest of the path and exit
|
||||
newPath += val + fldPath[eIdx+1:]
|
||||
} else {
|
||||
// else just add until the next [
|
||||
newPath += val + fldPath[eIdx+1:bIdx]
|
||||
}
|
||||
idx = bIdx
|
||||
}
|
||||
if !hasDyn { // the path doesn't have dynamic selector
|
||||
newPath = EmptyString
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ddp *DynamicDataProvider) processFieldPath(fldPath string) (newPath string, err error) {
|
||||
idx := strings.Index(fldPath, IdxStart)
|
||||
if idx == -1 {
|
||||
return // no proccessing requred
|
||||
}
|
||||
newPath = fldPath[:idx+1] // add the first path of the path with the "[" included
|
||||
for idx != -1 { // stop when we do not find any "["
|
||||
fldPath = fldPath[idx+1:] // move the path to the begining of the index
|
||||
nextBeginIdx := strings.Index(fldPath, IdxStart) // get the next "[" if any
|
||||
nextEndIdx := strings.Index(fldPath, IdxEnd) // get the next "]" if any
|
||||
if nextEndIdx == -1 { // no end index found so return error
|
||||
err = ErrWrongPath
|
||||
newPath = EmptyString
|
||||
return
|
||||
}
|
||||
|
||||
// parse the rest of the field path until we match the [ ]
|
||||
bIdx, eIdx := nextBeginIdx, nextEndIdx
|
||||
for nextBeginIdx != -1 && nextBeginIdx < nextEndIdx { // do this until no new [ is found or the next begining [ is after the end ]
|
||||
nextBeginIdx = strings.Index(fldPath[bIdx+1:], IdxStart) // get the next "[" if any
|
||||
nextEndIdx = strings.Index(fldPath[eIdx+1:], IdxEnd) // get the next "]" if any
|
||||
if nextEndIdx == -1 { // no end index found so return error
|
||||
err = ErrWrongPath
|
||||
newPath = EmptyString
|
||||
return
|
||||
}
|
||||
if nextBeginIdx == -1 { // if no index found do not increment but replace it
|
||||
bIdx = -1
|
||||
} else {
|
||||
bIdx += nextBeginIdx + 1
|
||||
}
|
||||
// increment the indexes
|
||||
eIdx += nextEndIdx + 1
|
||||
}
|
||||
var val string
|
||||
for _, path := range strings.Split(fldPath[:eIdx], PipeSep) { // proccess the found path
|
||||
var iface interface{}
|
||||
if iface, err = DPDynamicInterface(path, ddp); err != nil {
|
||||
newPath = EmptyString
|
||||
return
|
||||
}
|
||||
val += IfaceAsString(iface) // compose the value
|
||||
}
|
||||
if bIdx == -1 { // if is the last ocurence add the rest of the path and exit
|
||||
newPath += val + fldPath[eIdx:]
|
||||
} else {
|
||||
// else just add until the next [
|
||||
newPath += val + fldPath[eIdx:bIdx+1]
|
||||
}
|
||||
idx = bIdx
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1,254 +0,0 @@
|
||||
/*
|
||||
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 (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDynamicDataProviderFieldAsInterface(t *testing.T) {
|
||||
dp := MapStorage{
|
||||
MetaCgrep: MapStorage{
|
||||
"Stir": MapStorage{
|
||||
"CHRG_ROUTE1_END": "Identity1",
|
||||
"CHRG_ROUTE2_END": "Identity2",
|
||||
"CHRG_ROUTE3_END": "Identity3",
|
||||
"CHRG_ROUTE4_END": "Identity4",
|
||||
},
|
||||
"Routes": MapStorage{
|
||||
"SortedRoutes": []MapStorage{
|
||||
{"ID": "ROUTE1"},
|
||||
{"ID": "ROUTE2"},
|
||||
{"ID": "ROUTE3"},
|
||||
{"ID": "ROUTE4"},
|
||||
},
|
||||
},
|
||||
"BestRoute": 0,
|
||||
},
|
||||
}
|
||||
ddp := NewDynamicDataProvider(dp)
|
||||
path := "*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[~*cgrep.BestRoute].ID|_END]"
|
||||
out, err := ddp.FieldAsInterface(strings.Split(path, NestingSep))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expected := "Identity1"
|
||||
if out != expected {
|
||||
t.Errorf("Expected: %q,received %q", expected, out)
|
||||
}
|
||||
path = "*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[~*cgrep.BestRoute].ID|_END"
|
||||
_, err = ddp.FieldAsInterface(strings.Split(path, NestingSep))
|
||||
if err != ErrWrongPath {
|
||||
t.Errorf("Expected error %s received %v", ErrWrongPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDynamicDataProviderFieldAsInterface2(t *testing.T) {
|
||||
dp := MapStorage{
|
||||
MetaCgrep: MapStorage{
|
||||
"Stir": map[string]interface{}{
|
||||
"CHRG_ROUTE1_END": "Identity1",
|
||||
"CHRG_ROUTE2_END": "Identity2",
|
||||
"CHRG_ROUTE3_END": "Identity3",
|
||||
"CHRG_ROUTE4_END": "Identity4",
|
||||
},
|
||||
"Routes": map[string]interface{}{
|
||||
"SortedRoutes": []map[string]interface{}{
|
||||
{"ID": "ROUTE1"},
|
||||
{"ID": "ROUTE2"},
|
||||
{"ID": "ROUTE3"},
|
||||
{"ID": "ROUTE4"},
|
||||
},
|
||||
},
|
||||
"BestRoute": 0,
|
||||
},
|
||||
}
|
||||
ddp := NewDynamicDataProvider(dp)
|
||||
path := "*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[~*cgrep.BestRoute].ID|_END]"
|
||||
out, err := ddp.FieldAsInterface(strings.Split(path, NestingSep))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expected := "Identity1"
|
||||
if out != expected {
|
||||
t.Errorf("Expected: %q,received %q", expected, out)
|
||||
}
|
||||
path = "*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[~*cgrep.BestRoute].ID|_END"
|
||||
_, err = ddp.FieldAsInterface(strings.Split(path, NestingSep))
|
||||
if err != ErrWrongPath {
|
||||
t.Errorf("Expected error %s received %v", ErrWrongPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDynamicDataProviderProccesFieldPath2(t *testing.T) {
|
||||
dp := MapStorage{
|
||||
MetaCgrep: MapStorage{
|
||||
"Stir": MapStorage{
|
||||
"CHRG_ROUTE1_END": "Identity1",
|
||||
"CHRG_ROUTE2_END": "Identity2",
|
||||
"CHRG_ROUTE3_END": "Identity3",
|
||||
"CHRG_ROUTE4_END": "Identity4",
|
||||
},
|
||||
"Routes": MapStorage{
|
||||
"SortedRoutes": []MapStorage{
|
||||
{"ID": "ROUTE1"},
|
||||
{"ID": "ROUTE2"},
|
||||
{"ID": "ROUTE3"},
|
||||
{"ID": "ROUTE4"},
|
||||
},
|
||||
},
|
||||
"BestRoute": 0,
|
||||
},
|
||||
}
|
||||
ddp := NewDynamicDataProvider(dp)
|
||||
newpath, err := ddp.processFieldPathForSet("~*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[1].ID|_END].Something[CHRG_|~*cgrep.Routes.SortedRoutes[~*cgrep.BestRoute].ID|_END]")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expectedPath := "~*cgrep.Stir.CHRG_ROUTE2_END.Something.CHRG_ROUTE1_END"
|
||||
if newpath != expectedPath {
|
||||
t.Errorf("Expected: %q,received %q", expectedPath, newpath)
|
||||
}
|
||||
_, err = ddp.processFieldPathForSet("~*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[1].ID|_END].Something[CHRG_|~*cgrep.Routes.SortedRoutes[~*cgrep.BestRoute].ID|_END")
|
||||
if err != ErrWrongPath {
|
||||
t.Errorf("Expected error %s received %v", ErrWrongPath, err)
|
||||
}
|
||||
|
||||
_, err = ddp.processFieldPathForSet("~*cgrep.Stir[CHRG_")
|
||||
if err != ErrWrongPath {
|
||||
t.Errorf("Expected error %s received %v", ErrWrongPath, err)
|
||||
}
|
||||
|
||||
_, err = ddp.processFieldPathForSet("~*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[1].ID2|_END]")
|
||||
if err != ErrNotFound {
|
||||
t.Errorf("Expected error %s received %v", ErrNotFound, err)
|
||||
}
|
||||
newpath, err = ddp.processFieldPathForSet("~*cgrep.Stir[1]")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if newpath != EmptyString {
|
||||
t.Errorf("Expected: %q,received %q", EmptyString, newpath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDynamicDataProviderFieldAsString(t *testing.T) {
|
||||
dp := MapStorage{
|
||||
MetaCgrep: MapStorage{
|
||||
"Stir": map[string]interface{}{
|
||||
"CHRG_ROUTE1_END": "Identity1",
|
||||
"CHRG_ROUTE2_END": "Identity2",
|
||||
"CHRG_ROUTE3_END": "Identity3",
|
||||
"CHRG_ROUTE4_END": "Identity4",
|
||||
},
|
||||
"Routes": map[string]interface{}{
|
||||
"SortedRoutes": []map[string]interface{}{
|
||||
{"ID": "ROUTE1"},
|
||||
{"ID": "ROUTE2"},
|
||||
{"ID": "ROUTE3"},
|
||||
{"ID": "ROUTE4"},
|
||||
},
|
||||
},
|
||||
"BestRoute": 0,
|
||||
},
|
||||
}
|
||||
ddp := NewDynamicDataProvider(dp)
|
||||
path := "*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[~*cgrep.BestRoute].ID|_END]"
|
||||
out, err := ddp.FieldAsString(strings.Split(path, NestingSep))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expected := "Identity1"
|
||||
if out != expected {
|
||||
t.Errorf("Expected: %q,received %q", expected, out)
|
||||
}
|
||||
path = "*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[~*cgrep.BestRoute].ID|_END"
|
||||
_, err = ddp.FieldAsString(strings.Split(path, NestingSep))
|
||||
if err != ErrWrongPath {
|
||||
t.Errorf("Expected error %s received %v", ErrWrongPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDynamicDataProviderGetFullFieldPath(t *testing.T) {
|
||||
dp := MapStorage{
|
||||
MetaCgrep: MapStorage{
|
||||
"Stir": MapStorage{
|
||||
"CHRG_ROUTE1_END": "Identity1",
|
||||
"CHRG_ROUTE2_END": "Identity2",
|
||||
"CHRG_ROUTE3_END": "Identity3",
|
||||
"CHRG_ROUTE4_END": "Identity4",
|
||||
},
|
||||
"Routes": MapStorage{
|
||||
"SortedRoutes": []MapStorage{
|
||||
{"ID": "ROUTE1"},
|
||||
{"ID": "ROUTE2"},
|
||||
{"ID": "ROUTE3"},
|
||||
{"ID": "ROUTE4"},
|
||||
},
|
||||
},
|
||||
"BestRoute": 0,
|
||||
},
|
||||
}
|
||||
ddp := NewDynamicDataProvider(dp)
|
||||
newpath, err := ddp.GetFullFieldPath("~*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[1].ID|_END].Something[CHRG_|~*cgrep.Routes.SortedRoutes[~*cgrep.BestRoute].ID|_END]")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expectedPath := "~*cgrep.Stir.CHRG_ROUTE2_END.Something.CHRG_ROUTE1_END"
|
||||
if newpath.Path != expectedPath {
|
||||
t.Errorf("Expected: %q,received %q", expectedPath, newpath)
|
||||
}
|
||||
_, err = ddp.GetFullFieldPath("~*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[1].ID|_END].Something[CHRG_|~*cgrep.Routes.SortedRoutes[~*cgrep.BestRoute].ID|_END")
|
||||
if err != ErrWrongPath {
|
||||
t.Errorf("Expected error %s received %v", ErrWrongPath, err)
|
||||
}
|
||||
|
||||
_, err = ddp.GetFullFieldPath("~*cgrep.Stir[CHRG_")
|
||||
if err != ErrWrongPath {
|
||||
t.Errorf("Expected error %s received %v", ErrWrongPath, err)
|
||||
}
|
||||
|
||||
_, err = ddp.GetFullFieldPath("~*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[1].ID2|_END]")
|
||||
if err != ErrNotFound {
|
||||
t.Errorf("Expected error %s received %v", ErrNotFound, err)
|
||||
}
|
||||
newpath, err = ddp.GetFullFieldPath("~*cgrep.Stir[1]")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if newpath != nil {
|
||||
t.Errorf("Expected: %v,received %q", nil, newpath)
|
||||
}
|
||||
|
||||
newpath, err = ddp.GetFullFieldPath("~*cgrep.Stir")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if newpath != nil {
|
||||
t.Errorf("Expected: %v,received %q", nil, newpath)
|
||||
}
|
||||
|
||||
newpath, err = ddp.GetFullFieldPath("*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[~*cgrep.BestRoute].ID|_END]")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if newpath == nil {
|
||||
t.Errorf("Expected: %v,received %q", nil, newpath)
|
||||
}
|
||||
}
|
||||
91
utils/dynamicfieldpath.go
Normal file
91
utils/dynamicfieldpath.go
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
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 (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetFullFieldPath returns the full path for the
|
||||
func GetFullFieldPath(fldPath string, dP DataProvider) (fpath *FullPath, err error) {
|
||||
var newPath string
|
||||
if newPath, err = processFieldPath(fldPath, dP); err != nil || newPath == EmptyString {
|
||||
return
|
||||
}
|
||||
fpath = &FullPath{
|
||||
PathItems: NewPathItems(strings.Split(newPath, NestingSep)),
|
||||
Path: newPath,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// replaces the dynamic path between <>
|
||||
func processFieldPath(fldPath string, dP DataProvider) (newPath string, err error) {
|
||||
idx := strings.IndexByte(fldPath, RSRDynStartChar)
|
||||
if idx == -1 {
|
||||
return // no proccessing requred
|
||||
}
|
||||
newPath = fldPath[:idx] // add the first path of the path without the "<"
|
||||
for idx != -1 { // stop when we do not find any "<"
|
||||
fldPath = fldPath[idx+1:] // move the path to the begining of the index
|
||||
nextBeginIdx := strings.IndexByte(fldPath, RSRDynStartChar) // get the next "<" if any
|
||||
nextEndIdx := strings.IndexByte(fldPath, RSRDynEndChar) // get the next ">" if any
|
||||
if nextEndIdx == -1 { // no end index found so return error
|
||||
err = ErrWrongPath
|
||||
newPath = EmptyString
|
||||
return
|
||||
}
|
||||
|
||||
// parse the rest of the field path until we match the [ ]
|
||||
bIdx, eIdx := nextBeginIdx, nextEndIdx
|
||||
for nextBeginIdx != -1 && nextBeginIdx < nextEndIdx { // do this until no new [ is found or the next begining [ is after the end ]
|
||||
nextBeginIdx = strings.IndexByte(fldPath[bIdx+1:], RSRDynStartChar) // get the next "<" if any
|
||||
nextEndIdx = strings.IndexByte(fldPath[eIdx+1:], RSRDynEndChar) // get the next ">" if any
|
||||
if nextEndIdx == -1 { // no end index found so return error
|
||||
err = ErrWrongPath
|
||||
newPath = EmptyString
|
||||
return
|
||||
}
|
||||
if nextBeginIdx == -1 { // if no index found do not increment but replace it
|
||||
bIdx = -1
|
||||
} else {
|
||||
bIdx += nextBeginIdx + 1
|
||||
}
|
||||
// increment the indexes
|
||||
eIdx += nextEndIdx + 1
|
||||
}
|
||||
var val string
|
||||
for _, path := range strings.Split(fldPath[:eIdx], PipeSep) { // proccess the found path
|
||||
if val, err = DPDynamicString(path, dP); err != nil {
|
||||
newPath = EmptyString
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if bIdx == -1 { // if is the last ocurence add the rest of the path and exit
|
||||
newPath += val + fldPath[eIdx+1:]
|
||||
} else {
|
||||
// else just add until the next [
|
||||
newPath += val + fldPath[eIdx+1:bIdx]
|
||||
}
|
||||
idx = bIdx
|
||||
}
|
||||
return
|
||||
}
|
||||
140
utils/dynamicfieldpath_test.go
Normal file
140
utils/dynamicfieldpath_test.go
Normal file
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
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 (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDynamicDataProviderProccesFieldPath(t *testing.T) {
|
||||
dp := MapStorage{
|
||||
MetaCgrep: MapStorage{
|
||||
"Stir": MapStorage{
|
||||
"CHRG_ROUTE1_END": "Identity1",
|
||||
"CHRG_ROUTE2_END": "Identity2",
|
||||
"CHRG_ROUTE3_END": "Identity3",
|
||||
"CHRG_ROUTE4_END": "Identity4",
|
||||
},
|
||||
"Routes": MapStorage{
|
||||
"SortedRoutes": []MapStorage{
|
||||
{"ID": "ROUTE1"},
|
||||
{"ID": "ROUTE2"},
|
||||
{"ID": "ROUTE3"},
|
||||
{"ID": "ROUTE4"},
|
||||
},
|
||||
},
|
||||
"BestRoute": 0,
|
||||
},
|
||||
}
|
||||
newpath, err := processFieldPath("~*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[1].ID|_END].Something[CHRG_|~*cgrep.Routes.SortedRoutes[~*cgrep.BestRoute].ID|_END]", dp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expectedPath := "~*cgrep.Stir.CHRG_ROUTE2_END.Something.CHRG_ROUTE1_END"
|
||||
if newpath != expectedPath {
|
||||
t.Errorf("Expected: %q,received %q", expectedPath, newpath)
|
||||
}
|
||||
_, err = processFieldPath("~*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[1].ID|_END].Something[CHRG_|~*cgrep.Routes.SortedRoutes[~*cgrep.BestRoute].ID|_END", dp)
|
||||
if err != ErrWrongPath {
|
||||
t.Errorf("Expected error %s received %v", ErrWrongPath, err)
|
||||
}
|
||||
|
||||
_, err = processFieldPath("~*cgrep.Stir[CHRG_", dp)
|
||||
if err != ErrWrongPath {
|
||||
t.Errorf("Expected error %s received %v", ErrWrongPath, err)
|
||||
}
|
||||
|
||||
_, err = processFieldPath("~*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[1].ID2|_END]", dp)
|
||||
if err != ErrNotFound {
|
||||
t.Errorf("Expected error %s received %v", ErrNotFound, err)
|
||||
}
|
||||
newpath, err = processFieldPath("~*cgrep.Stir[1]", dp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if newpath != EmptyString {
|
||||
t.Errorf("Expected: %q,received %q", EmptyString, newpath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDynamicDataProviderGetFullFieldPath(t *testing.T) {
|
||||
dp := MapStorage{
|
||||
MetaCgrep: MapStorage{
|
||||
"Stir": MapStorage{
|
||||
"CHRG_ROUTE1_END": "Identity1",
|
||||
"CHRG_ROUTE2_END": "Identity2",
|
||||
"CHRG_ROUTE3_END": "Identity3",
|
||||
"CHRG_ROUTE4_END": "Identity4",
|
||||
},
|
||||
"Routes": MapStorage{
|
||||
"SortedRoutes": []MapStorage{
|
||||
{"ID": "ROUTE1"},
|
||||
{"ID": "ROUTE2"},
|
||||
{"ID": "ROUTE3"},
|
||||
{"ID": "ROUTE4"},
|
||||
},
|
||||
},
|
||||
"BestRoute": 0,
|
||||
},
|
||||
}
|
||||
newpath, err := GetFullFieldPath("~*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[1].ID|_END].Something[CHRG_|~*cgrep.Routes.SortedRoutes[~*cgrep.BestRoute].ID|_END]", dp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expectedPath := "~*cgrep.Stir.CHRG_ROUTE2_END.Something.CHRG_ROUTE1_END"
|
||||
if newpath.Path != expectedPath {
|
||||
t.Errorf("Expected: %q,received %q", expectedPath, newpath)
|
||||
}
|
||||
_, err = GetFullFieldPath("~*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[1].ID|_END].Something[CHRG_|~*cgrep.Routes.SortedRoutes[~*cgrep.BestRoute].ID|_END", dp)
|
||||
if err != ErrWrongPath {
|
||||
t.Errorf("Expected error %s received %v", ErrWrongPath, err)
|
||||
}
|
||||
|
||||
_, err = GetFullFieldPath("~*cgrep.Stir[CHRG_", dp)
|
||||
if err != ErrWrongPath {
|
||||
t.Errorf("Expected error %s received %v", ErrWrongPath, err)
|
||||
}
|
||||
|
||||
_, err = GetFullFieldPath("~*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[1].ID2|_END]", dp)
|
||||
if err != ErrNotFound {
|
||||
t.Errorf("Expected error %s received %v", ErrNotFound, err)
|
||||
}
|
||||
newpath, err = GetFullFieldPath("~*cgrep.Stir[1]", dp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if newpath != nil {
|
||||
t.Errorf("Expected: %v,received %q", nil, newpath)
|
||||
}
|
||||
|
||||
newpath, err = GetFullFieldPath("~*cgrep.Stir", dp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if newpath != nil {
|
||||
t.Errorf("Expected: %v,received %q", nil, newpath)
|
||||
}
|
||||
|
||||
newpath, err = GetFullFieldPath("*cgrep.Stir[CHRG_|~*cgrep.Routes.SortedRoutes[~*cgrep.BestRoute].ID|_END]", dp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if newpath == nil {
|
||||
t.Errorf("Expected: %v,received %q", nil, newpath)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user