-
Notifications
You must be signed in to change notification settings - Fork 7
✨ [url] Add a new package containing URL helper functions
#752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2693641
Add url package with helpers for url operations
Kem-Gov ca7704b
Refactor url functions and add tests
Kem-Gov 3ddbc3d
Refactor matching path segment functions and add tests
Kem-Gov ac9800f
Add news file
Kem-Gov c006ec6
Fix linting
Kem-Gov aef1477
PR suggestions
Kem-Gov d88f2c5
Refactor url helper functions
Kem-Gov eba5bdf
Use collection.ParseListWithCleanup in SplitPath function
Kem-Gov d564f6f
Update with PR suggestions
Kem-Gov 7597d76
Add ozzo string rule for checking if a value is a valid path parameter
Kem-Gov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| :sparkles: `[url]` Add a new package containing url helper functions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| package url | ||
|
|
||
| import ( | ||
| netUrl "net/url" | ||
| "path" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "github.com/ARM-software/golang-utils/utils/collection" | ||
| "github.com/ARM-software/golang-utils/utils/commonerrors" | ||
| "github.com/ARM-software/golang-utils/utils/reflection" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultPathSeparator = "/" | ||
| minimumPathParameterLength = 3 | ||
| ) | ||
|
|
||
| // Section 3.3 of RFC3986 details valid characters for path segments (see https://datatracker.ietf.org/doc/html/rfc3986#section-3.3) | ||
| var validPathRegex = regexp.MustCompile(`^(?:[A-Za-z0-9._~\-!$&'()*+,;=:@{}]|%[0-9A-Fa-f]{2})+$`) | ||
|
|
||
| // PathSegmentMatcherFunc defines the signature for path segment matcher functions. | ||
| type PathSegmentMatcherFunc = func(segmentA, segmentB string) (match bool, err error) | ||
|
|
||
| // ValidatePathParameter checks whether a path parameter is valid. An error is returned if it is invalid. | ||
| // Version 3.1.0 of the OpenAPI spec provides some guidance for path parameter values (see https://spec.openapis.org/oas/v3.1.0.html#path-templating) | ||
| func ValidatePathParameter(parameter string) error { | ||
| if !MatchesPathParameterSyntax(parameter) { | ||
| return commonerrors.Newf(commonerrors.ErrInvalid, "parameter %q must not be empty, cannot contain only whitespaces, have a length greater than or equal to three, start with an opening brace, and end with a closing brace", parameter) | ||
| } | ||
|
|
||
| unescapedSegment, err := netUrl.PathUnescape(parameter) | ||
| if err != nil { | ||
| return commonerrors.WrapErrorf(commonerrors.ErrInvalid, err, "an error occurred during path unescaping for parameter %q", parameter) | ||
| } | ||
|
|
||
| if !validPathRegex.MatchString(unescapedSegment) { | ||
| return commonerrors.Newf(commonerrors.ErrInvalid, "parameter %q unescaped to %q can only contain alphanumeric characters, dashes, underscores, and a single pair of braces", parameter, unescapedSegment) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // MatchesPathParameterSyntax checks whether the parameter string matches the syntax for a path parameter as described by the OpenAPI spec (see https://spec.openapis.org/oas/v3.0.0.html#path-templating). | ||
| func MatchesPathParameterSyntax(parameter string) bool { | ||
| if reflection.IsEmpty(parameter) { | ||
| return false | ||
| } | ||
|
|
||
| if len(parameter) < minimumPathParameterLength { | ||
| return false | ||
| } | ||
|
|
||
| if !strings.HasPrefix(parameter, "{") || !strings.HasSuffix(parameter, "}") { | ||
| return false | ||
| } | ||
|
|
||
| return strings.Count(parameter, "{") == 1 && strings.Count(parameter, "}") == 1 | ||
| } | ||
|
|
||
| // HasMatchingPathSegments checks whether two path strings match based on their segments by doing a simple equality check on each path segment pair. | ||
| func HasMatchingPathSegments(pathA, pathB string) (match bool, err error) { | ||
| return MatchingPathSegments(pathA, pathB, BasicEqualityPathSegmentMatcher) | ||
| } | ||
|
|
||
| // HasMatchingPathSegmentsWithParams is similar to HasMatchingPathSegments but also considers segments as matching if at least one of them contains a path parameter. | ||
| // | ||
| // HasMatchingPathSegmentsWithParams("/some/{param}/path", "/some/{param}/path") // true | ||
| // HasMatchingPathSegmentsWithParams("/some/abc/path", "/some/{param}/path") // true | ||
| // HasMatchingPathSegmentsWithParams("/some/abc/path", "/some/def/path") // false | ||
| func HasMatchingPathSegmentsWithParams(pathA, pathB string) (match bool, err error) { | ||
| return MatchingPathSegments(pathA, pathB, BasicEqualityPathSegmentWithParamMatcher) | ||
| } | ||
|
|
||
| // BasicEqualityPathSegmentMatcher is a PathSegmentMatcherFunc that performs direct string comparison of two path segments. | ||
| func BasicEqualityPathSegmentMatcher(segmentA, segmentB string) (match bool, err error) { | ||
| match = segmentA == segmentB | ||
| return | ||
| } | ||
|
|
||
| // BasicEqualityPathSegmentWithParamMatcher is a PathSegmentMatcherFunc that is similar to BasicEqualityPathSegmentMatcher but accounts for path parameter segments. | ||
| func BasicEqualityPathSegmentWithParamMatcher(segmentA, segmentB string) (match bool, err error) { | ||
| if MatchesPathParameterSyntax(segmentA) { | ||
| if errValidatePathASeg := ValidatePathParameter(segmentA); errValidatePathASeg != nil { | ||
| err = commonerrors.WrapErrorf(commonerrors.ErrInvalid, errValidatePathASeg, "an error occurred while validating path parameter %q", segmentA) | ||
| return | ||
| } | ||
|
|
||
| match = !reflection.IsEmpty(segmentB) | ||
| return | ||
| } | ||
|
|
||
| if MatchesPathParameterSyntax(segmentB) { | ||
| if errValidatePathBSeg := ValidatePathParameter(segmentB); errValidatePathBSeg != nil { | ||
| err = commonerrors.WrapErrorf(commonerrors.ErrInvalid, errValidatePathBSeg, "an error occurred while validating path parameter %q", segmentB) | ||
| return | ||
| } | ||
|
|
||
| match = !reflection.IsEmpty(segmentA) | ||
| return | ||
| } | ||
|
|
||
| return BasicEqualityPathSegmentMatcher(segmentA, segmentB) | ||
| } | ||
|
|
||
| // MatchingPathSegments checks whether two path strings match based on their segments using the provided matcher function. | ||
| func MatchingPathSegments(pathA, pathB string, matcherFn PathSegmentMatcherFunc) (match bool, err error) { | ||
| if reflection.IsEmpty(pathA) { | ||
| err = commonerrors.UndefinedVariable("path A") | ||
| return | ||
| } | ||
|
|
||
| if reflection.IsEmpty(pathB) { | ||
| err = commonerrors.UndefinedVariable("path B") | ||
| return | ||
| } | ||
|
|
||
| if matcherFn == nil { | ||
| err = commonerrors.UndefinedVariable("segment matcher function") | ||
| return | ||
| } | ||
|
|
||
| unescapedPathA, errPathASeg := netUrl.PathUnescape(pathA) | ||
| if errPathASeg != nil { | ||
| err = commonerrors.WrapErrorf(commonerrors.ErrUnexpected, errPathASeg, "an error occurred while unescaping path %q", pathA) | ||
| return | ||
| } | ||
|
|
||
| unescapedPathB, errPathBSeg := netUrl.PathUnescape(pathB) | ||
| if errPathBSeg != nil { | ||
| err = commonerrors.WrapErrorf(commonerrors.ErrUnexpected, errPathBSeg, "an error occurred while unescaping path %q", pathB) | ||
| return | ||
| } | ||
|
|
||
| pathASegments := SplitPath(unescapedPathA) | ||
| pathBSegments := SplitPath(unescapedPathB) | ||
| if len(pathASegments) != len(pathBSegments) { | ||
| return | ||
| } | ||
Kem-Gov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| for i := range pathBSegments { | ||
| match, err = matcherFn(pathASegments[i], pathBSegments[i]) | ||
| if err != nil { | ||
| err = commonerrors.WrapErrorf(commonerrors.ErrUnexpected, err, "an error occurred during execution of the matcher function for path segments %q and %q", pathASegments[i], pathBSegments[i]) | ||
| return | ||
| } | ||
|
|
||
| if !match { | ||
| return | ||
| } | ||
| } | ||
|
|
||
| match = true | ||
| return | ||
| } | ||
|
|
||
| // SplitPath returns a slice containing the individual segments that make up the path string p. | ||
| // It looks for the default forward slash path separator when splitting. | ||
| func SplitPath(p string) []string { | ||
| if reflection.IsEmpty(p) { | ||
| return []string{} | ||
| } | ||
|
|
||
| p = path.Clean(p) | ||
| p = strings.Trim(p, defaultPathSeparator) | ||
| return collection.ParseListWithCleanup(p, defaultPathSeparator) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.