Pass opts to expand and expand-middleware

This commit is contained in:
Tommi Reiman 2017-08-17 11:47:53 +03:00
parent f39ef9ecad
commit 24bb758e99
4 changed files with 21 additions and 20 deletions

View file

@ -361,7 +361,7 @@ Routers can be configured via options. Options allow things like [`clojure.spec`
| `:path` | Base-path for routes (default `""`)
| `:routes` | Initial resolved routes (default `[]`)
| `:meta` | Initial expanded route-meta vector (default `[]`)
| `:expand` | Function of `arg => meta` to expand route arg to route meta-data (default `reitit.core/expand`)
| `:expand` | Function of `arg opts => meta` to expand route arg to route meta-data (default `reitit.core/expand`)
| `:coerce` | Function of `[path meta] opts => [path meta]` to coerce resolved route, can throw or return `nil`
| `:compile` | Function of `[path meta] opts => handler` to compile a route handler

View file

@ -5,31 +5,32 @@
(:import (reitit.impl Route))))
(defprotocol Expand
(expand [this]))
(expand [this opts]))
(extend-protocol Expand
#?(:clj clojure.lang.Keyword
:cljs cljs.core.Keyword)
(expand [this] {:name this})
(expand [this _] {:name this})
#?(:clj clojure.lang.PersistentArrayMap
:cljs cljs.core.PersistentArrayMap)
(expand [this] this)
(expand [this _] this)
#?(:clj clojure.lang.PersistentHashMap
:cljs cljs.core.PersistentHashMap)
(expand [this] this)
(expand [this _] this)
#?(:clj clojure.lang.Fn
:cljs function)
(expand [this] {:handler this})
(expand [this _] {:handler this})
nil
(expand [_]))
(expand [_ _]))
(defn walk [data {:keys [path meta routes expand]
:or {path "", meta [], routes [], expand expand}}]
:or {path "", meta [], routes [], expand expand}
:as opts}]
(letfn
[(walk-many [p m r]
(reduce #(into %1 (walk-one p m %2)) [] r))
@ -40,7 +41,7 @@
[meta childs] (if (vector? maybe-meta)
[{} args]
[maybe-meta (rest args)])
macc (into macc (expand meta))]
macc (into macc (expand meta opts))]
(if (seq childs)
(walk-many (str pacc path) macc childs)
[[(str pacc path) macc]]))))]
@ -151,7 +152,7 @@
| `:path` | Base-path for routes (default `\"\"`)
| `:routes` | Initial resolved routes (default `[]`)
| `:meta` | Initial expanded route-meta vector (default `[]`)
| `:expand` | Function of `arg => meta` to expand route arg to route meta-data (default `reitit.core/expand`)
| `:expand` | Function of `arg opts => meta` to expand route arg to route meta-data (default `reitit.core/expand`)
| `:coerce` | Function of `[path meta] opts => [path meta]` to coerce resolved route, can throw or return `nil`
| `:compile` | Function of `[path meta] opts => handler` to compile a route handler"
([data]

View file

@ -3,22 +3,22 @@
[reitit.core :as reitit]))
(defprotocol ExpandMiddleware
(expand-middleware [this]))
(expand-middleware [this opts]))
(extend-protocol ExpandMiddleware
#?(:clj clojure.lang.APersistentVector
:cljs cljs.core.PersistentVector)
(expand-middleware [[f & args]]
(expand-middleware [[f & args] _]
(fn [handler]
(apply f handler args)))
#?(:clj clojure.lang.Fn
:cljs function)
(expand-middleware [this] this)
(expand-middleware [this _] this)
nil
(expand-middleware [_]))
(expand-middleware [_ _]))
(defn- ensure-handler! [path meta scope]
(when-not (:handler meta)
@ -28,18 +28,18 @@
(merge {:path path, :meta meta}
(if scope {:scope scope}))))))
(defn compose-middleware [middleware]
(defn compose-middleware [middleware opts]
(->> middleware
(keep identity)
(map expand-middleware)
(map #(expand-middleware % opts))
(apply comp identity)))
(defn compile-handler
([route opts]
(compile-handler route opts nil))
([[path {:keys [middleware handler] :as meta}] _ scope]
([[path {:keys [middleware handler] :as meta}] opts scope]
(ensure-handler! path meta scope)
((compose-middleware middleware) handler)))
((compose-middleware middleware opts) handler)))
(defn router
([data]

View file

@ -30,11 +30,11 @@
(defn get-match [request]
(::match request))
(defn coerce-handler [[path meta] {:keys [expand]}]
(defn coerce-handler [[path meta] {:keys [expand] :as opts}]
[path (reduce
(fn [acc method]
(if (contains? acc method)
(update acc method expand)
(update acc method expand opts)
acc)) meta http-methods)])
(defn compile-handler [[path meta] opts]