mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 08:21:11 +00:00
faster impl, removes all intermediate steps + cleanup
This commit is contained in:
parent
1e5fb601da
commit
f9841363c5
2 changed files with 62 additions and 109 deletions
|
|
@ -69,27 +69,25 @@
|
||||||
([match query-params]
|
([match query-params]
|
||||||
(some-> match :path (cond-> (seq query-params) (str "?" (impl/query-string query-params))))))
|
(some-> match :path (cond-> (seq query-params) (str "?" (impl/query-string query-params))))))
|
||||||
|
|
||||||
|
(defn- matcher [router1 router2]
|
||||||
|
(if (and router1 router2)
|
||||||
|
(reify
|
||||||
|
Router
|
||||||
|
(match-by-path [_ path]
|
||||||
|
(or (match-by-path router1 path)
|
||||||
|
(match-by-path router2 path)))
|
||||||
|
(match-by-name [_ name]
|
||||||
|
(or (match-by-name router1 name)
|
||||||
|
(match-by-name router2 name)))
|
||||||
|
(match-by-name [_ name path-params]
|
||||||
|
(or (match-by-name router1 name path-params)
|
||||||
|
(match-by-name router2 name path-params))))
|
||||||
|
(or router1 router2)))
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; Different routers
|
;; Different routers
|
||||||
;;
|
;;
|
||||||
|
|
||||||
(defn empty-router
|
|
||||||
"Creates an empty routes, not matching to anything"
|
|
||||||
([] (empty-router nil))
|
|
||||||
([opts]
|
|
||||||
^{:type ::router}
|
|
||||||
(reify Router
|
|
||||||
(router-name [_]
|
|
||||||
:empty-router)
|
|
||||||
(routes [_])
|
|
||||||
(compiled-routes [_])
|
|
||||||
(options [_]
|
|
||||||
opts)
|
|
||||||
(route-names [_])
|
|
||||||
(match-by-path [_ _])
|
|
||||||
(match-by-name [_ _])
|
|
||||||
(match-by-name [_ _ _]))))
|
|
||||||
|
|
||||||
(defn linear-router
|
(defn linear-router
|
||||||
"Creates a linear-router from resolved routes and optional
|
"Creates a linear-router from resolved routes and optional
|
||||||
expanded options. See [[router]] for available options, plus the following:
|
expanded options. See [[router]] for available options, plus the following:
|
||||||
|
|
@ -120,16 +118,11 @@
|
||||||
^{:type ::router}
|
^{:type ::router}
|
||||||
(reify
|
(reify
|
||||||
Router
|
Router
|
||||||
(router-name [_]
|
(router-name [_] :linear-router)
|
||||||
:linear-router)
|
(routes [_] routes)
|
||||||
(routes [_]
|
(compiled-routes [_] compiled-routes)
|
||||||
routes)
|
(options [_] opts)
|
||||||
(compiled-routes [_]
|
(route-names [_] names)
|
||||||
compiled-routes)
|
|
||||||
(options [_]
|
|
||||||
opts)
|
|
||||||
(route-names [_]
|
|
||||||
names)
|
|
||||||
(match-by-path [_ path]
|
(match-by-path [_ path]
|
||||||
(if-let [match (match-by-path path)]
|
(if-let [match (match-by-path path)]
|
||||||
(-> (:data match)
|
(-> (:data match)
|
||||||
|
|
@ -167,16 +160,11 @@
|
||||||
routes (impl/uncompile-routes compiled-routes)]
|
routes (impl/uncompile-routes compiled-routes)]
|
||||||
^{:type ::router}
|
^{:type ::router}
|
||||||
(reify Router
|
(reify Router
|
||||||
(router-name [_]
|
(router-name [_] :lookup-router)
|
||||||
:lookup-router)
|
(routes [_] routes)
|
||||||
(routes [_]
|
(compiled-routes [_] compiled-routes)
|
||||||
routes)
|
(options [_] opts)
|
||||||
(compiled-routes [_]
|
(route-names [_] names)
|
||||||
compiled-routes)
|
|
||||||
(options [_]
|
|
||||||
opts)
|
|
||||||
(route-names [_]
|
|
||||||
names)
|
|
||||||
(match-by-path [_ path]
|
(match-by-path [_ path]
|
||||||
(impl/fast-get data path))
|
(impl/fast-get data path))
|
||||||
(match-by-name [_ name]
|
(match-by-name [_ name]
|
||||||
|
|
@ -197,8 +185,7 @@
|
||||||
([compiled-routes]
|
([compiled-routes]
|
||||||
(trie-router compiled-routes {}))
|
(trie-router compiled-routes {}))
|
||||||
([compiled-routes opts]
|
([compiled-routes opts]
|
||||||
(if-not compiled-routes
|
(when compiled-routes
|
||||||
(empty-router opts)
|
|
||||||
(let [compiler (::trie/trie-compiler opts (trie/compiler))
|
(let [compiler (::trie/trie-compiler opts (trie/compiler))
|
||||||
names (impl/find-names compiled-routes opts)
|
names (impl/find-names compiled-routes opts)
|
||||||
[pl nl] (reduce
|
[pl nl] (reduce
|
||||||
|
|
@ -218,16 +205,11 @@
|
||||||
^{:type ::router}
|
^{:type ::router}
|
||||||
(reify
|
(reify
|
||||||
Router
|
Router
|
||||||
(router-name [_]
|
(router-name [_] :trie-router)
|
||||||
:trie-router)
|
(routes [_] routes)
|
||||||
(routes [_]
|
(compiled-routes [_] compiled-routes)
|
||||||
routes)
|
(options [_] opts)
|
||||||
(compiled-routes [_]
|
(route-names [_] names)
|
||||||
compiled-routes)
|
|
||||||
(options [_]
|
|
||||||
opts)
|
|
||||||
(route-names [_]
|
|
||||||
names)
|
|
||||||
(match-by-path [_ path]
|
(match-by-path [_ path]
|
||||||
(if-let [match (match-by-path path)]
|
(if-let [match (match-by-path path)]
|
||||||
(-> (:data match)
|
(-> (:data match)
|
||||||
|
|
@ -257,25 +239,17 @@
|
||||||
routes (impl/uncompile-routes compiled-routes)]
|
routes (impl/uncompile-routes compiled-routes)]
|
||||||
^{:type ::router}
|
^{:type ::router}
|
||||||
(reify Router
|
(reify Router
|
||||||
(router-name [_]
|
(router-name [_] :single-static-path-router)
|
||||||
:single-static-path-router)
|
(routes [_] routes)
|
||||||
(routes [_]
|
(compiled-routes [_] compiled-routes)
|
||||||
routes)
|
(options [_] opts)
|
||||||
(compiled-routes [_]
|
(route-names [_] names)
|
||||||
compiled-routes)
|
|
||||||
(options [_]
|
|
||||||
opts)
|
|
||||||
(route-names [_]
|
|
||||||
names)
|
|
||||||
(match-by-path [_ path]
|
(match-by-path [_ path]
|
||||||
(if (#?(:clj .equals :cljs =) p path)
|
(if (#?(:clj .equals :cljs =) p path) match))
|
||||||
match))
|
|
||||||
(match-by-name [_ name]
|
(match-by-name [_ name]
|
||||||
(if (= n name)
|
(if (= n name) match))
|
||||||
match))
|
|
||||||
(match-by-name [_ name path-params]
|
(match-by-name [_ name path-params]
|
||||||
(if (= n name)
|
(if (= n name) (impl/fast-assoc match :path-params (impl/path-params path-params))))))))
|
||||||
(impl/fast-assoc match :path-params (impl/path-params path-params))))))))
|
|
||||||
|
|
||||||
(defn mixed-router
|
(defn mixed-router
|
||||||
"Creates two routers: [[lookup-router]] or [[single-static-path-router]] for
|
"Creates two routers: [[lookup-router]] or [[single-static-path-router]] for
|
||||||
|
|
@ -290,28 +264,18 @@
|
||||||
wildcard-router (trie-router wild opts)
|
wildcard-router (trie-router wild opts)
|
||||||
static-router (->static-router lookup opts)
|
static-router (->static-router lookup opts)
|
||||||
names (impl/find-names compiled-routes opts)
|
names (impl/find-names compiled-routes opts)
|
||||||
routes (impl/uncompile-routes compiled-routes)]
|
routes (impl/uncompile-routes compiled-routes)
|
||||||
|
router (matcher static-router wildcard-router)]
|
||||||
^{:type ::router}
|
^{:type ::router}
|
||||||
(reify Router
|
(reify Router
|
||||||
(router-name [_]
|
(router-name [_] :mixed-router)
|
||||||
:mixed-router)
|
(routes [_] routes)
|
||||||
(routes [_]
|
(compiled-routes [_] compiled-routes)
|
||||||
routes)
|
(options [_] opts)
|
||||||
(compiled-routes [_]
|
(route-names [_] names)
|
||||||
compiled-routes)
|
(match-by-path [_ path] (match-by-path router path))
|
||||||
(options [_]
|
(match-by-name [_ name] (match-by-name router name))
|
||||||
opts)
|
(match-by-name [_ name path-params] (match-by-name router name path-params))))))
|
||||||
(route-names [_]
|
|
||||||
names)
|
|
||||||
(match-by-path [_ path]
|
|
||||||
(or (match-by-path static-router path)
|
|
||||||
(match-by-path wildcard-router path)))
|
|
||||||
(match-by-name [_ name]
|
|
||||||
(or (match-by-name static-router name)
|
|
||||||
(match-by-name wildcard-router name)))
|
|
||||||
(match-by-name [_ name path-params]
|
|
||||||
(or (match-by-name static-router name path-params)
|
|
||||||
(match-by-name wildcard-router name path-params)))))))
|
|
||||||
|
|
||||||
(defn quarantine-router
|
(defn quarantine-router
|
||||||
"Creates two routers: [[mixed-router]] for non-conflicting routes
|
"Creates two routers: [[mixed-router]] for non-conflicting routes
|
||||||
|
|
@ -326,28 +290,18 @@
|
||||||
linear-router (linear-router conflicting opts)
|
linear-router (linear-router conflicting opts)
|
||||||
mixed-router (mixed-router non-conflicting opts)
|
mixed-router (mixed-router non-conflicting opts)
|
||||||
names (impl/find-names compiled-routes opts)
|
names (impl/find-names compiled-routes opts)
|
||||||
routes (impl/uncompile-routes compiled-routes)]
|
routes (impl/uncompile-routes compiled-routes)
|
||||||
|
router (matcher mixed-router linear-router)]
|
||||||
^{:type ::router}
|
^{:type ::router}
|
||||||
(reify Router
|
(reify Router
|
||||||
(router-name [_]
|
(router-name [_] :quarantine-router)
|
||||||
:quarantine-router)
|
(routes [_] routes)
|
||||||
(routes [_]
|
(compiled-routes [_] compiled-routes)
|
||||||
routes)
|
(options [_] opts)
|
||||||
(compiled-routes [_]
|
(route-names [_] names)
|
||||||
compiled-routes)
|
(match-by-path [_ path] (match-by-path router path))
|
||||||
(options [_]
|
(match-by-name [_ name] (match-by-name router name))
|
||||||
opts)
|
(match-by-name [_ name path-params] (match-by-name router name path-params))))))
|
||||||
(route-names [_]
|
|
||||||
names)
|
|
||||||
(match-by-path [_ path]
|
|
||||||
(or (match-by-path mixed-router path)
|
|
||||||
(match-by-path linear-router path)))
|
|
||||||
(match-by-name [_ name]
|
|
||||||
(or (match-by-name mixed-router name)
|
|
||||||
(match-by-name linear-router name)))
|
|
||||||
(match-by-name [_ name path-params]
|
|
||||||
(or (match-by-name mixed-router name path-params)
|
|
||||||
(match-by-name linear-router name path-params)))))))
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; Creating Routers
|
;; Creating Routers
|
||||||
|
|
|
||||||
|
|
@ -433,8 +433,7 @@
|
||||||
(is (= :root (-> (r/match-by-path router2 "/") :data :name)))))
|
(is (= :root (-> (r/match-by-path router2 "/") :data :name)))))
|
||||||
|
|
||||||
(deftest routing-bug-test-538
|
(deftest routing-bug-test-538
|
||||||
(let [router (r/router
|
(let [router (r/router [["/:a"] ["/:b"]] {:conflicts nil})]
|
||||||
[["/:a"]
|
|
||||||
["/:b"]]
|
|
||||||
{:conflicts nil})]
|
|
||||||
(is (nil? (r/match-by-path router "")))))
|
(is (nil? (r/match-by-path router "")))))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue