mirror of
https://github.com/metosin/reitit.git
synced 2026-02-23 18:32:22 +00:00
Fixes for the SegmentTrie
This commit is contained in:
parent
433cf9102d
commit
b8d8286265
2 changed files with 28 additions and 10 deletions
|
|
@ -52,7 +52,7 @@ public class SegmentTrie {
|
||||||
|
|
||||||
private Map<String, SegmentTrie> childs = new HashMap<>();
|
private Map<String, SegmentTrie> childs = new HashMap<>();
|
||||||
private Map<Keyword, SegmentTrie> wilds = new HashMap<>();
|
private Map<Keyword, SegmentTrie> wilds = new HashMap<>();
|
||||||
private Keyword catchAll = null;
|
private Map<Keyword, SegmentTrie> catchAll = new HashMap<>();
|
||||||
private Object data;
|
private Object data;
|
||||||
|
|
||||||
public SegmentTrie add(String path, Object data) {
|
public SegmentTrie add(String path, Object data) {
|
||||||
|
|
@ -68,7 +68,13 @@ public class SegmentTrie {
|
||||||
}
|
}
|
||||||
pointer = s;
|
pointer = s;
|
||||||
} else if (p.startsWith("*")) {
|
} else if (p.startsWith("*")) {
|
||||||
pointer.catchAll = Keyword.intern(p.substring(1));
|
Keyword k = Keyword.intern(p.substring(1));
|
||||||
|
SegmentTrie s = pointer.catchAll.get(k);
|
||||||
|
if (s == null) {
|
||||||
|
s = new SegmentTrie();
|
||||||
|
pointer.catchAll.put(k, s);
|
||||||
|
}
|
||||||
|
pointer = s;
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
SegmentTrie s = pointer.childs.get(p);
|
SegmentTrie s = pointer.childs.get(p);
|
||||||
|
|
@ -97,8 +103,11 @@ public class SegmentTrie {
|
||||||
|
|
||||||
public Matcher matcher() {
|
public Matcher matcher() {
|
||||||
Matcher m;
|
Matcher m;
|
||||||
if (catchAll != null) {
|
if (!catchAll.isEmpty()) {
|
||||||
m = new CatchAllMatcher(catchAll, data);
|
m = new CatchAllMatcher(catchAll.keySet().iterator().next(), catchAll.values().iterator().next().data);
|
||||||
|
if (data != null) {
|
||||||
|
m = new LinearMatcher(Arrays.asList(new DataMatcher(data), m));
|
||||||
|
}
|
||||||
} else if (!wilds.isEmpty()) {
|
} else if (!wilds.isEmpty()) {
|
||||||
if (wilds.size() == 1 && data == null && childs.isEmpty()) {
|
if (wilds.size() == 1 && data == null && childs.isEmpty()) {
|
||||||
m = new WildMatcher(wilds.keySet().iterator().next(), wilds.values().iterator().next().matcher());
|
m = new WildMatcher(wilds.keySet().iterator().next(), wilds.values().iterator().next().matcher());
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,20 @@
|
||||||
(:require [clojure.test :refer [deftest testing is are]]
|
(:require [clojure.test :refer [deftest testing is are]]
|
||||||
[reitit.segment :as s]))
|
[reitit.segment :as s]))
|
||||||
|
|
||||||
(-> (s/insert nil "/foo" {:a 1}) (s/compile) (s/lookup "/foo"))
|
(deftest tests
|
||||||
; => #reitit.segment.Match{:data {:a 1}, :path-params {}}
|
(is (= (s/->Match {:a 1} {})
|
||||||
|
(-> (s/insert nil "/foo" {:a 1})
|
||||||
|
(s/compile)
|
||||||
|
(s/lookup "/foo"))))
|
||||||
|
|
||||||
(-> (s/insert nil "/foo" {:a 1}) (s/insert "/foo/*" {:b 1}) (s/compile) (s/lookup "/foo"))
|
(is (= (s/->Match {:a 1} {})
|
||||||
; => nil
|
(-> (s/insert nil "/foo" {:a 1})
|
||||||
|
(s/insert "/foo/*bar" {:b 1})
|
||||||
|
(s/compile)
|
||||||
|
(s/lookup "/foo"))))
|
||||||
|
|
||||||
(-> (s/insert nil "/foo" {:a 1}) (s/insert "/foo/*" {:b 1}) (s/compile) (s/lookup "/foo/bar"))
|
(is (= (s/->Match {:b 1} {:bar "bar"})
|
||||||
; => #reitit.segment.Match{:data {:b 1}, :path-params {: "bar"}}
|
(-> (s/insert nil "/foo" {:a 1})
|
||||||
|
(s/insert "/foo/*bar" {:b 1})
|
||||||
|
(s/compile)
|
||||||
|
(s/lookup "/foo/bar")))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue