-
Notifications
You must be signed in to change notification settings - Fork 72
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
pat: support generating URLs from a Pattern #4
base: master
Are you sure you want to change the base?
Conversation
that would match the given pattern with the provided variables. You must | ||
provide a mapping for all named matches defined in the Pattern. | ||
*/ | ||
func (p *Pattern) URLPath(vars map[pattern.Variable]string) (string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm completely unattached to this method name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might just call it Path
(or URL
and return a url.URL
). Seems a bit simpler.
We could also try to make it round-trip with Match
and return a full-blown http.Request
but that seems like it's a bit much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One other thing: this takes map[pattern.Variable]string
, while much of the rest of the pattern
ecosystem produces and consumes map[pattern.Variable]interface{}
. I know it's kind of ugly (and if I end up regretting one thing from this redesign I feel like it'll probably be that type), but we should probably accept that type here as well (and type assertion our way to victory)
func (p *Pattern) URLPath(vars map[pattern.Variable]string) (string, error) { | ||
var missing []string | ||
|
||
path := patternRe.ReplaceAllStringFunc(p.raw, func(s string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd probably prefer looping through pats
and literals
here: it's a little bit ugly, but I suspect it'll be more efficient, and the error handling becomes a bit nicer since we don't need to deal with escaping the callback.
Could you also rebase this on |
Adds support for generating paths from a Pattern and a mapping from named matches to string values. This is useful in writing API clients as it allows for constructing URLs that exactly match the routing table with semantically meaningful arguments.
@zenazn: Responded to comments and rebased on master to pick up tests. The only comment I disagree with is changing the interface to accept a In this case, it doesn't make much semantic sense to consider the output of |
variables. You must provide a mapping for all named matches defined in | ||
the Pattern. | ||
*/ | ||
func (p *Pattern) URL(vars map[pattern.Variable]string) (url.URL, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you normally pass around *url.URL
The biggest reason to use the So to be fair, I don't know that this even a thing I want, but that's the thing at stake here. I agree with you that it makes little sense for this package in particular, but a stronger goal of mine is to build a good ecosystem of stable and flexible types. |
Thinking about it more, I think The counter point would be providing support for things like "converters" in Flask (concretely, supporting Realistically I don't think I'd implement the "converter" semantic this way in Go at all. Instead I'd compose |
(oh... and I'm a little braindead getting over the flu so please tell me if that last comment was rambling and incomprehensible) |
Adds support for generating paths from a Pattern and a mapping from named matches to string values. This is useful in writing API clients as it allows for constructing URLs that exactly match the routing table with semantically meaningful arguments.