-
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 5 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
Some comments aren't visible on the classic Files Changed page.
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,136 @@ | ||
| package url | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/ARM-software/golang-utils/utils/commonerrors" | ||
| "github.com/ARM-software/golang-utils/utils/reflection" | ||
| ) | ||
|
|
||
| const defaultPathSeparator = "/" | ||
|
|
||
| // IsParamSegment checks whether the segment string is a path parameter | ||
| func IsParamSegment(segment string) bool { | ||
| return len(segment) >= 2 && segment[0] == '{' && segment[len(segment)-1] == '}' | ||
Kem-Gov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // HasMatchingPathSegments checks whether two path strings match based on their segments. | ||
| func HasMatchingPathSegments(pathA, pathB string) (bool, error) { | ||
| return hasMatchingPathSegments(pathA, pathB, func(segmentA, segmentB string) bool { | ||
Kem-Gov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return segmentA == segmentB | ||
| }) | ||
| } | ||
|
|
||
| // 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) (bool, error) { | ||
| return hasMatchingPathSegments(pathA, pathB, func(pathASeg, pathBSeg string) bool { | ||
| switch { | ||
| case IsParamSegment(pathASeg): | ||
| return pathBSeg != "" | ||
Kem-Gov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| case IsParamSegment(pathBSeg): | ||
| return pathASeg != "" | ||
| default: | ||
| return pathASeg == pathBSeg | ||
Kem-Gov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }) | ||
| } | ||
|
|
||
| func hasMatchingPathSegments(pathA, pathB string, segmentMatcherFn func(segmentA, segmentB string) bool) (match bool, err error) { | ||
Kem-Gov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if reflection.IsEmpty(pathA) { | ||
| err = commonerrors.UndefinedVariable("path A") | ||
| return | ||
| } | ||
|
|
||
| if reflection.IsEmpty(pathB) { | ||
| err = commonerrors.UndefinedVariable("path B") | ||
| return | ||
| } | ||
|
|
||
| if segmentMatcherFn == nil { | ||
| err = commonerrors.UndefinedVariable("segment matcher function") | ||
| return | ||
| } | ||
|
|
||
| pathASegments := SplitPath(pathA) | ||
| pathBSegments := SplitPath(pathB) | ||
| if len(pathASegments) != len(pathBSegments) { | ||
| return | ||
| } | ||
Kem-Gov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| for i := range pathBSegments { | ||
| if !segmentMatcherFn(pathASegments[i], pathBSegments[i]) { | ||
| return | ||
| } | ||
| } | ||
|
|
||
| match = true | ||
| return | ||
| } | ||
|
|
||
| // SplitPath returns a slice containing the individual segments that make up the path string. It looks for the default "/" path separator when splitting. | ||
| func SplitPath(path string) []string { | ||
| return SplitPathWithSeparator(path, defaultPathSeparator) | ||
| } | ||
|
|
||
| // SplitPathWithSeparator is similar to SplitPath but allows for specifying the path separator to look for when splitting. | ||
| func SplitPathWithSeparator(path string, separator string) []string { | ||
| path = strings.TrimSpace(path) | ||
| if reflection.IsEmpty(path) || path == separator { | ||
| return nil | ||
| } | ||
|
|
||
| path = strings.Trim(path, separator) | ||
| segments := strings.Split(path, separator) | ||
| out := segments[:0] | ||
| for _, p := range segments { | ||
| if !reflection.IsEmpty(p) { | ||
| out = append(out, p) | ||
| } | ||
| } | ||
| return out | ||
Kem-Gov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // JoinPaths returns a single concatenated path string from the supplied paths and correctly sets the default "/" separator between them. | ||
| func JoinPaths(paths ...string) (joinedPath string, err error) { | ||
| return JoinPathsWithSeparator(defaultPathSeparator, paths...) | ||
| } | ||
|
|
||
| // JoinPathsWithSeparator is similar to JoinPaths but allows for specifying the path separator to use. | ||
| func JoinPathsWithSeparator(separator string, paths ...string) (joinedPath string, err error) { | ||
| if paths == nil { | ||
| err = commonerrors.UndefinedVariable("paths") | ||
| return | ||
| } | ||
| if len(paths) == 0 { | ||
| return | ||
| } | ||
| if len(paths) == 1 { | ||
| joinedPath = paths[0] | ||
| return | ||
| } | ||
|
|
||
| if reflection.IsEmpty(separator) { | ||
| separator = defaultPathSeparator | ||
| } | ||
|
|
||
| joinedPath = paths[0] | ||
| for _, p := range paths[1:] { | ||
| pathAHasSlashSuffix := strings.HasSuffix(joinedPath, separator) | ||
| pathBHasSlashPrefix := strings.HasPrefix(p, separator) | ||
|
|
||
| switch { | ||
| case pathAHasSlashSuffix && pathBHasSlashPrefix: | ||
| joinedPath += p[1:] | ||
| case !pathAHasSlashSuffix && !pathBHasSlashPrefix: | ||
| joinedPath += separator + p | ||
| default: | ||
| joinedPath += p | ||
| } | ||
| } | ||
Kem-Gov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return | ||
| } | ||
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.