diff --git a/modules/reitit-core/java-src/reitit/Trie.java b/modules/reitit-core/java-src/reitit/Trie.java index 55250fdc..b9b3b167 100644 --- a/modules/reitit-core/java-src/reitit/Trie.java +++ b/modules/reitit-core/java-src/reitit/Trie.java @@ -67,6 +67,8 @@ public class Trie { public interface Matcher { Match match(int i, Path path, Match match); + + int depth(); } public static StaticMatcher staticMatcher(String path, Matcher child) { @@ -98,6 +100,11 @@ public class Trie { return child.match(i + size, path, match); } + @Override + public int depth() { + return child.depth() + 1; + } + @Override public String toString() { return "[\"" + new String(path) + "\" " + child + "]"; @@ -124,6 +131,11 @@ public class Trie { return null; } + @Override + public int depth() { + return 1; + } + @Override public String toString() { return (data != null ? data.toString() : "nil"); @@ -171,6 +183,11 @@ public class Trie { return null; } + @Override + public int depth() { + return child.depth() + 1; + } + @Override public String toString() { return "[" + key + " " + child + "]"; @@ -200,6 +217,11 @@ public class Trie { return null; } + @Override + public int depth() { + return 1; + } + @Override public String toString() { return "[" + parameter + " " + new DataMatcher(data) + "]"; @@ -217,6 +239,7 @@ public class Trie { LinearMatcher(List childs) { this.childs = childs.toArray(new Matcher[0]); + Arrays.sort(this.childs, Comparator.comparing(Matcher::depth).reversed()); this.size = childs.size(); } @@ -231,6 +254,11 @@ public class Trie { return null; } + @Override + public int depth() { + return Arrays.stream(childs).mapToInt(Matcher::depth).max().orElseThrow(NoSuchElementException::new); + } + @Override public String toString() { return Arrays.toString(childs); diff --git a/modules/reitit-core/src/reitit/trie.cljc b/modules/reitit-core/src/reitit/trie.cljc index a1f3aa9c..a6417423 100644 --- a/modules/reitit-core/src/reitit/trie.cljc +++ b/modules/reitit-core/src/reitit/trie.cljc @@ -85,7 +85,7 @@ (assoc-in node [:children path] (-insert (-node {}) ps data))))] (if-let [child (get-in node' [:children ""])] ;; optimize by removing empty paths - (-> (merge-with merge node' child) + (-> (merge-with merge (dissoc node' :data) child) (update :children dissoc "")) node')))