Default compile get the :handler from meta

This commit is contained in:
Tommi Reiman 2017-08-13 14:40:46 +03:00
parent 5f1d6a61a6
commit 0a58510dc2
2 changed files with 52 additions and 39 deletions

View file

@ -55,11 +55,11 @@
(meta-merge acc {k v})) (meta-merge acc {k v}))
{} x)) {} x))
(defn resolve-routes [data {:keys [coerce] :or {coerce identity} :as opts}] (defn resolve-routes [data {:keys [coerce] :as opts}]
(->> (walk data opts) (cond-> (->> (walk data opts)
(map-meta merge-meta) (map-meta merge-meta))
(mapv (partial coerce)) coerce (->> (mapv (partial coerce))
(filterv identity))) (filterv identity))))
(defn compile-route [compile [p m :as route]] (defn compile-route [compile [p m :as route]]
[p m (if compile (compile route))]) [p m (if compile (compile route))])
@ -71,6 +71,10 @@
(defrecord Match [template meta path handler params]) (defrecord Match [template meta path handler params])
(def default-router-options
{:coerce identity
:compile (comp :handler second)})
(defrecord LinearRouter [routes data lookup] (defrecord LinearRouter [routes data lookup]
Routing Routing
(routes [_] (routes [_]
@ -91,8 +95,9 @@
See [[router]] for available options" See [[router]] for available options"
([routes] ([routes]
(linear-router routes {})) (linear-router routes {}))
([routes {:keys [compile]}] ([routes opts]
(let [compiled (map (partial compile-route compile) routes)] (let [{:keys [compile]} (meta-merge default-router-options opts)
compiled (map (partial compile-route compile) routes)]
(->LinearRouter (->LinearRouter
routes routes
(mapv (partial impl/create) compiled) (mapv (partial impl/create) compiled)
@ -119,8 +124,9 @@
See [[router]] for available options" See [[router]] for available options"
([routes] ([routes]
(lookup-router routes {})) (lookup-router routes {}))
([routes {:keys [compile]}] ([routes opts]
(let [compiled (map (partial compile-route compile) routes)] (let [{:keys [compile]} (meta-merge default-router-options opts)
compiled (map (partial compile-route compile) routes)]
(when-let [route (some impl/contains-wilds? (map first routes))] (when-let [route (some impl/contains-wilds? (map first routes))]
(throw (throw
(ex-info (ex-info
@ -150,7 +156,7 @@
| `:meta` | Initial expanded route-meta vector (default `[]`) | `:meta` | Initial expanded route-meta vector (default `[]`)
| `:expand` | Function `arg => meta` to expand route arg to route meta-data (default `reitit.core/expand`) | `:expand` | Function `arg => meta` to expand route arg to route meta-data (default `reitit.core/expand`)
| `:coerce` | Function `[path meta] => [path meta]` to coerce resolved route, can throw or return `nil` (default `identity`) | `:coerce` | Function `[path meta] => [path meta]` to coerce resolved route, can throw or return `nil` (default `identity`)
| `:compile` | Function `[path meta] => handler` to compile a route handler" | `:compile` | Function `[path meta] => handler` to compile a route handler (default `(comp :handler second)`)"
([data] ([data]
(router data {})) (router data {}))
([data opts] ([data opts]

View file

@ -56,36 +56,43 @@
["/api/:version/ping"] {}))))))) ["/api/:version/ping"] {})))))))
(testing "route coercion & compilation" (testing "route coercion & compilation"
(let [compile-times (atom 0) (testing "custom compile"
coerce (fn [[path meta]] (let [compile-times (atom 0)
(if-not (:invalid? meta) coerce (fn [[path meta]]
[path (assoc meta :path path)])) (if-not (:invalid? meta)
compile (fn [[path meta]] [path (assoc meta :path path)]))
(swap! compile-times inc) compile (fn [[path meta]]
(constantly path)) (swap! compile-times inc)
router (reitit/router (constantly path))
["/api" {:roles #{:admin}} router (reitit/router
["/ping" ::ping] ["/api" {:roles #{:admin}}
["/pong" ::pong] ["/ping" ::ping]
["/hidden" {:invalid? true} ["/pong" ::pong]
["/utter"] ["/hidden" {:invalid? true}
["/crap"]]] ["/utter"]
{:coerce coerce ["/crap"]]]
:compile compile})] {:coerce coerce
(testing "routes are coerced" :compile compile})]
(is (= [["/api/ping" {:name ::ping (testing "routes are coerced"
:path "/api/ping", (is (= [["/api/ping" {:name ::ping
:roles #{:admin}}] :path "/api/ping",
["/api/pong" {:name ::pong :roles #{:admin}}]
:path "/api/pong", ["/api/pong" {:name ::pong
:roles #{:admin}}]] :path "/api/pong",
(reitit/routes router)))) :roles #{:admin}}]]
(testing "route match contains compiled handler" (reitit/routes router))))
(is (= 2 @compile-times)) (testing "route match contains compiled handler"
(let [{:keys [handler]} (reitit/match-by-path router "/api/pong")] (is (= 2 @compile-times))
(let [{:keys [handler]} (reitit/match-by-path router "/api/pong")]
(is handler)
(is (= "/api/pong" (handler)))
(is (= 2 @compile-times))))))
(testing "default compile"
(let [router (reitit/router ["/ping" (constantly "ok")])]
(println (reitit/match-by-path router "/ping"))
(let [{:keys [handler]} (reitit/match-by-path router "/ping")]
(is handler) (is handler)
(is (= "/api/pong" (handler))) (is (= "ok" (handler)))))))
(is (= 2 @compile-times))))))
(testing "bide sample" (testing "bide sample"
(let [routes [["/auth/login" :auth/login] (let [routes [["/auth/login" :auth/login]