diff --git a/src/adl_support/core.clj b/src/adl_support/core.clj index 0a5df51..e4a18cc 100644 --- a/src/adl_support/core.clj +++ b/src/adl_support/core.clj @@ -29,15 +29,44 @@ (reduce merge (map - #(let [pair (split % #"=") - v (try - (read-string (nth pair 1)) - (catch Exception _ - (nth pair 1))) - value (if (number? v) v (str v))] - (hash-map (keyword (first pair)) value)) + #(let [pair (split % #"=")] + (if (= (count pair) 2) + (let + [v (try + (read-string (nth pair 1)) + (catch Exception _ + (nth pair 1))) + value (if (number? v) v (str v))] + (hash-map (keyword (first pair)) value)) + {})) (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 raw-resolve-template [n] diff --git a/test/adl_support/core_test.clj b/test/adl_support/core_test.clj index 3f5f040..073b364 100644 --- a/test/adl_support/core_test.clj +++ b/test/adl_support/core_test.clj @@ -19,4 +19,8 @@ (let [expected {:name "simon" :id 1} 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")) + (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")) )) +