Skip to content

Commit aef1477

Browse files
committed
PR suggestions
1 parent c006ec6 commit aef1477

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

utils/url/url.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ import (
99

1010
const defaultPathSeparator = "/"
1111

12-
// IsParamSegment checks whether the segment string is a path parameter
12+
// The expected function signature for checking whether two path segments match.
13+
type PathSegmentMatcherFunc = func(segmentA, segmentB string) bool
14+
15+
// IsParamSegment checks whether the segment string is a path parameter as described by the OpenAPI spec (see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#path-templating).
1316
func IsParamSegment(segment string) bool {
14-
return len(segment) >= 2 && segment[0] == '{' && segment[len(segment)-1] == '}'
17+
return len(segment) >= 2 && strings.HasPrefix(segment, "{") && strings.HasSuffix(segment, "}")
1518
}
1619

1720
// HasMatchingPathSegments checks whether two path strings match based on their segments.
1821
func HasMatchingPathSegments(pathA, pathB string) (bool, error) {
19-
return hasMatchingPathSegments(pathA, pathB, func(segmentA, segmentB string) bool {
22+
return MatchingPathSegments(pathA, pathB, func(segmentA, segmentB string) bool {
2023
return segmentA == segmentB
2124
})
2225
}
@@ -27,19 +30,20 @@ func HasMatchingPathSegments(pathA, pathB string) (bool, error) {
2730
// HasMatchingPathSegmentsWithParams("/some/abc/path", "/some/{param}/path") // true
2831
// HasMatchingPathSegmentsWithParams("/some/abc/path", "/some/def/path") // false
2932
func HasMatchingPathSegmentsWithParams(pathA, pathB string) (bool, error) {
30-
return hasMatchingPathSegments(pathA, pathB, func(pathASeg, pathBSeg string) bool {
33+
return MatchingPathSegments(pathA, pathB, func(pathASeg, pathBSeg string) bool {
3134
switch {
3235
case IsParamSegment(pathASeg):
33-
return pathBSeg != ""
36+
return !reflection.IsEmpty(pathBSeg)
3437
case IsParamSegment(pathBSeg):
35-
return pathASeg != ""
38+
return !reflection.IsEmpty(pathASeg)
3639
default:
3740
return pathASeg == pathBSeg
3841
}
3942
})
4043
}
4144

42-
func hasMatchingPathSegments(pathA, pathB string, segmentMatcherFn func(segmentA, segmentB string) bool) (match bool, err error) {
45+
// MatchingPathSegments checks whether two path strings match based on their segments using the provided matcher function.
46+
func MatchingPathSegments(pathA, pathB string, matcherFn PathSegmentMatcherFunc) (match bool, err error) {
4347
if reflection.IsEmpty(pathA) {
4448
err = commonerrors.UndefinedVariable("path A")
4549
return
@@ -50,7 +54,7 @@ func hasMatchingPathSegments(pathA, pathB string, segmentMatcherFn func(segmentA
5054
return
5155
}
5256

53-
if segmentMatcherFn == nil {
57+
if matcherFn == nil {
5458
err = commonerrors.UndefinedVariable("segment matcher function")
5559
return
5660
}
@@ -62,7 +66,7 @@ func hasMatchingPathSegments(pathA, pathB string, segmentMatcherFn func(segmentA
6266
}
6367

6468
for i := range pathBSegments {
65-
if !segmentMatcherFn(pathASegments[i], pathBSegments[i]) {
69+
if !matcherFn(pathASegments[i], pathBSegments[i]) {
6670
return
6771
}
6872
}

0 commit comments

Comments
 (0)