Last active
December 12, 2015 10:28
-
-
Save hozumi/4758987 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn entity->map [convert-data entity] | |
(reduce (fn [m attr] | |
(if (keyword? attr) | |
(if-let [v (attr entity)] | |
(assoc m attr v) | |
m) | |
(let [[attr conv-fn] attr] | |
(if-let [v (attr entity)] | |
(assoc m attr (conv-fn v)) | |
m)))) | |
{} | |
convert-data)) | |
;; example | |
(def book-convert-data | |
[:book/title | |
:book/created_at | |
[:book/created_by :author/id] | |
[:book/comments (fn [comments] (map :comment/text comments))]]) | |
(entity->map book-convert-data a-book-entity) | |
; => {:book/title "a book" | |
; :book/created_at 1360418787603 | |
; :book/created_by "john" | |
; :book/comments ("good!", "bad!")} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment