mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 16:31:11 +00:00
Don't reorder routes with :linear-router, fixes #229
This commit is contained in:
parent
70ec78a72c
commit
850b47134a
4 changed files with 21 additions and 12 deletions
|
|
@ -233,17 +233,19 @@ public class Trie {
|
|||
}
|
||||
}
|
||||
|
||||
public static LinearMatcher linearMatcher(List<Matcher> childs) {
|
||||
return new LinearMatcher(childs);
|
||||
public static LinearMatcher linearMatcher(List<Matcher> childs, boolean inOrder) {
|
||||
return new LinearMatcher(childs, inOrder);
|
||||
}
|
||||
|
||||
static final class LinearMatcher implements Matcher {
|
||||
private final Matcher[] childs;
|
||||
private final int size;
|
||||
|
||||
LinearMatcher(List<Matcher> childs) {
|
||||
LinearMatcher(List<Matcher> childs, boolean inOrder) {
|
||||
this.childs = childs.toArray(new Matcher[0]);
|
||||
if (!inOrder) {
|
||||
Arrays.sort(this.childs, Comparator.comparing(Matcher::depth).thenComparing(Matcher::length).reversed());
|
||||
}
|
||||
this.size = childs.size();
|
||||
}
|
||||
|
||||
|
|
@ -286,7 +288,7 @@ public class Trie {
|
|||
linearMatcher(
|
||||
Arrays.asList(
|
||||
staticMatcher("login", dataMatcher(null, 1)),
|
||||
staticMatcher("recovery", dataMatcher(null, 2)))))));
|
||||
staticMatcher("recovery", dataMatcher(null, 2))), true))), true);
|
||||
System.err.println(matcher);
|
||||
System.out.println(lookup(matcher, "/auth/login"));
|
||||
System.out.println(lookup(matcher, "/auth/recovery"));
|
||||
|
|
|
|||
|
|
@ -96,7 +96,7 @@
|
|||
[[] {}]
|
||||
compiled-routes)
|
||||
lookup (impl/fast-map nl)
|
||||
matcher (trie/linear-matcher compiler pl)
|
||||
matcher (trie/linear-matcher compiler pl true)
|
||||
match-by-path (trie/path-matcher matcher compiler)
|
||||
routes (impl/uncompile-routes compiled-routes)]
|
||||
^{:type ::router}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@
|
|||
(static-matcher [this path matcher])
|
||||
(wild-matcher [this key end matcher])
|
||||
(catch-all-matcher [this key params data])
|
||||
(linear-matcher [this matchers])
|
||||
(linear-matcher [this matchers ordered?])
|
||||
(-pretty [this matcher])
|
||||
(-path-matcher [this matcher]))
|
||||
|
||||
|
|
@ -244,8 +244,8 @@
|
|||
(view [_] [key [data]])
|
||||
(depth [_] 1)
|
||||
(length [_]))))
|
||||
(linear-matcher [_ matchers]
|
||||
(let [matchers (vec (reverse (sort-by (juxt depth length) matchers)))
|
||||
(linear-matcher [_ matchers ordered?]
|
||||
(let [matchers (vec (if ordered? matchers (reverse (sort-by (juxt depth length) matchers))))
|
||||
size (count matchers)]
|
||||
(reify Matcher
|
||||
(match [_ i max path]
|
||||
|
|
@ -275,8 +275,8 @@
|
|||
(Trie/wildMatcher key (if end (Character. end)) matcher))
|
||||
(catch-all-matcher [_ key params data]
|
||||
(Trie/catchAllMatcher key params data))
|
||||
(linear-matcher [_ matchers]
|
||||
(Trie/linearMatcher matchers))
|
||||
(linear-matcher [_ matchers ordered?]
|
||||
(Trie/linearMatcher matchers ordered?))
|
||||
(-pretty [_ matcher]
|
||||
(-> matcher str read-string eval))
|
||||
(-path-matcher [_ matcher]
|
||||
|
|
@ -328,7 +328,7 @@
|
|||
(wild-matcher compiler pv (ffirst ends) (compile c compiler (conj cp pv)))))))
|
||||
(into (for [[p c] catch-all] (catch-all-matcher compiler (:value p) params (:data c)))))]
|
||||
(cond
|
||||
(> (count matchers) 1) (linear-matcher compiler matchers)
|
||||
(> (count matchers) 1) (linear-matcher compiler matchers false)
|
||||
(= (count matchers) 1) (first matchers)
|
||||
:else (data-matcher compiler {} nil)))))
|
||||
|
||||
|
|
|
|||
|
|
@ -382,3 +382,10 @@
|
|||
(let [router (r/router ["/endpoint" (->Named :kikka)])]
|
||||
(is (= [["/endpoint" {:name :kikka}]]
|
||||
(r/routes router)))))
|
||||
|
||||
(deftest routing-order-test-229
|
||||
(let [router (r/router
|
||||
[["/" :root]
|
||||
["/" {:name :create :method :post}]]
|
||||
{:conflicts nil})]
|
||||
(is (= :root (-> (r/match-by-path router "/") :data :name)))))
|
||||
Loading…
Reference in a new issue