From 24bb758e9937a287d32b6d9eaee39176c999f52d Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Thu, 17 Aug 2017 11:47:53 +0300 Subject: [PATCH] Pass opts to `expand` and `expand-middleware` --- README.md | 2 +- src/reitit/core.cljc | 19 ++++++++++--------- src/reitit/middleware.cljc | 16 ++++++++-------- src/reitit/ring.cljc | 4 ++-- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 0519a6e5..c73eba40 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/reitit/core.cljc b/src/reitit/core.cljc index 2be7d618..cdd36186 100644 --- a/src/reitit/core.cljc +++ b/src/reitit/core.cljc @@ -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] diff --git a/src/reitit/middleware.cljc b/src/reitit/middleware.cljc index 378c9974..0dd97da7 100644 --- a/src/reitit/middleware.cljc +++ b/src/reitit/middleware.cljc @@ -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] diff --git a/src/reitit/ring.cljc b/src/reitit/ring.cljc index 354d2f59..98b52e9a 100644 --- a/src/reitit/ring.cljc +++ b/src/reitit/ring.cljc @@ -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]