Skip to content
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

feat: add URLParam function to router #259

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

eduardoboucas
Copy link
Member

I wanted to define a route like /route/{param} and extract the value of param in the handler. I didn't find a built-in way of extracting the value of URL parameters with the router, so this PR adds a function that wraps the URLParam function from chi.

Example usage:

r.Get("/jobs/{jobId}", func(w http.ResponseWriter, req *http.Request) error {
  jobId := r.URLParam(req, "jobId")
  
  doSomething(jobId)
  
  w.WriteHeader(http.StatusOK)

  return nil
})

Apologies if it's already possible to do this with the router and I completely missed it, in which case feel free to close the PR.

@eduardoboucas eduardoboucas added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Oct 31, 2021
@eduardoboucas eduardoboucas requested a review from a team as a code owner October 31, 2021 15:30
Copy link
Contributor

@mheffner mheffner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool 👍

Comment on lines +158 to +160
func (r *chiWrapper) URLParam(req *http.Request, name string) string {
return chi.URLParam(req, name)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't get why this needs to be a method on the chiWrapper?
why not make this a freestanding function?

Copy link
Member Author

@eduardoboucas eduardoboucas Nov 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't have to be, as it doesn't access the chi instance, but from a functional perspective it felt like the appropriate place. I don't have a strong preference, though. What location do you suggest? router/helpers.go?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like it could just be some code like this to reexport it?

const URLParam = chi.URLParam

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally prefer having a method that we explicitly implement and which we can customise the behaviour of in the future, if needed.

Is this a blocker for you?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can replace a reexport with a freestanding function at any point, it will not break anyone except if the signature differs which is the same if it is a function now.

Copy link
Contributor

@mraerino mraerino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've often just used chi.URLParam directly

@eduardoboucas
Copy link
Member Author

i've often just used chi.URLParam directly

But that means using chi as a direct dependency and interacting both with the abstraction and with the underlying package, which doesn't feel ideal?

@mheffner
Copy link
Contributor

Good to merge?

@eduardoboucas
Copy link
Member Author

Good to merge?

Ouch, sorry! This one fell through the cracks.

Unless #259 (comment) is a blocker, this is good to go. I'll wait for that to be clarified and then merge.

@github-actions github-actions bot added the stale label Mar 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants