From 08edbb941927233490f7e8052da903407e463bcf Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Fri, 11 Aug 2017 16:02:08 +0300 Subject: [PATCH] Route coercion, fixes #4 --- src/reitit/core.cljc | 7 +++++-- test/cljc/reitit/core_test.cljc | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/reitit/core.cljc b/src/reitit/core.cljc index 6e2a05f6..2021af29 100644 --- a/src/reitit/core.cljc +++ b/src/reitit/core.cljc @@ -55,8 +55,11 @@ (meta-merge acc {k v})) {} x)) -(defn resolve-routes [data opts] - (->> (walk data opts) (map-meta merge-meta))) +(defn resolve-routes [data {:keys [coerce] :or {coerce identity} :as opts}] + (->> (walk data opts) + (map-meta merge-meta) + (mapv (partial coerce)) + (filterv identity))) (defprotocol Routing (routes [this]) diff --git a/test/cljc/reitit/core_test.cljc b/test/cljc/reitit/core_test.cljc index 8399de67..e00cf429 100644 --- a/test/cljc/reitit/core_test.cljc +++ b/test/cljc/reitit/core_test.cljc @@ -55,6 +55,26 @@ (reitit/resolve-routes ["/api/:version/ping"] {}))))))) + (testing "route coercion" + (let [coerce (fn [[path meta]] + (if-not (:invalid? meta) + [path (assoc meta :path path)])) + router (reitit/router + ["/api" {:roles #{:admin}} + ["/ping" ::ping] + ["/pong" ::pong] + ["/hidden" {:invalid? true} + ["/utter"] + ["/crap"]]] + {:coerce coerce})] + (is (= [["/api/ping" {:name ::ping + :path "/api/ping", + :roles #{:admin}}] + ["/api/pong" {:name ::pong + :path "/api/pong", + :roles #{:admin}}]] + (reitit/routes router))))) + (testing "bide sample" (let [routes [["/auth/login" :auth/login] ["/auth/recovery/token/:token" :auth/recovery]