fast-router -> single-static-path-router

This commit is contained in:
Tommi Reiman 2017-10-02 17:53:07 +03:00
parent e9c0639914
commit 8afbbee813
2 changed files with 14 additions and 14 deletions

View file

@ -213,16 +213,16 @@
(if-let [match (impl/fast-get lookup name)] (if-let [match (impl/fast-get lookup name)]
(match params))))))) (match params)))))))
(defn fast-router (defn single-static-path-router
"Creates a super-fast router of 1 static route(s) and optional "Creates a fast router of 1 static route(s) and optional
expanded options. See [[router]] for available options" expanded options. See [[router]] for available options"
([routes] ([routes]
(fast-router routes {})) (single-static-path-router routes {}))
([routes opts] ([routes opts]
(when (or (not= (count routes) 1) (some impl/wild-route? routes)) (when (or (not= (count routes) 1) (some impl/wild-route? routes))
(throw (throw
(ex-info (ex-info
(str ":fast-router requires exactly 1 static route: " routes) (str ":single-static-path-router requires exactly 1 static route: " routes)
{:routes routes}))) {:routes routes})))
(let [[n :as names] (find-names routes opts) (let [[n :as names] (find-names routes opts)
[[p meta result] :as compiled] (compile-routes routes opts) [[p meta result] :as compiled] (compile-routes routes opts)
@ -230,7 +230,7 @@
match (->Match p meta result {} p)] match (->Match p meta result {} p)]
(reify Router (reify Router
(router-name [_] (router-name [_]
:fast-router) :single-static-path-router)
(routes [_] (routes [_]
compiled) compiled)
(options [_] (options [_]
@ -248,7 +248,7 @@
(impl/fast-assoc match :params params))))))) (impl/fast-assoc match :params params)))))))
(defn mixed-router (defn mixed-router
"Creates two routers: [[lookup-router]] or [[fast-ruoter]] for "Creates two routers: [[lookup-router]] or [[single-static-path-router]] for
static routes and [[linear-router]] for wildcard routes. All static routes and [[linear-router]] for wildcard routes. All
routes should be non-conflicting. Takes resolved routes and optional routes should be non-conflicting. Takes resolved routes and optional
expanded options. See [[router]] for options." expanded options. See [[router]] for options."
@ -256,7 +256,7 @@
(mixed-router routes {})) (mixed-router routes {}))
([routes opts] ([routes opts]
(let [{linear true, lookup false} (group-by impl/wild-route? routes) (let [{linear true, lookup false} (group-by impl/wild-route? routes)
->static-router (if (= 1 (count lookup)) fast-router lookup-router) ->static-router (if (= 1 (count lookup)) single-static-path-router lookup-router)
wildcard-router (linear-router linear opts) wildcard-router (linear-router linear opts)
static-router (->static-router lookup opts) static-router (->static-router lookup opts)
names (find-names routes opts)] names (find-names routes opts)]
@ -304,7 +304,7 @@
all-wilds? (every? impl/wild-route? routes) all-wilds? (every? impl/wild-route? routes)
router (cond router (cond
router router router router
(and (= 1 (count routes)) (not wilds?)) fast-router (and (= 1 (count routes)) (not wilds?)) single-static-path-router
(not wilds?) lookup-router (not wilds?) lookup-router
all-wilds? linear-router all-wilds? linear-router
(not conflicting) mixed-router (not conflicting) mixed-router

View file

@ -70,9 +70,9 @@
(r/resolve-routes (r/resolve-routes
["/api/:version/ping"] {}))))))) ["/api/:version/ping"] {})))))))
(testing "fast-router" (testing "single-static-path-router"
(let [router (r/router ["/api" ["/ipa" ["/large" ::beer]]])] (let [router (r/router ["/api" ["/ipa" ["/large" ::beer]]])]
(is (= :fast-router (r/router-name router))) (is (= :single-static-path-router (r/router-name router)))
(is (= [["/api/ipa/large" {:name ::beer} nil]] (is (= [["/api/ipa/large" {:name ::beer} nil]]
(r/routes router))) (r/routes router)))
(is (= true (map? (r/options router)))) (is (= true (map? (r/options router))))
@ -94,16 +94,16 @@
(testing "can't be created with wildcard routes" (testing "can't be created with wildcard routes"
(is (thrown-with-msg? (is (thrown-with-msg?
ExceptionInfo ExceptionInfo
#":fast-router requires exactly 1 static route" #":single-static-path-router requires exactly 1 static route"
(r/fast-router (r/single-static-path-router
(r/resolve-routes (r/resolve-routes
["/api/:version/ping"] {}))))) ["/api/:version/ping"] {})))))
(testing "can't be created with multiple routes" (testing "can't be created with multiple routes"
(is (thrown-with-msg? (is (thrown-with-msg?
ExceptionInfo ExceptionInfo
#":fast-router requires exactly 1 static route" #":single-static-path-router requires exactly 1 static route"
(r/fast-router (r/single-static-path-router
(r/resolve-routes (r/resolve-routes
[["/ping"] [["/ping"]
["/pong"]] {}))))))) ["/pong"]] {})))))))