Skip to content

Commit eba5bdf

Browse files
committed
Use collection.ParseListWithCleanup in SplitPath function
1 parent d88f2c5 commit eba5bdf

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

utils/url/url.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"regexp"
77
"strings"
88

9+
"github.com/ARM-software/golang-utils/utils/collection"
910
"github.com/ARM-software/golang-utils/utils/commonerrors"
1011
"github.com/ARM-software/golang-utils/utils/reflection"
1112
)
@@ -143,16 +144,11 @@ func MatchingPathSegments(pathA, pathB string, matcherFn PathSegmentMatcherFunc)
143144
// SplitPath returns a slice containing the individual segments that make up the path string p.
144145
// It looks for the default forward slash path separator when splitting.
145146
func SplitPath(p string) []string {
146-
p = strings.TrimSpace(p)
147-
if reflection.IsEmpty(p) || p == defaultPathSeparator {
148-
return nil
149-
}
150-
151-
p = path.Clean(p)
152-
p = strings.Trim(p, defaultPathSeparator)
153147
if reflection.IsEmpty(p) {
154148
return []string{}
155149
}
156150

157-
return strings.Split(p, defaultPathSeparator)
151+
p = path.Clean(p)
152+
p = strings.Trim(p, defaultPathSeparator)
153+
return collection.ParseListWithCleanup(p, defaultPathSeparator)
158154
}

utils/url/url_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,58 @@ func TestUrl_MatchingPathSegments(t *testing.T) {
412412
})
413413
}
414414
}
415+
416+
func TestUrl_SplitPath(t *testing.T) {
417+
tests := []struct {
418+
name string
419+
path string
420+
result []string
421+
}{
422+
{
423+
"empty path",
424+
"",
425+
[]string{},
426+
},
427+
{
428+
"root path",
429+
"/",
430+
[]string{"/"},
431+
},
432+
{
433+
"root path with repeated slashes",
434+
"///",
435+
[]string{"/"},
436+
},
437+
{
438+
"path with one segment",
439+
"abc",
440+
[]string{"abc"},
441+
},
442+
{
443+
"path with two segments",
444+
"abc/123",
445+
[]string{"abc", "123"},
446+
},
447+
{
448+
"path with multiple segments",
449+
"abc/123/def/456",
450+
[]string{"abc", "123", "def", "456"},
451+
},
452+
{
453+
"path with multiple segments including param segment",
454+
"abc/123/def/456/zzz/{param1}/999",
455+
[]string{"abc", "123", "def", "456", "zzz", "{param1}", "999"},
456+
},
457+
}
458+
459+
for i := range tests {
460+
test := tests[i]
461+
t.Run(test.name, func(t *testing.T) {
462+
segments := SplitPath(test.path)
463+
464+
for i, s := range segments {
465+
assert.Equal(t, test.result[i], s)
466+
}
467+
})
468+
}
469+
}

0 commit comments

Comments
 (0)