mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 08:21: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) {
|
public static LinearMatcher linearMatcher(List<Matcher> childs, boolean inOrder) {
|
||||||
return new LinearMatcher(childs);
|
return new LinearMatcher(childs, inOrder);
|
||||||
}
|
}
|
||||||
|
|
||||||
static final class LinearMatcher implements Matcher {
|
static final class LinearMatcher implements Matcher {
|
||||||
private final Matcher[] childs;
|
private final Matcher[] childs;
|
||||||
private final int size;
|
private final int size;
|
||||||
|
|
||||||
LinearMatcher(List<Matcher> childs) {
|
LinearMatcher(List<Matcher> childs, boolean inOrder) {
|
||||||
this.childs = childs.toArray(new Matcher[0]);
|
this.childs = childs.toArray(new Matcher[0]);
|
||||||
Arrays.sort(this.childs, Comparator.comparing(Matcher::depth).thenComparing(Matcher::length).reversed());
|
if (!inOrder) {
|
||||||
|
Arrays.sort(this.childs, Comparator.comparing(Matcher::depth).thenComparing(Matcher::length).reversed());
|
||||||
|
}
|
||||||
this.size = childs.size();
|
this.size = childs.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -286,7 +288,7 @@ public class Trie {
|
||||||
linearMatcher(
|
linearMatcher(
|
||||||
Arrays.asList(
|
Arrays.asList(
|
||||||
staticMatcher("login", dataMatcher(null, 1)),
|
staticMatcher("login", dataMatcher(null, 1)),
|
||||||
staticMatcher("recovery", dataMatcher(null, 2)))))));
|
staticMatcher("recovery", dataMatcher(null, 2))), true))), true);
|
||||||
System.err.println(matcher);
|
System.err.println(matcher);
|
||||||
System.out.println(lookup(matcher, "/auth/login"));
|
System.out.println(lookup(matcher, "/auth/login"));
|
||||||
System.out.println(lookup(matcher, "/auth/recovery"));
|
System.out.println(lookup(matcher, "/auth/recovery"));
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,7 @@
|
||||||
[[] {}]
|
[[] {}]
|
||||||
compiled-routes)
|
compiled-routes)
|
||||||
lookup (impl/fast-map nl)
|
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)
|
match-by-path (trie/path-matcher matcher compiler)
|
||||||
routes (impl/uncompile-routes compiled-routes)]
|
routes (impl/uncompile-routes compiled-routes)]
|
||||||
^{:type ::router}
|
^{:type ::router}
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@
|
||||||
(static-matcher [this path matcher])
|
(static-matcher [this path matcher])
|
||||||
(wild-matcher [this key end matcher])
|
(wild-matcher [this key end matcher])
|
||||||
(catch-all-matcher [this key params data])
|
(catch-all-matcher [this key params data])
|
||||||
(linear-matcher [this matchers])
|
(linear-matcher [this matchers ordered?])
|
||||||
(-pretty [this matcher])
|
(-pretty [this matcher])
|
||||||
(-path-matcher [this matcher]))
|
(-path-matcher [this matcher]))
|
||||||
|
|
||||||
|
|
@ -244,8 +244,8 @@
|
||||||
(view [_] [key [data]])
|
(view [_] [key [data]])
|
||||||
(depth [_] 1)
|
(depth [_] 1)
|
||||||
(length [_]))))
|
(length [_]))))
|
||||||
(linear-matcher [_ matchers]
|
(linear-matcher [_ matchers ordered?]
|
||||||
(let [matchers (vec (reverse (sort-by (juxt depth length) matchers)))
|
(let [matchers (vec (if ordered? matchers (reverse (sort-by (juxt depth length) matchers))))
|
||||||
size (count matchers)]
|
size (count matchers)]
|
||||||
(reify Matcher
|
(reify Matcher
|
||||||
(match [_ i max path]
|
(match [_ i max path]
|
||||||
|
|
@ -275,8 +275,8 @@
|
||||||
(Trie/wildMatcher key (if end (Character. end)) matcher))
|
(Trie/wildMatcher key (if end (Character. end)) matcher))
|
||||||
(catch-all-matcher [_ key params data]
|
(catch-all-matcher [_ key params data]
|
||||||
(Trie/catchAllMatcher key params data))
|
(Trie/catchAllMatcher key params data))
|
||||||
(linear-matcher [_ matchers]
|
(linear-matcher [_ matchers ordered?]
|
||||||
(Trie/linearMatcher matchers))
|
(Trie/linearMatcher matchers ordered?))
|
||||||
(-pretty [_ matcher]
|
(-pretty [_ matcher]
|
||||||
(-> matcher str read-string eval))
|
(-> matcher str read-string eval))
|
||||||
(-path-matcher [_ matcher]
|
(-path-matcher [_ matcher]
|
||||||
|
|
@ -328,7 +328,7 @@
|
||||||
(wild-matcher compiler pv (ffirst ends) (compile c compiler (conj cp pv)))))))
|
(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)))))]
|
(into (for [[p c] catch-all] (catch-all-matcher compiler (:value p) params (:data c)))))]
|
||||||
(cond
|
(cond
|
||||||
(> (count matchers) 1) (linear-matcher compiler matchers)
|
(> (count matchers) 1) (linear-matcher compiler matchers false)
|
||||||
(= (count matchers) 1) (first matchers)
|
(= (count matchers) 1) (first matchers)
|
||||||
:else (data-matcher compiler {} nil)))))
|
:else (data-matcher compiler {} nil)))))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -382,3 +382,10 @@
|
||||||
(let [router (r/router ["/endpoint" (->Named :kikka)])]
|
(let [router (r/router ["/endpoint" (->Named :kikka)])]
|
||||||
(is (= [["/endpoint" {:name :kikka}]]
|
(is (= [["/endpoint" {:name :kikka}]]
|
||||||
(r/routes router)))))
|
(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