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 (-> []
(cond-> data (conj (data-matcher data)))
(into (for [[p c] children] (static-matcher p (compile c))))
(into (for [[p c] wilds, end (ends c)]
(wild-matcher (:value p) (first end) (compile (update c :children select-keys [end])))))
(into
(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)))))]
(cond
(> (count matchers) 1) (linear-matcher matchers)

View file

@ -90,6 +90,7 @@
(is (= ::wild (by-path "/olipa/kerran/avaruus/vaan/ei/toista/kertaa")))))
(testing "bracket-params"
(testing "successful"
(let [router (r/router
[["/{abba}" ::abba]
["/abba/1" ::abba2]
@ -99,8 +100,7 @@
["/abba/{dabba}/boo" ::boo]
["/{a/jabba}/{a.b/dabba}/{a.b.c/doo}/{a.b.c.d/daa}/{*foo/bar}" ::wild]
["/files/file-{name}.html" ::html]
["/files/file-{name}.json" ::json]
["/files/file-{name}-large.json" ::large]]
["/files/file-{name}.json" ::json]]
{:router r})
by-path #(-> router (r/match-by-path %) ((juxt (comp :name :data) :path-params)))]
(is (= [::abba {:abba "abba"}] (by-path "/abba")))
@ -116,11 +116,20 @@
:a.b.c.d/daa "vaan"
:foo/bar "ei/toista/kertaa"}]
(by-path "/olipa/kerran/avaruus/vaan/ei/toista/kertaa")))
(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")))
(is (= [::html {:name "10"}] (by-path "/files/file-10.html")))))
))
(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"
(let [router (r/router