one more time

This commit is contained in:
Tommi Reiman 2022-03-10 15:21:15 +02:00
parent f9841363c5
commit b0602d60c9
2 changed files with 56 additions and 63 deletions

View file

@ -69,21 +69,6 @@
([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
;; ;;
@ -185,42 +170,41 @@
([compiled-routes] ([compiled-routes]
(trie-router compiled-routes {})) (trie-router compiled-routes {}))
([compiled-routes opts] ([compiled-routes opts]
(when compiled-routes (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 (fn [[pl nl] [p {:keys [name] :as data} result]]
(fn [[pl nl] [p {:keys [name] :as data} result]] (let [{:keys [path-params] :as route} (impl/parse p opts)
(let [{:keys [path-params] :as route} (impl/parse p opts) f #(if-let [path (impl/path-for route %)]
f #(if-let [path (impl/path-for route %)] (->Match p data result (impl/url-decode-coll %) path)
(->Match p data result (impl/url-decode-coll %) path) (->PartialMatch p data result (impl/url-decode-coll %) path-params))]
(->PartialMatch p data result (impl/url-decode-coll %) path-params))] [(trie/insert pl p (->Match p data result nil nil) opts)
[(trie/insert pl p (->Match p data result nil nil) opts) (if name (assoc nl name f) nl)]))
(if name (assoc nl name f) nl)])) [nil {}]
[nil {}] compiled-routes)
compiled-routes) matcher (trie/compile pl compiler)
matcher (trie/compile pl compiler) match-by-path (if matcher (trie/path-matcher matcher compiler))
match-by-path (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 [_] :trie-router)
(router-name [_] :trie-router) (routes [_] routes)
(routes [_] routes) (compiled-routes [_] compiled-routes)
(compiled-routes [_] compiled-routes) (options [_] opts)
(options [_] opts) (route-names [_] names)
(route-names [_] names) (match-by-path [_ path]
(match-by-path [_ path] (if-let [match (and match-by-path (match-by-path path))]
(if-let [match (match-by-path path)] (-> (:data match)
(-> (:data match) (assoc :path-params (:params match))
(assoc :path-params (:params match)) (assoc :path path))))
(assoc :path path)))) (match-by-name [_ name]
(match-by-name [_ name] (if-let [match (impl/fast-get lookup name)]
(if-let [match (impl/fast-get lookup name)] (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
@ -264,8 +248,7 @@
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 [_] :mixed-router) (router-name [_] :mixed-router)
@ -273,9 +256,15 @@
(compiled-routes [_] compiled-routes) (compiled-routes [_] compiled-routes)
(options [_] opts) (options [_] opts)
(route-names [_] names) (route-names [_] names)
(match-by-path [_ path] (match-by-path router path)) (match-by-path [_ path]
(match-by-name [_ name] (match-by-name router name)) (or (match-by-path static-router path)
(match-by-name [_ name path-params] (match-by-name router name path-params)))))) (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
@ -290,8 +279,7 @@
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 [_] :quarantine-router) (router-name [_] :quarantine-router)
@ -299,9 +287,15 @@
(compiled-routes [_] compiled-routes) (compiled-routes [_] compiled-routes)
(options [_] opts) (options [_] opts)
(route-names [_] names) (route-names [_] names)
(match-by-path [_ path] (match-by-path router path)) (match-by-path [_ path]
(match-by-name [_ name] (match-by-name router name)) (or (match-by-path mixed-router path)
(match-by-name [_ name path-params] (match-by-name router name path-params)))))) (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

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."