Fail fast with multiple terminators.

This commit is contained in:
Tommi Reiman 2019-02-09 16:01:06 +02:00
parent 29a54a3262
commit ae1a8f7919
2 changed files with 48 additions and 32 deletions

View file

@ -260,8 +260,15 @@
matchers (-> [] matchers (-> []
(cond-> data (conj (data-matcher data))) (cond-> data (conj (data-matcher data)))
(into (for [[p c] children] (static-matcher p (compile c)))) (into (for [[p c] children] (static-matcher p (compile c))))
(into (for [[p c] wilds, end (ends c)] (into
(wild-matcher (:value p) (first end) (compile (update c :children select-keys [end]))))) (for [[p c] wilds]
(let [p (:value p)
ends (ends c)]
(if (seq (rest ends))
(throw
(ex-info
(str "Trie compliation error: wild " p " has two terminators: " ends) {}))
(wild-matcher p (ffirst ends) (compile c))))))
(into (for [[p c] catch-all] (catch-all-matcher (:value p) (:data c)))))] (into (for [[p c] catch-all] (catch-all-matcher (:value p) (:data c)))))]
(cond (cond
(> (count matchers) 1) (linear-matcher matchers) (> (count matchers) 1) (linear-matcher matchers)

View file

@ -90,37 +90,46 @@
(is (= ::wild (by-path "/olipa/kerran/avaruus/vaan/ei/toista/kertaa"))))) (is (= ::wild (by-path "/olipa/kerran/avaruus/vaan/ei/toista/kertaa")))))
(testing "bracket-params" (testing "bracket-params"
(let [router (r/router (testing "successful"
[["/{abba}" ::abba] (let [router (r/router
["/abba/1" ::abba2] [["/{abba}" ::abba]
["/{jabba}/2" ::jabba2] ["/abba/1" ::abba2]
["/{abba}/{dabba}/doo" ::doo] ["/{jabba}/2" ::jabba2]
["/abba/dabba/boo/baa" ::baa] ["/{abba}/{dabba}/doo" ::doo]
["/abba/{dabba}/boo" ::boo] ["/abba/dabba/boo/baa" ::baa]
["/{a/jabba}/{a.b/dabba}/{a.b.c/doo}/{a.b.c.d/daa}/{*foo/bar}" ::wild] ["/abba/{dabba}/boo" ::boo]
["/files/file-{name}.html" ::html] ["/{a/jabba}/{a.b/dabba}/{a.b.c/doo}/{a.b.c.d/daa}/{*foo/bar}" ::wild]
["/files/file-{name}.json" ::json] ["/files/file-{name}.html" ::html]
["/files/file-{name}-large.json" ::large]] ["/files/file-{name}.json" ::json]]
{:router r}) {:router r})
by-path #(-> router (r/match-by-path %) ((juxt (comp :name :data) :path-params)))] by-path #(-> router (r/match-by-path %) ((juxt (comp :name :data) :path-params)))]
(is (= [::abba {:abba "abba"}] (by-path "/abba"))) (is (= [::abba {:abba "abba"}] (by-path "/abba")))
(is (= [::abba2 {}] (by-path "/abba/1"))) (is (= [::abba2 {}] (by-path "/abba/1")))
(is (= [::jabba2 {:jabba "abba"}] (by-path "/abba/2"))) (is (= [::jabba2 {:jabba "abba"}] (by-path "/abba/2")))
(is (= [::doo {:abba "abba", :dabba "1"}] (by-path "/abba/1/doo"))) (is (= [::doo {:abba "abba", :dabba "1"}] (by-path "/abba/1/doo")))
(is (= [::boo {:dabba "1"}] (by-path "/abba/1/boo"))) (is (= [::boo {:dabba "1"}] (by-path "/abba/1/boo")))
(is (= [::baa {}] (by-path "/abba/dabba/boo/baa"))) (is (= [::baa {}] (by-path "/abba/dabba/boo/baa")))
(is (= [::boo {:dabba "dabba"}] (by-path "/abba/dabba/boo"))) (is (= [::boo {:dabba "dabba"}] (by-path "/abba/dabba/boo")))
(is (= [::wild {:a/jabba "olipa" (is (= [::wild {:a/jabba "olipa"
:a.b/dabba "kerran" :a.b/dabba "kerran"
:a.b.c/doo "avaruus" :a.b.c/doo "avaruus"
:a.b.c.d/daa "vaan" :a.b.c.d/daa "vaan"
:foo/bar "ei/toista/kertaa"}] :foo/bar "ei/toista/kertaa"}]
(by-path "/olipa/kerran/avaruus/vaan/ei/toista/kertaa"))) (by-path "/olipa/kerran/avaruus/vaan/ei/toista/kertaa")))
(is (= [::html {:name "10"}] (by-path "/files/file-10.html"))) (is (= [::html {:name "10"}] (by-path "/files/file-10.html")))))
(is (= [::json {:name "10"}] (by-path "/files/file-10.json")))
;(is (= [::large {:name "10"}] (by-path "/files/file-10-large.json")))
)) (testing "invalid syntax fails fast"
(testing "unbalanced brackets"
(is (thrown-with-msg?
ExceptionInfo
#"^Unbalanced brackets"
(r/router ["/kikka/{kukka"]))))
(testing "multiple terminators"
(is (thrown-with-msg?
ExceptionInfo
#"^Trie compliation error: wild :kukka has two terminators"
(r/router [["/{kukka}.json"]
["/{kukka}-json"]]))))))
(testing "empty path segments" (testing "empty path segments"
(let [router (r/router (let [router (r/router