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,46 +197,48 @@
([compiled-routes] ([compiled-routes]
(trie-router compiled-routes {})) (trie-router compiled-routes {}))
([compiled-routes opts] ([compiled-routes opts]
(let [compiler (::trie/trie-compiler opts (trie/compiler)) (if-not compiled-routes
names (impl/find-names compiled-routes opts) (empty-router opts)
[pl nl] (reduce (let [compiler (::trie/trie-compiler opts (trie/compiler))
(fn [[pl nl] [p {:keys [name] :as data} result]] names (impl/find-names compiled-routes opts)
(let [{:keys [path-params] :as route} (impl/parse p opts) [pl nl] (reduce
f #(if-let [path (impl/path-for route %)] (fn [[pl nl] [p {:keys [name] :as data} result]]
(->Match p data result (impl/url-decode-coll %) path) (let [{:keys [path-params] :as route} (impl/parse p opts)
(->PartialMatch p data result (impl/url-decode-coll %) path-params))] f #(if-let [path (impl/path-for route %)]
[(trie/insert pl p (->Match p data result nil nil) opts) (->Match p data result (impl/url-decode-coll %) path)
(if name (assoc nl name f) nl)])) (->PartialMatch p data result (impl/url-decode-coll %) path-params))]
[nil {}] [(trie/insert pl p (->Match p data result nil nil) opts)
compiled-routes) (if name (assoc nl name f) nl)]))
matcher (trie/compile pl compiler) [nil {}]
match-by-path (trie/path-matcher matcher compiler) compiled-routes)
lookup (impl/fast-map nl) matcher (trie/compile pl compiler)
routes (impl/uncompile-routes compiled-routes)] match-by-path (trie/path-matcher matcher compiler)
^{:type ::router} lookup (impl/fast-map nl)
(reify routes (impl/uncompile-routes compiled-routes)]
Router ^{:type ::router}
(router-name [_] (reify
:trie-router) Router
(routes [_] (router-name [_]
routes) :trie-router)
(compiled-routes [_] (routes [_]
compiled-routes) routes)
(options [_] (compiled-routes [_]
opts) compiled-routes)
(route-names [_] (options [_]
names) opts)
(match-by-path [_ path] (route-names [_]
(if-let [match (match-by-path path)] names)
(-> (:data match) (match-by-path [_ path]
(assoc :path-params (:params match)) (if-let [match (match-by-path path)]
(assoc :path path)))) (-> (:data match)
(match-by-name [_ name] (assoc :path-params (:params match))
(if-let [match (impl/fast-get lookup name)] (assoc :path path))))
(match nil))) (match-by-name [_ name]
(match-by-name [_ name path-params] (if-let [match (impl/fast-get lookup name)]
(if-let [match (impl/fast-get lookup name)] (match nil)))
(match (impl/path-params path-params)))))))) (match-by-name [_ name path-params]
(if-let [match (impl/fast-get lookup name)]
(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 "")))))