Minor fixes
This commit is contained in:
parent
1ccb368ab4
commit
ff5a948063
|
@ -29,15 +29,44 @@
|
||||||
(reduce
|
(reduce
|
||||||
merge
|
merge
|
||||||
(map
|
(map
|
||||||
#(let [pair (split % #"=")
|
#(let [pair (split % #"=")]
|
||||||
v (try
|
(if (= (count pair) 2)
|
||||||
|
(let
|
||||||
|
[v (try
|
||||||
(read-string (nth pair 1))
|
(read-string (nth pair 1))
|
||||||
(catch Exception _
|
(catch Exception _
|
||||||
(nth pair 1)))
|
(nth pair 1)))
|
||||||
value (if (number? v) v (str v))]
|
value (if (number? v) v (str v))]
|
||||||
(hash-map (keyword (first pair)) value))
|
(hash-map (keyword (first pair)) value))
|
||||||
|
{}))
|
||||||
(split query-string #"\&")))))
|
(split query-string #"\&")))))
|
||||||
|
|
||||||
|
|
||||||
|
(defn massage-params
|
||||||
|
"Sending empty strings, or numbers as strings, to the database often isn't
|
||||||
|
helpful. Massage these `params` to eliminate these problems."
|
||||||
|
[params]
|
||||||
|
(reduce
|
||||||
|
merge
|
||||||
|
{}
|
||||||
|
(map
|
||||||
|
(fn [k]
|
||||||
|
(let [v (params k)
|
||||||
|
vr (if
|
||||||
|
(string? v)
|
||||||
|
(try
|
||||||
|
(read-string v)
|
||||||
|
(catch Exception _ nil)))]
|
||||||
|
(cond
|
||||||
|
(nil? v) {}
|
||||||
|
(= v "") {}
|
||||||
|
(number? vr) {k vr}
|
||||||
|
true
|
||||||
|
{k v})))
|
||||||
|
(keys params))))
|
||||||
|
|
||||||
|
(massage-params {:a "a" :b "1" :c nil})
|
||||||
|
|
||||||
(defn
|
(defn
|
||||||
raw-resolve-template
|
raw-resolve-template
|
||||||
[n]
|
[n]
|
||||||
|
|
|
@ -19,4 +19,8 @@
|
||||||
(let [expected {:name "simon" :id 1}
|
(let [expected {:name "simon" :id 1}
|
||||||
actual (query-string-to-map "id=1&name=simon")]
|
actual (query-string-to-map "id=1&name=simon")]
|
||||||
(is (= expected actual) "One string value, one integer. Order of pairs might be reversed, and that's OK"))
|
(is (= expected actual) "One string value, one integer. Order of pairs might be reversed, and that's OK"))
|
||||||
|
(let [expected {:address_id_expanded "AIRDS"}
|
||||||
|
actual (query-string-to-map "id=&address_id_expanded=AIRDS&sub-address=")]
|
||||||
|
(is (= expected actual) "Yeys with no values should not be included in the map"))
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue