Merge pull request #539 from metosin/fix-538

fix #538
This commit is contained in:
Tommi Reiman 2022-03-10 15:34:05 +02:00 committed by GitHub
commit ae73d031b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 70 deletions

View file

@ -103,16 +103,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)
@ -150,16 +145,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]
@ -193,24 +183,19 @@
[nil {}] [nil {}]
compiled-routes) compiled-routes)
matcher (trie/compile pl compiler) matcher (trie/compile pl compiler)
match-by-path (trie/path-matcher matcher compiler) match-by-path (if matcher (trie/path-matcher matcher compiler))
lookup (impl/fast-map nl) lookup (impl/fast-map nl)
routes (impl/uncompile-routes compiled-routes)] routes (impl/uncompile-routes compiled-routes)]
^{: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 (and match-by-path (match-by-path path))]
(-> (:data match) (-> (:data match)
(assoc :path-params (:params match)) (assoc :path-params (:params match))
(assoc :path path)))) (assoc :path path))))
@ -238,25 +223,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
@ -274,16 +251,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 [_] :mixed-router)
:mixed-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]
(or (match-by-path static-router path) (or (match-by-path static-router path)
(match-by-path wildcard-router path))) (match-by-path wildcard-router path)))
@ -310,16 +282,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 [_] :quarantine-router)
:quarantine-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]
(or (match-by-path mixed-router path) (or (match-by-path mixed-router path)
(match-by-path linear-router path))) (match-by-path linear-router path)))

View file

@ -364,8 +364,7 @@
(into (for [[p c] catch-all] (catch-all-matcher compiler (:value p) params (:data c)))))] (into (for [[p c] catch-all] (catch-all-matcher compiler (:value p) params (:data c)))))]
(cond (cond
(> (count matchers) 1) (linear-matcher compiler matchers false) (> (count matchers) 1) (linear-matcher compiler matchers false)
(= (count matchers) 1) (first matchers) (= (count matchers) 1) (first matchers)))))
:else (data-matcher compiler {} nil)))))
(defn pretty (defn pretty
"Returns a simplified EDN structure of a compiled trie for printing purposes." "Returns a simplified EDN structure of a compiled trie for printing purposes."

View file

@ -431,3 +431,7 @@
{:conflicts nil})] {:conflicts nil})]
(is (= :root (-> (r/match-by-path router "/") :data :name))) (is (= :root (-> (r/match-by-path router "/") :data :name)))
(is (= :root (-> (r/match-by-path router2 "/") :data :name))))) (is (= :root (-> (r/match-by-path router2 "/") :data :name)))))
(deftest routing-bug-test-538
(let [router (r/router [["/:a"] ["/:b"]] {:conflicts nil})]
(is (nil? (r/match-by-path router "")))))