Skip to content

Instantly share code, notes, and snippets.

@hozumi
Last active December 12, 2015 10:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hozumi/4758987 to your computer and use it in GitHub Desktop.
Save hozumi/4758987 to your computer and use it in GitHub Desktop.
(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