Permalink
Browse files

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

1 parent 84446f6 commit c1e23d5e206f7ff804e78f30bb9fea469e75a5a7 @weavejester weavejester committed Mar 23, 2012
View
4 ring-core/src/ring/middleware/multipart_params.clj
@@ -1,6 +1,6 @@
(ns ring.middleware.multipart-params (ns ring.middleware.multipart-params
"Parse multipart upload into 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] (:import [org.apache.commons.fileupload.util Streams]
[org.apache.commons.fileupload [org.apache.commons.fileupload
RequestContext RequestContext
@@ -54,7 +54,7 @@
(->> (request-context request encoding) (->> (request-context request encoding)
(file-item-seq) (file-item-seq)
(map #(parse-file-item % store)) (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 (defn- load-var
"Returns the var named by the supplied symbol, or nil if not found. Attempts "Returns the var named by the supplied symbol, or nil if not found. Attempts
View
30 ring-core/src/ring/util/codec.clj
@@ -1,19 +1,20 @@
(ns ring.util.codec (ns ring.util.codec
"Encoding and decoding utilities." "Encoding and decoding utilities."
+ (:use ring.util.data)
+ (:require [clojure.string :as string])
(:import java.io.File (:import java.io.File
(java.net URLEncoder URLDecoder) (java.net URLEncoder URLDecoder)
- org.apache.commons.codec.binary.Base64) + org.apache.commons.codec.binary.Base64))
- (:require [clojure.string :as string]))
(defn url-encode (defn url-encode
- "Returns the form-url-encoded version of the given string, using either a + "Returns the url-encoded version of the given string, using either a specified
- specified encoding or UTF-8 by default." + encoding or UTF-8 by default."
[unencoded & [encoding]] [unencoded & [encoding]]
(URLEncoder/encode unencoded (or encoding "UTF-8"))) (URLEncoder/encode unencoded (or encoding "UTF-8")))
(defn url-decode (defn url-decode
- "Returns the form-url-decoded version of the given string, using either a + "Returns the url-decoded version of the given string, using either a specified
- specified encoding or UTF-8 by default." + encoding or UTF-8 by default. If the encoding is invalid, nil is returned."
[encoded & [encoding]] [encoded & [encoding]]
(try (try
(URLDecoder/decode encoded (or encoding "UTF-8")) (URLDecoder/decode encoded (or encoding "UTF-8"))
@@ -29,17 +30,6 @@
[^String encoded] [^String encoded]
(Base64/decodeBase64 (.getBytes 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 (defn form-decode
"Parse parameters from a string into a map." "Parse parameters from a string into a map."
([^String param-string] ([^String param-string]
@@ -48,9 +38,9 @@
(reduce (reduce
(fn [param-map encoded-param] (fn [param-map encoded-param]
(if-let [[_ key val] (re-matches #"([^=]+)=(.*)" encoded-param)] (if-let [[_ key val] (re-matches #"([^=]+)=(.*)" encoded-param)]
- (assoc-param param-map + (assoc+ param-map
- (url-decode key encoding) + (url-decode key encoding)
- (url-decode (or val "") encoding)) + (url-decode (or val "") encoding))
param-map)) param-map))
{} {}
(string/split param-string #"&")))) (string/split param-string #"&"))))
View
13 ring-core/src/ring/util/data.clj
@@ -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.