mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Finished implementing the DataNode as a replacement
This commit is contained in:
committed by
Dan Christian Bogos
parent
fc2f7631bd
commit
c13a5cf790
@@ -28,7 +28,7 @@ func TestOrderedNavigableMap(t *testing.T) {
|
||||
|
||||
onm.Set(&FullPath{
|
||||
Path: "Field1",
|
||||
PathItems: []string{"Field1"},
|
||||
PathSlice: []string{"Field1"},
|
||||
}, NewLeafNode(10))
|
||||
expOrder := [][]string{{"Field1"}}
|
||||
if !reflect.DeepEqual(expOrder, onm.GetOrder()) {
|
||||
@@ -37,7 +37,7 @@ func TestOrderedNavigableMap(t *testing.T) {
|
||||
|
||||
onm.Set(&FullPath{
|
||||
Path: "Field2[0]",
|
||||
PathItems: []string{"Field2", "0"},
|
||||
PathSlice: []string{"Field2", "0"},
|
||||
}, NewLeafNode("1001"))
|
||||
expOrder = [][]string{
|
||||
{"Field1"},
|
||||
@@ -49,7 +49,7 @@ func TestOrderedNavigableMap(t *testing.T) {
|
||||
|
||||
onm.Set(&FullPath{
|
||||
Path: "Field2[1].Account[0]",
|
||||
PathItems: []string{"Field2", "1", "Account", "0"},
|
||||
PathSlice: []string{"Field2", "1", "Account", "0"},
|
||||
}, NewLeafNode(10))
|
||||
expOrder = [][]string{
|
||||
{"Field1"},
|
||||
@@ -62,7 +62,7 @@ func TestOrderedNavigableMap(t *testing.T) {
|
||||
|
||||
onm.Set(&FullPath{
|
||||
Path: "Field2[1].Account[1]",
|
||||
PathItems: []string{"Field2", "1", "Account", "1"},
|
||||
PathSlice: []string{"Field2", "1", "Account", "1"},
|
||||
}, NewLeafNode(11))
|
||||
expOrder = [][]string{
|
||||
{"Field1"},
|
||||
@@ -76,7 +76,7 @@ func TestOrderedNavigableMap(t *testing.T) {
|
||||
|
||||
onm.Set(&FullPath{
|
||||
Path: "Field2[2]",
|
||||
PathItems: []string{"Field2", "2"},
|
||||
PathSlice: []string{"Field2", "2"},
|
||||
}, NewLeafNode(111))
|
||||
expOrder = [][]string{
|
||||
{"Field1"},
|
||||
@@ -91,7 +91,7 @@ func TestOrderedNavigableMap(t *testing.T) {
|
||||
|
||||
onm.Set(&FullPath{
|
||||
Path: "Field3.Field4.Field5",
|
||||
PathItems: []string{"Field3", "Field4", "Field5"},
|
||||
PathSlice: []string{"Field3", "Field4", "Field5"},
|
||||
}, NewLeafNode(5))
|
||||
expOrder = [][]string{
|
||||
{"Field1"},
|
||||
@@ -133,7 +133,7 @@ func TestOrderedNavigableMap(t *testing.T) {
|
||||
// sliceDeNM
|
||||
exp := []*DataNode{NewLeafNode("500"), NewLeafNode("502")}
|
||||
path := []string{"Field2"}
|
||||
if err := onm.SetAsSlice(&FullPath{Path: path[0], PathItems: path}, exp); err != nil {
|
||||
if err := onm.SetAsSlice(&FullPath{Path: path[0], PathSlice: path}, exp); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
path = []string{"Field2"}
|
||||
@@ -231,11 +231,11 @@ func TestOrderedNavigableMapEmpty(t *testing.T) {
|
||||
func TestOrderedNavigableMapGetSet(t *testing.T) {
|
||||
nm := NewOrderedNavigableMap()
|
||||
nm.Set(&FullPath{
|
||||
PathItems: []string{"Account", "0"},
|
||||
PathSlice: []string{"Account", "0"},
|
||||
Path: "Account",
|
||||
}, NewLeafNode(1001))
|
||||
nm.Set(&FullPath{
|
||||
PathItems: []string{"Account", "1"},
|
||||
PathSlice: []string{"Account", "1"},
|
||||
Path: "Account",
|
||||
}, NewLeafNode("account_on_new_branch"))
|
||||
|
||||
@@ -280,7 +280,7 @@ func TestOrderedNavigableMapGetSet(t *testing.T) {
|
||||
}
|
||||
|
||||
path = []string{"Field2", "2"}
|
||||
if err := nm.Set(&FullPath{Path: strings.Join(path, NestingSep), PathItems: path}, NewLeafNode("500")); err != nil {
|
||||
if err := nm.Set(&FullPath{Path: strings.Join(path, NestingSep), PathSlice: path}, NewLeafNode("500")); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if val, err := nm.Field(path); err != nil {
|
||||
@@ -290,7 +290,7 @@ func TestOrderedNavigableMapGetSet(t *testing.T) {
|
||||
}
|
||||
|
||||
path = []string{"Field2", "1", "Account"}
|
||||
if err := nm.Set(&FullPath{Path: strings.Join(path, NestingSep), PathItems: path}, NewLeafNode("5")); err != nil {
|
||||
if err := nm.Set(&FullPath{Path: strings.Join(path, NestingSep), PathSlice: path}, NewLeafNode("5")); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
path = []string{"Field2", "1", "Account"}
|
||||
@@ -350,19 +350,19 @@ func TestOrderedNavigableMapFieldAsString(t *testing.T) {
|
||||
func TestOrderedNavigableMapGetOrder(t *testing.T) {
|
||||
nm := NewOrderedNavigableMap()
|
||||
nm.Set(&FullPath{
|
||||
PathItems: []string{"Field1", "Field2", "0"},
|
||||
PathSlice: []string{"Field1", "Field2", "0"},
|
||||
Path: "Field1.Field2[0]",
|
||||
}, NewLeafNode("1003"))
|
||||
nm.Set(&FullPath{
|
||||
PathItems: []string{"Field1", "Field2", "1"},
|
||||
PathSlice: []string{"Field1", "Field2", "1"},
|
||||
Path: "Field1.Field2[1]",
|
||||
}, NewLeafNode("Val"))
|
||||
nm.Set(&FullPath{
|
||||
PathItems: []string{"Field3", "Field4", "Field5", "0"},
|
||||
PathSlice: []string{"Field3", "Field4", "Field5", "0"},
|
||||
Path: "Field3.Field4.Field5",
|
||||
}, NewLeafNode("1001"))
|
||||
nm.Set(&FullPath{
|
||||
PathItems: []string{"Field1", "Field2", "2"},
|
||||
PathSlice: []string{"Field1", "Field2", "2"},
|
||||
Path: "Field1.Field2[2]",
|
||||
}, NewLeafNode(101))
|
||||
expected := [][]string{
|
||||
@@ -386,7 +386,7 @@ func TestOrderedNavigableMapSet(t *testing.T) {
|
||||
|
||||
path := []string{"Field1", "0"}
|
||||
if err := nm.Set(&FullPath{
|
||||
PathItems: path,
|
||||
PathSlice: path,
|
||||
Path: strings.Join(path, NestingSep),
|
||||
}, NewLeafNode("1001")); err != nil {
|
||||
t.Error(err)
|
||||
@@ -400,20 +400,20 @@ func TestOrderedNavigableMapSet(t *testing.T) {
|
||||
t.Errorf("Expected %s ,received: %s", order, nm.GetOrder())
|
||||
}
|
||||
if err := nm.Set(&FullPath{
|
||||
PathItems: []string{"Field1", "0", ""},
|
||||
PathSlice: []string{"Field1", "0", ""},
|
||||
Path: "Field1[0]",
|
||||
}, NewLeafNode("1001")); err != ErrWrongPath {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if err := nm.SetAsSlice(&FullPath{
|
||||
PathItems: []string{"Field1", "0", ""},
|
||||
PathSlice: []string{"Field1", "0", ""},
|
||||
Path: "Field1[0]",
|
||||
}, []*DataNode{}); err != ErrWrongPath {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := nm.SetAsSlice(&FullPath{
|
||||
PathItems: []string{"Field1", "10"},
|
||||
PathSlice: []string{"Field1", "10"},
|
||||
Path: "Field[10]",
|
||||
}, []*DataNode{}); err != ErrNotFound {
|
||||
t.Error(err)
|
||||
@@ -422,7 +422,7 @@ func TestOrderedNavigableMapSet(t *testing.T) {
|
||||
nMap = &DataNode{Type: NMMapType, Map: map[string]*DataNode{"Field1": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1002")}}}}
|
||||
order = [][]string{path}
|
||||
if err := nm.Set(&FullPath{
|
||||
PathItems: path,
|
||||
PathSlice: path,
|
||||
Path: strings.Join(path, NestingSep),
|
||||
}, NewLeafNode("1002")); err != nil {
|
||||
t.Error(err)
|
||||
@@ -440,7 +440,7 @@ func TestOrderedNavigableMapSet(t *testing.T) {
|
||||
}}
|
||||
order = append(order, path)
|
||||
if err := nm.Set(&FullPath{
|
||||
PathItems: path,
|
||||
PathSlice: path,
|
||||
Path: strings.Join(path, NestingSep),
|
||||
}, NewLeafNode("1002")); err != nil {
|
||||
t.Error(err)
|
||||
@@ -458,7 +458,7 @@ func TestOrderedNavigableMapSet(t *testing.T) {
|
||||
}}
|
||||
order = append(order, path)
|
||||
if err := nm.Set(&FullPath{
|
||||
PathItems: path,
|
||||
PathSlice: path,
|
||||
Path: strings.Join(path, NestingSep),
|
||||
}, NewLeafNode("1003")); err != nil {
|
||||
t.Error(err)
|
||||
@@ -478,7 +478,7 @@ func TestOrderedNavigableMapSet(t *testing.T) {
|
||||
}}
|
||||
order = append(order, []string{"Field3", "0"}, []string{"Field3", "1"})
|
||||
if err := nm.SetAsSlice(&FullPath{
|
||||
PathItems: path,
|
||||
PathSlice: path,
|
||||
Path: strings.Join(path, NestingSep),
|
||||
}, obj.Slice); err != nil {
|
||||
t.Error(err)
|
||||
@@ -503,7 +503,7 @@ func TestOrderedNavigableMapSet(t *testing.T) {
|
||||
{"Field1", "1"},
|
||||
}
|
||||
if err := nm.SetAsSlice(&FullPath{
|
||||
PathItems: []string{"Field1"},
|
||||
PathSlice: []string{"Field1"},
|
||||
Path: "Field1",
|
||||
}, obj.Slice); err != nil {
|
||||
t.Error(err)
|
||||
@@ -514,63 +514,64 @@ func TestOrderedNavigableMapSet(t *testing.T) {
|
||||
if !reflect.DeepEqual(nm.GetOrder(), order) {
|
||||
t.Errorf("Expected %s ,received: %s", order, nm.GetOrder())
|
||||
}
|
||||
obj = &DataNode{Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1005"), NewLeafNode("1006")}}
|
||||
nMap = &DataNode{Type: NMMapType, Map: map[string]*DataNode{
|
||||
"Field1": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1005"), NewLeafNode("1006")}},
|
||||
"Field2": NewLeafNode("1002"),
|
||||
"Field3": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1004"), NewLeafNode("1007")}},
|
||||
}}
|
||||
order = [][]string{
|
||||
{"Field2"},
|
||||
{"Field3", "0"},
|
||||
{"Field1", "0"},
|
||||
{"Field1", "1"},
|
||||
{"Field3", "1"},
|
||||
}
|
||||
if err := nm.Set(&FullPath{
|
||||
PathItems: []string{"Field3", "-1"},
|
||||
Path: "Field3[-1]",
|
||||
}, NewLeafNode("1007")); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !reflect.DeepEqual(nm.nm, nMap) {
|
||||
t.Errorf("Expected %s ,received: %s", ToJSON(nMap), ToJSON(nm))
|
||||
}
|
||||
if !reflect.DeepEqual(nm.GetOrder(), order) {
|
||||
t.Errorf("Expected %s ,received: %s", ToJSON(order), ToJSON(nm.GetOrder()))
|
||||
}
|
||||
// try dynamic path
|
||||
// obj = &DataNode{Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1005"), NewLeafNode("1006")}}
|
||||
// nMap = &DataNode{Type: NMMapType, Map: map[string]*DataNode{
|
||||
// "Field1": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1005"), NewLeafNode("1006")}},
|
||||
// "Field2": NewLeafNode("1002"),
|
||||
// "Field3": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1004"), NewLeafNode("1007")}},
|
||||
// }}
|
||||
// order = [][]string{
|
||||
// {"Field2"},
|
||||
// {"Field3", "0"},
|
||||
// {"Field1", "0"},
|
||||
// {"Field1", "1"},
|
||||
// {"Field3", "1"},
|
||||
// }
|
||||
// if err := nm.Set(&FullPath{
|
||||
// PathSlice: []string{"Field3", "-1"},
|
||||
// Path: "Field3[-1]",
|
||||
// }, NewLeafNode("1007")); err != nil {
|
||||
// t.Error(err)
|
||||
// }
|
||||
// if !reflect.DeepEqual(nm.nm, nMap) {
|
||||
// t.Errorf("Expected %s ,received: %s", ToJSON(nMap), ToJSON(nm))
|
||||
// }
|
||||
// if !reflect.DeepEqual(nm.GetOrder(), order) {
|
||||
// t.Errorf("Expected %s ,received: %s", ToJSON(order), ToJSON(nm.GetOrder()))
|
||||
// }
|
||||
}
|
||||
|
||||
func TestOrderedNavigableMapRemove(t *testing.T) {
|
||||
nm := NewOrderedNavigableMap()
|
||||
nm.Set(&FullPath{
|
||||
PathItems: []string{"Field2"},
|
||||
PathSlice: []string{"Field2"},
|
||||
Path: "Field2",
|
||||
}, NewLeafNode("1003"))
|
||||
nm.Set(&FullPath{
|
||||
PathItems: []string{"Field3", "Field4"},
|
||||
PathSlice: []string{"Field3", "Field4"},
|
||||
Path: "Field3.Field4",
|
||||
}, NewLeafNode("Val"))
|
||||
nm.Set(&FullPath{
|
||||
PathItems: []string{"Field1"},
|
||||
PathSlice: []string{"Field1"},
|
||||
Path: "Field1",
|
||||
}, NewLeafNode("1001"))
|
||||
nm.SetAsSlice(&FullPath{
|
||||
PathItems: []string{"Field5"},
|
||||
PathSlice: []string{"Field5"},
|
||||
Path: "Field5",
|
||||
}, []*DataNode{NewLeafNode(10), NewLeafNode(101)})
|
||||
|
||||
if err := nm.Remove(&FullPath{}); err != ErrWrongPath {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := nm.Remove(&FullPath{PathItems: []string{}}); err != ErrWrongPath {
|
||||
if err := nm.Remove(&FullPath{PathSlice: []string{}}); err != ErrWrongPath {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := nm.Remove(&FullPath{PathItems: []string{"field"}, Path: "field"}); err != nil {
|
||||
if err := nm.Remove(&FullPath{PathSlice: []string{"field"}, Path: "field"}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if err := nm.Remove(&FullPath{PathItems: []string{"-1", ""}}); err != ErrWrongPath {
|
||||
if err := nm.Remove(&FullPath{PathSlice: []string{"-1", ""}}); err != ErrWrongPath {
|
||||
t.Error(err)
|
||||
}
|
||||
nMap := &DataNode{Type: NMMapType, Map: map[string]*DataNode{
|
||||
@@ -604,7 +605,7 @@ func TestOrderedNavigableMapRemove(t *testing.T) {
|
||||
{"Field5", "1"},
|
||||
}
|
||||
|
||||
if err := nm.Remove(&FullPath{PathItems: []string{"Field2"}, Path: "Field2"}); err != nil {
|
||||
if err := nm.Remove(&FullPath{PathSlice: []string{"Field2"}, Path: "Field2"}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !reflect.DeepEqual(nm.nm, nMap) {
|
||||
@@ -622,7 +623,7 @@ func TestOrderedNavigableMapRemove(t *testing.T) {
|
||||
{"Field1"},
|
||||
}
|
||||
|
||||
if err := nm.Remove(&FullPath{PathItems: []string{"Field5"}, Path: "Field5"}); err != nil {
|
||||
if err := nm.Remove(&FullPath{PathSlice: []string{"Field5"}, Path: "Field5"}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !reflect.DeepEqual(nm.nm, nMap) {
|
||||
@@ -631,7 +632,7 @@ func TestOrderedNavigableMapRemove(t *testing.T) {
|
||||
if !reflect.DeepEqual(nm.GetOrder(), order) {
|
||||
t.Errorf("Expected %s ,received: %s", order, nm.GetOrder())
|
||||
}
|
||||
if err := nm.Remove(&FullPath{PathItems: []string{"Field1", "0", ""}}); err != ErrWrongPath {
|
||||
if err := nm.Remove(&FullPath{PathSlice: []string{"Field1", "0", ""}}); err != ErrWrongPath {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
@@ -802,19 +803,19 @@ func BenchmarkNavigableMapField(b *testing.B) {
|
||||
func TestOrderedNavigableMapRemoveAll(t *testing.T) {
|
||||
nm := NewOrderedNavigableMap()
|
||||
nm.Set(&FullPath{
|
||||
PathItems: []string{"Field2"},
|
||||
PathSlice: []string{"Field2"},
|
||||
Path: "Field2",
|
||||
}, NewLeafNode("1003"))
|
||||
nm.Set(&FullPath{
|
||||
PathItems: []string{"Field3", "Field4"},
|
||||
PathSlice: []string{"Field3", "Field4"},
|
||||
Path: "Field3.Field4",
|
||||
}, NewLeafNode("Val"))
|
||||
nm.Set(&FullPath{
|
||||
PathItems: []string{"Field1"},
|
||||
PathSlice: []string{"Field1"},
|
||||
Path: "Field1",
|
||||
}, NewLeafNode("1001"))
|
||||
nm.SetAsSlice(&FullPath{
|
||||
PathItems: []string{"Field5"},
|
||||
PathSlice: []string{"Field5"},
|
||||
Path: "Field5",
|
||||
}, []*DataNode{NewLeafNode(10), NewLeafNode(101)})
|
||||
expected := NewOrderedNavigableMap()
|
||||
@@ -831,7 +832,7 @@ func TestOrderedNavigableMapRemove2(t *testing.T) {
|
||||
}},
|
||||
}
|
||||
expErr := `strconv.Atoi: parsing "nan": invalid syntax`
|
||||
if err := nm.Remove(&FullPath{PathItems: []string{"Field1", "nan", ""}, Path: "Field1[nan]"}); err == nil || err.Error() != expErr {
|
||||
if err := nm.Remove(&FullPath{PathSlice: []string{"Field1", "nan", ""}, Path: "Field1[nan]"}); err == nil || err.Error() != expErr {
|
||||
t.Errorf("Expected error: %s,received: %v", expErr, err)
|
||||
}
|
||||
}
|
||||
@@ -839,23 +840,23 @@ func TestOrderedNavigableMapRemove2(t *testing.T) {
|
||||
func TestOrderedNavigableMapOrderedFields(t *testing.T) {
|
||||
nm := NewOrderedNavigableMap()
|
||||
nm.Set(&FullPath{
|
||||
PathItems: []string{"Field1", "Field2", "0"},
|
||||
PathSlice: []string{"Field1", "Field2", "0"},
|
||||
Path: "Field1.Field2[0]",
|
||||
}, NewLeafNode("1003"))
|
||||
nm.Set(&FullPath{
|
||||
PathItems: []string{"Field1", "Field3", "0"},
|
||||
PathSlice: []string{"Field1", "Field3", "0"},
|
||||
Path: "Field1.Field3[0]",
|
||||
}, NewLeafNode("1004"))
|
||||
nm.Set(&FullPath{
|
||||
PathItems: []string{"Field5"},
|
||||
PathSlice: []string{"Field5"},
|
||||
Path: "Field5",
|
||||
}, NewLeafNode("1005"))
|
||||
nm.Set(&FullPath{
|
||||
PathItems: []string{"Field6"},
|
||||
PathSlice: []string{"Field6"},
|
||||
Path: "Field6",
|
||||
}, NewLeafNode("1006"))
|
||||
nm.Remove(&FullPath{
|
||||
PathItems: []string{"Field5"},
|
||||
PathSlice: []string{"Field5"},
|
||||
Path: "Field5",
|
||||
})
|
||||
exp := []interface{}{"1003", "1004", "1006"}
|
||||
|
||||
Reference in New Issue
Block a user