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

Support nested query params #28

@piotr-yuxuan

Description

@piotr-yuxuan

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 "&"))))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions