Skip to content

Add route information to request. #140

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 1 commit into from
Jul 5, 2015

Conversation

cbui
Copy link
Contributor

@cbui cbui commented Jun 2, 2015

Feature for #82 and subsumes #93

@@ -102,6 +102,12 @@
((mw handler) request)
(handler request))))

(defn- add-route-information
Copy link
Owner

Choose a reason for hiding this comment

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

assoc-route-info would be a name more in keeping with the other functions in the namespace.

@cbui
Copy link
Contributor Author

cbui commented Jun 3, 2015

Couple of questions:

Is the reason you say to precompute the route info to decouple the add-route-information function from needing to know about the stuff? Secondly, after doing that and renaming the add-route-information to assoc-route-info, it doesn't seem like there's much added to having that private fn so I removed it. Is that cool?

Thanks for your feedback!

@weavejester
Copy link
Owner

Precomputation just avoids creating a new vector each time. So currently you've written:

 (defn make-route [method path handler]
   (if-method method
     (if-route path
       (wrap-route-middleware
         (fn [request]
           (let [route-info [(or method :any) (str path)]]
             (response/render (handler (assoc request :route route-info))
                              request)))))))

But we can take the route-info binding out to the top level instead:

 (defn make-route [method path handler]
   (let [route-info [(or method :any) (str path)]]
     (if-method method
       (if-route path
         (wrap-route-middleware
           (fn [request]
             (let [request (assoc request :route route-info)]
               (response/render (handler request) request))))))))

Now the route-info vector is only calculated once, when the route is first constructed. Even though building the route-info isn't likely to take long, if we can save a little time and memory just by moving the let binding around, we should do it.

@weavejester
Copy link
Owner

Removing the private function was the right call, I think. Could you also add a test to check that ANY routes work okay as well? They're a special case, so we should ensure they work.

@cbui
Copy link
Contributor Author

cbui commented Jul 1, 2015

Ping @weavejester

@weavejester
Copy link
Owner

Thanks for the ping. This should be okay, but I think that :compojure/route might be a better key, as :route is a little generic. Make that change and I'll merge :)

@cbui
Copy link
Contributor Author

cbui commented Jul 5, 2015

@weavejester good?

@weavejester
Copy link
Owner

That looks fine. Now you just need to squash your commits.

@cbui cbui force-pushed the route-information branch from 7aeb8ea to d443944 Compare July 5, 2015 18:51
@cbui
Copy link
Contributor Author

cbui commented Jul 5, 2015

Squashed.

@weavejester
Copy link
Owner

Thanks! Can you remove the ending "." from the commit summary? Then I'll merge.

@cbui cbui force-pushed the route-information branch from d443944 to fdef421 Compare July 5, 2015 20:37
@cbui
Copy link
Contributor Author

cbui commented Jul 5, 2015

Done!

weavejester added a commit that referenced this pull request Jul 5, 2015
@weavejester weavejester merged commit fad10ea into weavejester:master Jul 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants