1
我使用和clojure.jdbc訪問PostgreSQL數據庫。連字符強調數據庫結果的Clojure映射鍵
數據作爲下劃線地圖返回,例如, {:created_at "some date"}
,當我期望連字符關鍵字如:created-at
。有沒有一種簡單的方法可以將這些地圖重新組裝成複合版本?
我使用和clojure.jdbc訪問PostgreSQL數據庫。連字符強調數據庫結果的Clojure映射鍵
數據作爲下劃線地圖返回,例如, {:created_at "some date"}
,當我期望連字符關鍵字如:created-at
。有沒有一種簡單的方法可以將這些地圖重新組裝成複合版本?
我已使用clojure.walk/postwalk
來完成此操作。
(defn transform-keys
"Recursively transforms all map keys in coll with the transform-key fn."
[transform-key coll]
(letfn [(transform [x] (if (map? x)
(into {} (map (fn [[k v]] [(transform-key k) v]) x))
x))]
(walk/postwalk transform coll)))
第一個參數是一個函數,該函數接受現有密鑰並返回新密鑰。在你的情況下,你可以將關鍵字轉換爲字符串,用連字符代替下劃線,並將其轉換回關鍵字。