mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-25 09:08:45 +05:00
Removed config.NavigableMap
This commit is contained in:
committed by
Dan Christian Bogos
parent
49108050a4
commit
f10f570a27
340
utils/mapstorage.go
Normal file
340
utils/mapstorage.go
Normal file
@@ -0,0 +1,340 @@
|
||||
/*
|
||||
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 (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// dataStorage is the DataProvider that can be updated
|
||||
type dataStorage interface {
|
||||
DataProvider
|
||||
|
||||
Set(fldPath []string, val interface{}) error
|
||||
Remove(fldPath []string) error
|
||||
GetKeys(nesteed bool) []string
|
||||
}
|
||||
|
||||
// MapStorage is the basic dataStorage
|
||||
type MapStorage map[string]interface{}
|
||||
|
||||
// String returns the map as json string
|
||||
func (ms MapStorage) String() string { return ToJSON(ms) }
|
||||
|
||||
// FieldAsInterface returns the value from the path
|
||||
func (ms MapStorage) FieldAsInterface(fldPath []string) (val interface{}, err error) {
|
||||
if len(fldPath) == 0 {
|
||||
err = errors.New("empty field path")
|
||||
return
|
||||
|
||||
}
|
||||
opath, indx := GetPathIndex(fldPath[0])
|
||||
var has bool
|
||||
if val, has = ms[opath]; !has {
|
||||
err = ErrNotFound
|
||||
return
|
||||
}
|
||||
if len(fldPath) == 1 {
|
||||
if indx == nil {
|
||||
return
|
||||
|
||||
}
|
||||
switch rv := val.(type) {
|
||||
case []string:
|
||||
if len(rv) <= *indx {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
val = rv[*indx]
|
||||
return
|
||||
case []interface{}:
|
||||
if len(rv) <= *indx {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
val = rv[*indx]
|
||||
return
|
||||
default:
|
||||
}
|
||||
// only if all above fails use reflect:
|
||||
vr := reflect.ValueOf(val)
|
||||
if vr.Kind() == reflect.Ptr {
|
||||
vr = vr.Elem()
|
||||
}
|
||||
if vr.Kind() != reflect.Slice && vr.Kind() != reflect.Array {
|
||||
return nil, ErrNotFound
|
||||
|
||||
}
|
||||
if *indx >= vr.Len() {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return vr.Index(*indx).Interface(), nil
|
||||
|
||||
}
|
||||
if indx == nil {
|
||||
switch dp := ms[fldPath[0]].(type) {
|
||||
case DataProvider:
|
||||
return dp.FieldAsInterface(fldPath[1:])
|
||||
case map[string]interface{}:
|
||||
return MapStorage(dp).FieldAsInterface(fldPath[1:])
|
||||
default:
|
||||
err = ErrWrongPath
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
switch dp := ms[opath].(type) {
|
||||
case []DataProvider:
|
||||
if len(dp) <= *indx {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return dp[*indx].FieldAsInterface(fldPath[1:])
|
||||
case []MapStorage:
|
||||
if len(dp) <= *indx {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return dp[*indx].FieldAsInterface(fldPath[1:])
|
||||
case []map[string]interface{}:
|
||||
if len(dp) <= *indx {
|
||||
return nil, ErrNotFound
|
||||
|
||||
}
|
||||
return MapStorage(dp[*indx]).FieldAsInterface(fldPath[1:])
|
||||
case []interface{}:
|
||||
if len(dp) <= *indx {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
switch ds := dp[*indx].(type) {
|
||||
case DataProvider:
|
||||
return ds.FieldAsInterface(fldPath[1:])
|
||||
case map[string]interface{}:
|
||||
return MapStorage(ds).FieldAsInterface(fldPath[1:])
|
||||
default:
|
||||
}
|
||||
default:
|
||||
|
||||
}
|
||||
err = ErrNotFound // xml compatible
|
||||
val = nil
|
||||
return
|
||||
}
|
||||
|
||||
// FieldAsString returns the value from path as string
|
||||
func (ms MapStorage) FieldAsString(fldPath []string) (str string, err error) {
|
||||
var val interface{}
|
||||
if val, err = ms.FieldAsInterface(fldPath); err != nil {
|
||||
return
|
||||
}
|
||||
return IfaceAsString(val), nil
|
||||
}
|
||||
|
||||
// Set sets the value at the given path
|
||||
func (ms MapStorage) Set(fldPath []string, val interface{}) (err error) {
|
||||
if len(fldPath) == 0 {
|
||||
return ErrWrongPath
|
||||
}
|
||||
if len(fldPath) == 1 {
|
||||
ms[fldPath[0]] = val
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if _, has := ms[fldPath[0]]; !has {
|
||||
nMap := MapStorage{}
|
||||
ms[fldPath[0]] = nMap
|
||||
return nMap.Set(fldPath[1:], val)
|
||||
}
|
||||
switch dp := ms[fldPath[0]].(type) {
|
||||
case dataStorage:
|
||||
return dp.Set(fldPath[1:], val)
|
||||
case map[string]interface{}:
|
||||
return MapStorage(dp).Set(fldPath[1:], val)
|
||||
default:
|
||||
return ErrWrongPath
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// GetKeys returns all the keys from map
|
||||
func (ms MapStorage) GetKeys(nesteed bool) (keys []string) {
|
||||
if !nesteed {
|
||||
keys = make([]string, len(ms))
|
||||
i := 0
|
||||
for k := range ms {
|
||||
keys[i] = k
|
||||
i++
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
for k, v := range ms {
|
||||
keys = append(keys, k)
|
||||
switch rv := v.(type) {
|
||||
case dataStorage:
|
||||
for _, dsKey := range rv.GetKeys(nesteed) {
|
||||
keys = append(keys, k+NestingSep+dsKey)
|
||||
}
|
||||
case map[string]interface{}:
|
||||
for _, dsKey := range MapStorage(rv).GetKeys(nesteed) {
|
||||
keys = append(keys, k+NestingSep+dsKey)
|
||||
}
|
||||
case []MapStorage:
|
||||
for i, dp := range rv {
|
||||
pref := k + fmt.Sprintf("[%v]", i)
|
||||
keys = append(keys, pref)
|
||||
for _, dsKey := range dp.GetKeys(nesteed) {
|
||||
keys = append(keys, pref+NestingSep+dsKey)
|
||||
}
|
||||
}
|
||||
case []dataStorage:
|
||||
for i, dp := range rv {
|
||||
pref := k + fmt.Sprintf("[%v]", i)
|
||||
keys = append(keys, pref)
|
||||
for _, dsKey := range dp.GetKeys(nesteed) {
|
||||
keys = append(keys, pref+NestingSep+dsKey)
|
||||
}
|
||||
}
|
||||
case []map[string]interface{}:
|
||||
for i, dp := range rv {
|
||||
pref := k + fmt.Sprintf("[%v]", i)
|
||||
keys = append(keys, pref)
|
||||
for _, dsKey := range MapStorage(dp).GetKeys(nesteed) {
|
||||
keys = append(keys, pref+NestingSep+dsKey)
|
||||
}
|
||||
}
|
||||
case []interface{}:
|
||||
for i := range rv {
|
||||
keys = append(keys, k+fmt.Sprintf("[%v]", i))
|
||||
}
|
||||
case []string:
|
||||
for i := range rv {
|
||||
keys = append(keys, k+fmt.Sprintf("[%v]", i))
|
||||
}
|
||||
default:
|
||||
// ToDo:should not be called
|
||||
keys = append(keys, getPathFromInterface(v, k+NestingSep)...)
|
||||
}
|
||||
|
||||
}
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
// Remove removes the item at path
|
||||
func (ms MapStorage) Remove(fldPath []string) (err error) {
|
||||
if len(fldPath) == 0 {
|
||||
return ErrWrongPath
|
||||
}
|
||||
var val interface{}
|
||||
var has bool
|
||||
if val, has = ms[fldPath[0]]; !has {
|
||||
return // ignore (already removed)
|
||||
}
|
||||
if len(fldPath) == 1 {
|
||||
delete(ms, fldPath[0])
|
||||
|
||||
return
|
||||
}
|
||||
switch dp := val.(type) {
|
||||
case dataStorage:
|
||||
return dp.Remove(fldPath[1:])
|
||||
case map[string]interface{}:
|
||||
return MapStorage(dp).Remove(fldPath[1:])
|
||||
default:
|
||||
return ErrWrongPath
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// RemoteHost is part of dataStorage interface
|
||||
func (ms MapStorage) RemoteHost() net.Addr {
|
||||
return LocalAddr()
|
||||
}
|
||||
|
||||
// ToDo: remove the following functions
|
||||
func getPathFromValue(in reflect.Value, prefix string) (out []string) {
|
||||
switch in.Kind() {
|
||||
case reflect.Ptr:
|
||||
return getPathFromValue(in.Elem(), prefix)
|
||||
case reflect.Array, reflect.Slice:
|
||||
prefix = strings.TrimSuffix(prefix, NestingSep)
|
||||
for i := 0; i < in.Len(); i++ {
|
||||
pref := fmt.Sprintf("%s[%v]", prefix, i)
|
||||
out = append(out, pref)
|
||||
out = append(out, getPathFromValue(in.Index(i), pref+NestingSep)...)
|
||||
}
|
||||
case reflect.Map:
|
||||
iter := reflect.ValueOf(in).MapRange()
|
||||
for iter.Next() {
|
||||
pref := prefix + iter.Key().String()
|
||||
out = append(out, pref)
|
||||
out = append(out, getPathFromValue(iter.Value(), pref+NestingSep)...)
|
||||
}
|
||||
case reflect.Struct:
|
||||
inType := in.Type()
|
||||
for i := 0; i < in.NumField(); i++ {
|
||||
pref := prefix + inType.Field(i).Name
|
||||
out = append(out, pref)
|
||||
out = append(out, getPathFromValue(in.Field(i), pref+NestingSep)...)
|
||||
}
|
||||
case reflect.Invalid, reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.String, reflect.Chan, reflect.Func, reflect.UnsafePointer, reflect.Interface:
|
||||
default:
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// used by MapStorage2 GetKeys to return all values
|
||||
func getPathFromInterface(in interface{}, prefix string) (out []string) {
|
||||
switch vin := in.(type) {
|
||||
case map[string]interface{}:
|
||||
for k, val := range vin {
|
||||
pref := prefix + k
|
||||
out = append(out, pref)
|
||||
out = append(out, getPathFromInterface(val, pref+NestingSep)...)
|
||||
}
|
||||
case []map[string]interface{}:
|
||||
prefix = strings.TrimSuffix(prefix, NestingSep)
|
||||
for i, val := range vin {
|
||||
pref := fmt.Sprintf("%s[%v]", prefix, i)
|
||||
out = append(out, pref)
|
||||
out = append(out, getPathFromInterface(val, pref+NestingSep)...)
|
||||
}
|
||||
case []interface{}:
|
||||
prefix = strings.TrimSuffix(prefix, NestingSep)
|
||||
for i, val := range vin {
|
||||
pref := fmt.Sprintf("%s[%v]", prefix, i)
|
||||
out = append(out, pref)
|
||||
out = append(out, getPathFromInterface(val, pref+NestingSep)...)
|
||||
}
|
||||
case []string:
|
||||
prefix = strings.TrimSuffix(prefix, NestingSep)
|
||||
for i := range vin {
|
||||
pref := fmt.Sprintf("%s[%v]", prefix, i)
|
||||
out = append(out, pref)
|
||||
}
|
||||
case nil, int, int32, int64, uint32, uint64, bool, float32, float64, []uint8, time.Duration, time.Time, string: //no path
|
||||
default: //reflect based
|
||||
out = getPathFromValue(reflect.ValueOf(vin), prefix)
|
||||
}
|
||||
return
|
||||
}
|
||||
348
utils/mapstorage_test.go
Normal file
348
utils/mapstorage_test.go
Normal file
@@ -0,0 +1,348 @@
|
||||
/*
|
||||
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"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNavMapGetFieldAsString(t *testing.T) {
|
||||
nM := MapStorage{
|
||||
"FirstLevel": map[string]interface{}{
|
||||
"SecondLevel": map[string]interface{}{
|
||||
"ThirdLevel": map[string]interface{}{
|
||||
"Fld1": "Val1",
|
||||
},
|
||||
},
|
||||
},
|
||||
"AnotherFirstLevel": "ValAnotherFirstLevel",
|
||||
}
|
||||
eVal := "Val1"
|
||||
if strVal, err := nM.FieldAsString(
|
||||
strings.Split("FirstLevel>SecondLevel>ThirdLevel>Fld1", ">")); err != nil {
|
||||
t.Error(err)
|
||||
} else if strVal != eVal {
|
||||
t.Errorf("expecting: <%s> received: <%s>", eVal, strVal)
|
||||
}
|
||||
eVal = "ValAnotherFirstLevel"
|
||||
if strVal, err := nM.FieldAsString(
|
||||
strings.Split("AnotherFirstLevel", ">")); err != nil {
|
||||
t.Error(err)
|
||||
} else if strVal != eVal {
|
||||
t.Errorf("expecting: <%s> received: <%s>", eVal, strVal)
|
||||
}
|
||||
fPath := "NonExisting>AnotherFirstLevel"
|
||||
if _, err := nM.FieldAsString(strings.Split(fPath, ">")); err.Error() !=
|
||||
ErrNotFound.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
type myEv map[string]interface{}
|
||||
|
||||
func (ev myEv) AsMapStorage() (MapStorage, error) {
|
||||
return MapStorage(ev), nil
|
||||
}
|
||||
|
||||
func TestNavMapAsMapStorage(t *testing.T) {
|
||||
myData := myEv{
|
||||
"FirstLevel": map[string]interface{}{
|
||||
"SecondLevel": map[string]interface{}{
|
||||
"ThirdLevel": map[string]interface{}{
|
||||
"Fld1": 123.123,
|
||||
},
|
||||
},
|
||||
},
|
||||
"FistLever2": map[string]interface{}{
|
||||
"SecondLevel2": map[string]interface{}{
|
||||
"Field2": 123,
|
||||
},
|
||||
"Field3": "Value3",
|
||||
},
|
||||
"Field4": &testStruct{
|
||||
Item1: "Ten",
|
||||
Item2: 10,
|
||||
},
|
||||
}
|
||||
|
||||
eNavMap := MapStorage{
|
||||
"FirstLevel": map[string]interface{}{
|
||||
"SecondLevel": map[string]interface{}{
|
||||
"ThirdLevel": map[string]interface{}{
|
||||
"Fld1": 123.123,
|
||||
},
|
||||
},
|
||||
},
|
||||
"FistLever2": map[string]interface{}{
|
||||
"SecondLevel2": map[string]interface{}{
|
||||
"Field2": 123,
|
||||
},
|
||||
"Field3": "Value3",
|
||||
},
|
||||
"Field4": &testStruct{
|
||||
Item1: "Ten",
|
||||
Item2: 10,
|
||||
},
|
||||
}
|
||||
|
||||
if rcv, err := myData.AsMapStorage(); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(eNavMap, rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eNavMap, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
type testStruct struct {
|
||||
Item1 string
|
||||
Item2 int
|
||||
}
|
||||
|
||||
func TestNavMapAdd2(t *testing.T) {
|
||||
nM := MapStorage{}
|
||||
path := []string{"FistLever2", "SecondLevel2", "Field2"}
|
||||
data := 123
|
||||
nM.Set(path, data)
|
||||
path = []string{"FirstLevel", "SecondLevel", "ThirdLevel", "Fld1"}
|
||||
data1 := 123.123
|
||||
nM.Set(path, data1)
|
||||
path = []string{"FistLever2", "Field3"}
|
||||
data2 := "Value3"
|
||||
nM.Set(path, data2)
|
||||
path = []string{"Field4"}
|
||||
data3 := &testStruct{
|
||||
Item1: "Ten",
|
||||
Item2: 10,
|
||||
}
|
||||
nM.Set(path, data3)
|
||||
eNavMap := MapStorage{
|
||||
"FirstLevel": MapStorage{
|
||||
"SecondLevel": MapStorage{
|
||||
"ThirdLevel": MapStorage{
|
||||
"Fld1": 123.123,
|
||||
},
|
||||
},
|
||||
},
|
||||
"FistLever2": MapStorage{
|
||||
"SecondLevel2": MapStorage{
|
||||
"Field2": 123,
|
||||
},
|
||||
"Field3": "Value3",
|
||||
},
|
||||
"Field4": &testStruct{
|
||||
Item1: "Ten",
|
||||
Item2: 10,
|
||||
},
|
||||
}
|
||||
if !reflect.DeepEqual(nM, eNavMap) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eNavMap, nM)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNavMapString(t *testing.T) {
|
||||
myData := map[string]interface{}{
|
||||
"FirstLevel": map[string]interface{}{
|
||||
"SecondLevel": map[string]interface{}{
|
||||
"ThirdLevel": map[string]interface{}{
|
||||
"Fld1": "Val1",
|
||||
},
|
||||
},
|
||||
},
|
||||
"FistLever2": map[string]interface{}{
|
||||
"SecondLevel2": map[string]interface{}{
|
||||
"Field2": "Value2",
|
||||
},
|
||||
"Field3": "Value3",
|
||||
},
|
||||
"Field4": "Val4",
|
||||
}
|
||||
nM := MapStorage(myData)
|
||||
eStr := ToJSON(myData)
|
||||
if !reflect.DeepEqual(nM.String(), eStr) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eStr, nM.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNavMapGetField(t *testing.T) {
|
||||
nM := MapStorage{
|
||||
"FirstLevel": map[string]interface{}{
|
||||
"SecondLevel": map[string]interface{}{
|
||||
"ThirdLevel": map[string]interface{}{
|
||||
"Fld1": []interface{}{"Val1", "Val2"},
|
||||
},
|
||||
},
|
||||
},
|
||||
"FirstLevel2": map[string]interface{}{
|
||||
"SecondLevel2": []map[string]interface{}{
|
||||
map[string]interface{}{
|
||||
"ThirdLevel2": map[string]interface{}{
|
||||
"Fld1": "Val1",
|
||||
},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"Count": 10,
|
||||
"ThirdLevel2": map[string]interface{}{
|
||||
"Fld2": []string{"Val1", "Val2", "Val3"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"AnotherFirstLevel": "ValAnotherFirstLevel",
|
||||
}
|
||||
pth := []string{"FirstLevel", "SecondLevel", "ThirdLevel", "Fld1[0]"}
|
||||
eFld := "Val1"
|
||||
if fld, err := nM.FieldAsInterface(pth); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(eFld, fld) {
|
||||
t.Errorf("expecting: %s, received: %s", ToIJSON(eFld), ToIJSON(fld))
|
||||
}
|
||||
eFld2 := map[string]interface{}{"Fld1": "Val1"}
|
||||
pth = []string{"FirstLevel2", "SecondLevel2[0]", "ThirdLevel2"}
|
||||
if fld, err := nM.FieldAsInterface(pth); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(eFld2, fld) {
|
||||
t.Errorf("expecting: %s, received: %s", ToIJSON(eFld2), ToIJSON(fld))
|
||||
}
|
||||
eFld3 := "ValAnotherFirstLevel"
|
||||
pth = []string{"AnotherFirstLevel"}
|
||||
if fld, err := nM.FieldAsInterface(pth); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(eFld3, fld) {
|
||||
t.Errorf("expecting: %s, received: %s", ToIJSON(eFld3), ToIJSON(fld))
|
||||
}
|
||||
pth = []string{"AnotherFirstLevel2"}
|
||||
if _, err := nM.FieldAsInterface(pth); err == nil || err != ErrNotFound {
|
||||
t.Error(err)
|
||||
}
|
||||
pth = []string{"FirstLevel", "SecondLevel[1]", "ThirdLevel", "Fld1[0]"}
|
||||
if _, err := nM.FieldAsInterface(pth); err == nil || err != ErrNotFound {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNavMapFieldAsInterface(t *testing.T) {
|
||||
nM := MapStorage{
|
||||
"FirstLevel": map[string]interface{}{
|
||||
"SecondLevel": []map[string]interface{}{
|
||||
map[string]interface{}{
|
||||
"ThirdLevel": map[string]interface{}{
|
||||
"Fld1": "Val1",
|
||||
},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"Count": 10,
|
||||
"ThirdLevel2": map[string]interface{}{
|
||||
"Fld2": []string{"Val1", "Val2", "Val3"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"AnotherFirstLevel": "ValAnotherFirstLevel",
|
||||
}
|
||||
|
||||
path := []string{"FirstLevel", "SecondLevel[0]", "Count"}
|
||||
expErr := ErrNotFound
|
||||
var eVal interface{} = nil
|
||||
if _, err := nM.FieldAsInterface(path); err != nil && err.Error() != expErr.Error() {
|
||||
t.Errorf("Expected error: %s, received error: %v", expErr.Error(), err)
|
||||
}
|
||||
|
||||
path = []string{"AnotherFirstLevel", "SecondLevel", "Count"}
|
||||
expErr = ErrWrongPath
|
||||
if _, err := nM.FieldAsInterface(path); err != nil && err.Error() != expErr.Error() {
|
||||
t.Errorf("Expected error: %s, received error: %v", expErr.Error(), err)
|
||||
}
|
||||
|
||||
path = []string{"FirstLevel", "SecondLevel[1]", "Count"}
|
||||
eVal = 10
|
||||
if rplyVal, err := nM.FieldAsInterface(path); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(eVal, rplyVal) {
|
||||
t.Errorf("Expected: %s , received: %s", ToJSON(eVal), ToJSON(rplyVal))
|
||||
}
|
||||
|
||||
path = []string{"FirstLevel", "SecondLevel[1]", "ThirdLevel2", "Fld2"}
|
||||
eVal = []string{"Val1", "Val2", "Val3"}
|
||||
if rplyVal, err := nM.FieldAsInterface(path); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(eVal, rplyVal) {
|
||||
t.Errorf("Expected: %s , received: %s", ToJSON(eVal), ToJSON(rplyVal))
|
||||
}
|
||||
|
||||
path = []string{"FirstLevel", "SecondLevel[1]", "ThirdLevel2", "Fld2[2]"}
|
||||
eVal = "Val3"
|
||||
if rplyVal, err := nM.FieldAsInterface(path); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(eVal, rplyVal) {
|
||||
t.Errorf("Expected: %s , received: %s", ToJSON(eVal), ToJSON(rplyVal))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNavMapGetKeys(t *testing.T) {
|
||||
navMp := MapStorage{
|
||||
"FirstLevel": map[string]interface{}{
|
||||
"SecondLevel": map[string]interface{}{
|
||||
"ThirdLevel": map[string]interface{}{
|
||||
"Fld1": 123.123,
|
||||
},
|
||||
},
|
||||
},
|
||||
"FistLever2": map[string]interface{}{
|
||||
"SecondLevel2": map[string]interface{}{
|
||||
"Field2": 123,
|
||||
},
|
||||
"Field3": "Value3",
|
||||
"Field4": &testStruct{
|
||||
Item1: "Ten",
|
||||
Item2: 10,
|
||||
},
|
||||
},
|
||||
"Field5": &testStruct{
|
||||
Item1: "Ten",
|
||||
Item2: 10,
|
||||
},
|
||||
"Field6": []string{"1", "2"},
|
||||
}
|
||||
expKeys := []string{
|
||||
"FirstLevel",
|
||||
"FirstLevel.SecondLevel",
|
||||
"FirstLevel.SecondLevel.ThirdLevel",
|
||||
"FirstLevel.SecondLevel.ThirdLevel.Fld1",
|
||||
"FistLever2",
|
||||
"FistLever2.SecondLevel2",
|
||||
"FistLever2.SecondLevel2.Field2",
|
||||
"FistLever2.Field3",
|
||||
"FistLever2.Field4",
|
||||
"FistLever2.Field4.Item1",
|
||||
"FistLever2.Field4.Item2",
|
||||
"Field5",
|
||||
"Field5.Item1",
|
||||
"Field5.Item2",
|
||||
"Field6",
|
||||
"Field6[0]",
|
||||
"Field6[1]",
|
||||
}
|
||||
keys := navMp.GetKeys(true)
|
||||
sort.Strings(expKeys)
|
||||
sort.Strings(keys)
|
||||
if !reflect.DeepEqual(expKeys, keys) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", ToJSON(expKeys), ToJSON(keys))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user