mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Handle empty relative path case in HP.AsString()
Return '.' when HierarchyPath is empty and prefix is false.
This commit is contained in:
committed by
Dan Christian Bogos
parent
5a5c141806
commit
66893d3d76
@@ -647,18 +647,20 @@ func (hP HierarchyPath) AsString(sep string, prefix bool) string {
|
||||
var strHP strings.Builder
|
||||
|
||||
// If prefix is true and the HierarchyPath slice is empty, sep will be returned.
|
||||
// This will indicate the start of the absolute path.
|
||||
if prefix {
|
||||
strHP.WriteString(sep)
|
||||
}
|
||||
|
||||
// TODO: Since this function can convert both the full and the relative HierarchyPath to a string, with
|
||||
// prefix telling us which is which (true -> full path, false -> relative path), we should consider
|
||||
// returning the '.' character when prefix == false. Currently, because we are returning an empty string
|
||||
// if the path we are currently parsing is equal to the root path, when we attempt to retrieve the element
|
||||
// we receive the error "expr expression is nil".
|
||||
if len(hP) == 0 {
|
||||
// If prefix is false and HierarchyPath is empty, return '.' to represent the current directory in a relative path.
|
||||
// This convention avoids errors (e.g., "expr expression is nil") when retrieving elements from an empty path.
|
||||
if !prefix {
|
||||
return "."
|
||||
}
|
||||
return strHP.String()
|
||||
}
|
||||
|
||||
for i, elem := range hP {
|
||||
if i != 0 {
|
||||
strHP.WriteString(sep)
|
||||
|
||||
@@ -905,7 +905,7 @@ func TestHierarchyPathAsString(t *testing.T) {
|
||||
hP: HierarchyPath{},
|
||||
sep: "/",
|
||||
prefix: false,
|
||||
expected: "",
|
||||
expected: ".",
|
||||
},
|
||||
{
|
||||
name: "Empty HierarchyPath with prefix",
|
||||
@@ -961,7 +961,7 @@ func TestHierarchyPathAsString(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if received := tt.hP.AsString(tt.sep, tt.prefix); received != tt.expected {
|
||||
t.Errorf("expected %s, received %s", tt.expected, received)
|
||||
t.Errorf("expected <%s>, received <%s>", tt.expected, received)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user