From 26308bcb924686ccf7b29fe79d22a2e0faf48ba8 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Tue, 3 Oct 2017 21:04:07 +0300 Subject: [PATCH] Add reitit.core/route-info --- src/reitit/core.cljc | 6 +++++- src/reitit/impl.cljc | 29 +++++++++++++---------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/reitit/core.cljc b/src/reitit/core.cljc index 9b39df0c..7efe387f 100644 --- a/src/reitit/core.cljc +++ b/src/reitit/core.cljc @@ -98,6 +98,9 @@ (defn- compile-routes [routes opts] (into [] (keep #(compile-route % opts) routes))) +(defn route-info [route] + (select-keys (impl/create route) [:path :parts :params :result :meta])) + (defprotocol Router (router-name [this]) (routes [this]) @@ -256,6 +259,7 @@ (mixed-router routes {})) ([routes opts] (let [{linear true, lookup false} (group-by impl/wild-route? routes) + compiled (compile-routes routes opts) ->static-router (if (= 1 (count lookup)) single-static-path-router lookup-router) wildcard-router (linear-router linear opts) static-router (->static-router lookup opts) @@ -264,7 +268,7 @@ (router-name [_] :mixed-router) (routes [_] - routes) + compiled) (options [_] opts) (route-names [_] diff --git a/src/reitit/impl.cljc b/src/reitit/impl.cljc index 091a1a15..75da4dcf 100644 --- a/src/reitit/impl.cljc +++ b/src/reitit/impl.cljc @@ -105,22 +105,19 @@ (defn create [[path meta result]] (let [path #?(:clj (.intern ^String path) :cljs path)] - (if (contains-wilds? path) - (as-> (parse-path path) $ - (assoc $ :path-re (path-regex $)) - (merge $ {:path path - :matcher (path-matcher $) - :result result - :meta meta}) - (dissoc $ :path-re :path-constraints) - (update $ :path-params set) - (set/rename-keys $ {:path-parts :parts - :path-params :params}) - (map->Route $)) - (map->Route {:path path - :meta meta - :matcher #(if (#?(:clj .equals, :cljs =) path %) {}) - :result result})))) + (as-> (parse-path path) $ + (assoc $ :path-re (path-regex $)) + (merge $ {:path path + :matcher (if (contains-wilds? path) + (path-matcher $) + #(if (#?(:clj .equals, :cljs =) path %) {})) + :result result + :meta meta}) + (dissoc $ :path-re :path-constraints) + (update $ :path-params set) + (set/rename-keys $ {:path-parts :parts + :path-params :params}) + (map->Route $)))) (defn segments [path] (let [ss (-> (str/split path #"/") rest vec)]