reitit/options, reitit/router-type, reified protocols

This commit is contained in:
Tommi Reiman 2017-08-20 21:00:20 +03:00
parent bbcf0c8350
commit 3e3f728c4e
3 changed files with 130 additions and 610 deletions

File diff suppressed because one or more lines are too long

View file

@ -70,7 +70,9 @@
[p m (if compile (compile route opts))]) [p m (if compile (compile route opts))])
(defprotocol Routing (defprotocol Routing
(router-type [this])
(routes [this]) (routes [this])
(options [this])
(route-names [this]) (route-names [this])
(match-by-path [this path]) (match-by-path [this path])
(match-by-name [this name] [this name params])) (match-by-name [this name] [this name params]))
@ -97,25 +99,6 @@
:coerce (fn [route _] route) :coerce (fn [route _] route)
:compile (fn [[_ {:keys [handler]}] _] handler)}) :compile (fn [[_ {:keys [handler]}] _] handler)})
(defrecord LinearRouter [routes names data lookup]
Routing
(routes [_]
routes)
(route-names [_]
names)
(match-by-path [_ path]
(reduce
(fn [acc ^Route route]
(if-let [params ((:matcher route) path)]
(reduced (->Match (:path route) (:meta route) (:handler route) params path))))
nil data))
(match-by-name [_ name]
(if-let [match (lookup name)]
(match nil)))
(match-by-name [_ name params]
(if-let [match (lookup name)]
(match params))))
(defn linear-router (defn linear-router
"Creates a [[LinearRouter]] from resolved routes and optional "Creates a [[LinearRouter]] from resolved routes and optional
expanded options. See [[router]] for available options" expanded options. See [[router]] for available options"
@ -132,26 +115,33 @@
(->PartialMatch p meta handler % params))] (->PartialMatch p meta handler % params))]
[(conj data route) [(conj data route)
(if name (assoc lookup name f) lookup)])) (if name (assoc lookup name f) lookup)]))
[[] {}] compiled)] [[] {}] compiled)
(->LinearRouter routes names data lookup)))) lookup (impl/fast-map lookup)]
(reify
(defrecord LookupRouter [routes names data lookup]
Routing Routing
(router-type [_]
:linear-router)
(routes [_] (routes [_]
routes) routes)
(options [_]
opts)
(route-names [_] (route-names [_]
names) names)
(match-by-path [_ path] (match-by-path [_ path]
(impl/fast-get data path)) (reduce
(fn [acc ^Route route]
(if-let [params ((:matcher route) path)]
(reduced (->Match (:path route) (:meta route) (:handler route) params path))))
nil data))
(match-by-name [_ name] (match-by-name [_ name]
(if-let [match (impl/fast-get lookup name)] (if-let [match (impl/fast-get lookup name)]
(match nil))) (match nil)))
(match-by-name [_ name params] (match-by-name [_ name params]
(if-let [match (impl/fast-get lookup name)] (if-let [match (impl/fast-get lookup name)]
(match params)))) (match params)))))))
(defn lookup-router (defn lookup-router
"Creates a [[LookupRouter]] from resolved routes and optional "Creates a LookupRouter from resolved routes and optional
expanded options. See [[router]] for available options" expanded options. See [[router]] for available options"
([routes] ([routes]
(lookup-router routes {})) (lookup-router routes {}))
@ -169,8 +159,26 @@
[(assoc data p (->Match p meta handler {} p)) [(assoc data p (->Match p meta handler {} p))
(if name (if name
(assoc lookup name #(->Match p meta handler % p)) (assoc lookup name #(->Match p meta handler % p))
lookup)]) [{} {}] compiled)] lookup)]) [{} {}] compiled)
(->LookupRouter routes names (impl/fast-map data) (impl/fast-map lookup))))) data (impl/fast-map data)
lookup (impl/fast-map lookup)]
(reify Routing
(router-type [_]
:lookup-router)
(routes [_]
routes)
(options [_]
opts)
(route-names [_]
names)
(match-by-path [_ path]
(impl/fast-get data path))
(match-by-name [_ name]
(if-let [match (impl/fast-get lookup name)]
(match nil)))
(match-by-name [_ name params]
(if-let [match (impl/fast-get lookup name)]
(match params)))))))
(defn router (defn router
"Create a [[Router]] from raw route data and optionally an options map. "Create a [[Router]] from raw route data and optionally an options map.

View file

@ -9,9 +9,10 @@
(testing "linear router" (testing "linear router"
(let [router (reitit/router ["/api" ["/ipa" ["/:size" ::beer]]])] (let [router (reitit/router ["/api" ["/ipa" ["/:size" ::beer]]])]
(is (instance? LinearRouter router)) (is (= :linear-router (reitit/router-type router)))
(is (= [["/api/ipa/:size" {:name ::beer}]] (is (= [["/api/ipa/:size" {:name ::beer}]]
(reitit/routes router))) (reitit/routes router)))
(is (= true (map? (reitit/options router))))
(is (= (reitit/map->Match (is (= (reitit/map->Match
{:template "/api/ipa/:size" {:template "/api/ipa/:size"
:meta {:name ::beer} :meta {:name ::beer}
@ -41,9 +42,10 @@
(testing "lookup router" (testing "lookup router"
(let [router (reitit/router ["/api" ["/ipa" ["/large" ::beer]]])] (let [router (reitit/router ["/api" ["/ipa" ["/large" ::beer]]])]
(is (instance? LookupRouter router)) (is (= :lookup-router (reitit/router-type router)))
(is (= [["/api/ipa/large" {:name ::beer}]] (is (= [["/api/ipa/large" {:name ::beer}]]
(reitit/routes router))) (reitit/routes router)))
(is (= true (map? (reitit/options router))))
(is (= (reitit/map->Match (is (= (reitit/map->Match
{:template "/api/ipa/large" {:template "/api/ipa/large"
:meta {:name ::beer} :meta {:name ::beer}