Skip to content
This repository has been archived by the owner on Jan 30, 2019. It is now read-only.

Support nested query params #28

Open
piotr-yuxuan opened this issue Sep 13, 2017 · 2 comments
Open

Support nested query params #28

piotr-yuxuan opened this issue Sep 13, 2017 · 2 comments

Comments

@piotr-yuxuan
Copy link

Hello,

Thank you for that free software :-) I've noticed it doesn't support well nester parameters. However it's not that difficult. Would you accept a PR with a code like this?

(defn- kv-encoder-fn
  "Convert a [key value] pair into an \"k=v\" encoded string. Only recurs over maps, vectors and lists for the value."
  [map->query-string path]
  (fn [[key value]]
    (let [[path-head & path-tail :as nested-path] (map #(cond (keyword? %) (name %)
                                                              :else %)
                                                       (conj (vec path) key))]
      (cond (map? value) (map->query-string nested-path value)
            (sequential? value) (map->query-string nested-path (into {} (map-indexed vector (vec value))))
            :else (let [encoded-key (url-encode (str path-head (str/join (map #(str "[" % "]") path-tail))))
                        encoded-value (url-encode value)]
                    (str encoded-key "=" encoded-value))))))

(defn map->query-string
  "Convert a nested map of parameters into a query string. Only recurs over maps, vectors and lists."
  ([input]
   (map->query-string [] input))
  ([path input]
   (->> input
        sort
        (map (kv-encoder-fn map->query-string path))
        (str/join "&"))))
@piotr-yuxuan
Copy link
Author

?

@nenadalm
Copy link

Hi. As this project seems to be no longer active, I sent pr into one of the forks: arohner#2

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants