generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logic): add some functions to deal with urls
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,37 @@ | ||
package util | ||
|
||
import ( | ||
"net/url" | ||
) | ||
|
||
// UrlMatches is a function that returns a function that matches the given url against the given other item. | ||
// | ||
// The function matches the components of the given url against the components of the given other url. If the component | ||
// of the given other url is empty, it is considered to match the component of the given url. | ||
// For example: | ||
// - matchUrl("http://example.com/foo")("http://example.com/foo") -> true | ||
// - matchUrl("http://example.com/foo")("http://example.com/foo?bar=baz") -> false | ||
// - matchUrl("tel:123456789")("tel:") -> true | ||
// | ||
// The function is curried, and is a binary relation that is reflexive, associative (but not commutative). | ||
func UrlMatches(this *url.URL) func(*url.URL) bool { | ||
return func(that *url.URL) bool { | ||
return (that.Scheme == "" || that.Scheme == this.Scheme) && | ||
(that.Opaque == "" || that.Opaque == this.Opaque) && | ||
(that.User == nil || that.User.String() == "" || that.User.String() == this.User.String()) && | ||
(that.Host == "" || that.Host == this.Host) && | ||
(that.Path == "" || that.Path == this.Path) && | ||
(that.RawQuery == "" || that.RawQuery == this.RawQuery) && | ||
(that.Fragment == "" || that.Fragment == this.Fragment) | ||
} | ||
} | ||
|
||
// ParseUrlMust parses the given url and panics if it fails. | ||
// You have been warned. | ||
func ParseUrlMust(s string) *url.URL { | ||
u, err := url.Parse(s) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return u | ||
} |