This commit is contained in:
Tommi Reiman 2022-03-10 13:42:53 +02:00
parent acfc48faa1
commit 1e5fb601da
2 changed files with 66 additions and 40 deletions

View file

@ -73,6 +73,23 @@
;; 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:
@ -180,6 +197,8 @@
([compiled-routes] ([compiled-routes]
(trie-router compiled-routes {})) (trie-router compiled-routes {}))
([compiled-routes opts] ([compiled-routes opts]
(if-not 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
@ -219,7 +238,7 @@
(match nil))) (match nil)))
(match-by-name [_ name path-params] (match-by-name [_ name path-params]
(if-let [match (impl/fast-get lookup name)] (if-let [match (impl/fast-get lookup name)]
(match (impl/path-params path-params)))))))) (match (impl/path-params path-params)))))))))
(defn single-static-path-router (defn single-static-path-router
"Creates a fast router of 1 static route(s) and optional "Creates a fast router of 1 static route(s) and optional

View file

@ -431,3 +431,10 @@
{: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 "")))))