Skip to content

Commit ddea02a

Browse files
committed
Add :<< bindings (fixes weavejester#131)
1 parent c0e42d5 commit ddea02a

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

src/compojure/core.clj

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,30 @@
6262
(clout/route-compile ~route)
6363
~route)))
6464

65-
(defn- assoc-&-binding [binds req sym]
66-
(assoc binds sym `(dissoc (:params ~req)
67-
~@(map keyword (keys binds))
68-
~@(map str (keys binds)))))
65+
(defn- and-binding [req binds]
66+
`(dissoc (:params ~req) ~@(map keyword (keys binds)) ~@(map str (keys binds))))
6967

70-
(defn- assoc-symbol-binding [binds req sym]
71-
(assoc binds sym `(get-in ~req [:params ~(keyword sym)]
72-
(get-in ~req [:params ~(str sym)]))))
68+
(defn- symbol-binding [req sym]
69+
`(get-in ~req [:params ~(keyword sym)] (get-in ~req [:params ~(str sym)])))
70+
71+
(defn- application-binding [req sym func]
72+
`(~func ~(symbol-binding req sym)))
7373

7474
(defn- vector-bindings [args req]
7575
(loop [args args, binds {}]
76-
(if-let [sym (first args)]
77-
(cond
78-
(= '& sym)
79-
(recur (nnext args) (assoc-&-binding binds req (second args)))
80-
(= :as sym)
81-
(recur (nnext args) (assoc binds (second args) req))
82-
(symbol? sym)
83-
(recur (next args) (assoc-symbol-binding binds req sym))
84-
:else
85-
(throw (Exception. (str "Unexpected binding: " sym))))
76+
(if (first args)
77+
(let [[x y z] args]
78+
(cond
79+
(= '& x)
80+
(recur (nnext args) (assoc binds y (and-binding req binds)))
81+
(= :as x)
82+
(recur (nnext args) (assoc binds y req))
83+
(and (symbol? x) (= :<< y) (nnext args))
84+
(recur (drop 3 args) (assoc binds x (application-binding req x z)))
85+
(symbol? x)
86+
(recur (next args) (assoc binds x (symbol-binding req x)))
87+
:else
88+
(throw (Exception. (str "Unexpected binding: " x)))))
8689
(mapcat identity binds))))
8790

8891
(defn- warn-on-*-bindings! [bindings]

test/compojure/core_test.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
nil)
4343
req)))
4444

45+
(testing "vector 'x :<< coercion' arguments"
46+
(let [req (mock/request :get "/foo/10")]
47+
((GET "/:x/:y" [x y :<< #(Integer/parseInt %)]
48+
(is (= x "foo"))
49+
(is (= y 10))
50+
nil)
51+
req)))
52+
4553
(testing "map arguments"
4654
((GET "/foo" {params :params}
4755
(is (= params {:x "a", :y "b"}))

0 commit comments

Comments
 (0)