Permalink
Please sign in to comment.
Showing
with
34 additions
and 0 deletions.
22
ring-core/src/ring/middleware/keyword_params.clj
| @@ -0,0 +1,22 @@ | ||
| +(ns ring.middleware.keyword-params) | ||
| + | ||
| +(defn- keyify-params [target] | ||
| + (cond | ||
| + (string? target) | ||
| + target | ||
| + (map? target) | ||
| + (reduce | ||
| + (fn [m [k v]] | ||
| + (assoc m (keyword k) (keyify-params v))) | ||
| + {} | ||
| + target) | ||
| + (vector? target) | ||
| + (vec (map keyify-params target)))) | ||
| + | ||
| +(defn wrap-keyword-params | ||
| + "Middleware that converts the string-keyed :params map to one with keyword | ||
| + keys before forwarding the request to the given handler. | ||
| + Does not alter the maps under :*-params keys; these are left with strings." | ||
| + [handler] | ||
| + (fn [req] | ||
| + (handler (update-in req [:params] keyify-params)))) |
12
ring-core/test/ring/middleware/keyword_params_test.clj
| @@ -0,0 +1,12 @@ | ||
| +(ns ring.middleware.keyword-params-test | ||
| + (:use clojure.test | ||
| + ring.middleware.keyword-params)) | ||
| + | ||
| +(def wrapped-echo (wrap-keyword-params identity)) | ||
| + | ||
| +(deftest test-wrap-keyword-params | ||
| + (are [in out] (= out (:params (wrapped-echo {:params in}))) | ||
| + {"foo" "bar" "biz" "bat"} | ||
| + {:foo "bar" :biz "bat"} | ||
| + {"foo" "bar" "biz" [{"bat" "one"} {"bat" "two"}]} | ||
| + {:foo "bar" :biz [{:bat "one"} {:bat "two"}]})) |
0 comments on commit
d2b80a9