Permalink
Browse files

Moved assoc-param to ring.util.data/assoc+

  • Loading branch information...
1 parent 84446f6 commit c1e23d5e206f7ff804e78f30bb9fea469e75a5a7 @weavejester weavejester committed Mar 23, 2012
@@ -1,6 +1,6 @@
(ns ring.middleware.multipart-params
"Parse multipart upload into params."
- (:use [ring.util.codec :only (assoc-param)])
+ (:use [ring.util.data :only (assoc+)])
(:import [org.apache.commons.fileupload.util Streams]
[org.apache.commons.fileupload
RequestContext
@@ -54,7 +54,7 @@
(->> (request-context request encoding)
(file-item-seq)
(map #(parse-file-item % store))
- (reduce (fn [m [k v]] (assoc-param m k v)) {})))
+ (reduce (fn [m [k v]] (assoc+ m k v)) {})))
(defn- load-var
"Returns the var named by the supplied symbol, or nil if not found. Attempts
@@ -1,19 +1,20 @@
(ns ring.util.codec
"Encoding and decoding utilities."
+ (:use ring.util.data)
+ (:require [clojure.string :as string])
(:import java.io.File
(java.net URLEncoder URLDecoder)
- org.apache.commons.codec.binary.Base64)
- (:require [clojure.string :as string]))
+ org.apache.commons.codec.binary.Base64))
(defn url-encode
- "Returns the form-url-encoded version of the given string, using either a
- specified encoding or UTF-8 by default."
+ "Returns the url-encoded version of the given string, using either a specified
+ encoding or UTF-8 by default."
[unencoded & [encoding]]
(URLEncoder/encode unencoded (or encoding "UTF-8")))
(defn url-decode
- "Returns the form-url-decoded version of the given string, using either a
- specified encoding or UTF-8 by default."
+ "Returns the url-decoded version of the given string, using either a specified
+ encoding or UTF-8 by default. If the encoding is invalid, nil is returned."
[encoded & [encoding]]
(try
(URLDecoder/decode encoded (or encoding "UTF-8"))
@@ -29,17 +30,6 @@
[^String encoded]
(Base64/decodeBase64 (.getBytes encoded)))
-(defn assoc-param
- "Associate a key with a value. If the key already exists in the map,
- create a vector of values."
- [map key val]
- (assoc map key
- (if-let [cur (map key)]
- (if (vector? cur)
- (conj cur val)
- [cur val])
- val)))
-
(defn form-decode
"Parse parameters from a string into a map."
([^String param-string]
@@ -48,9 +38,9 @@
(reduce
(fn [param-map encoded-param]
(if-let [[_ key val] (re-matches #"([^=]+)=(.*)" encoded-param)]
- (assoc-param param-map
- (url-decode key encoding)
- (url-decode (or val "") encoding))
+ (assoc+ param-map
+ (url-decode key encoding)
+ (url-decode (or val "") encoding))
param-map))
{}
(string/split param-string #"&"))))
@@ -0,0 +1,13 @@
+(ns ring.util.data
+ "Miscellaneous functions for manipulating data structures.")
+
+(defn assoc+
+ "Associate a key with a value in a map. If the key already exists in the map,
+ a vector of values is associated with the key."
+ [map key val]
+ (assoc map key
+ (if-let [cur (get map key)]
+ (if (vector? cur)
+ (conj cur val)
+ [cur val])
+ val)))

0 comments on commit c1e23d5

Please sign in to comment.