Permalink
Browse files

Introduce ring.middleware.keyword-params.

  • Loading branch information...
1 parent 4bc4619 commit d2b80a987e3a3f770baee89f87622545996f9690 @mmcgrana committed Feb 28, 2010
View
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))))
View
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

Please sign in to comment.