Skip to content

Instantly share code, notes, and snippets.

@codonnell
Created June 20, 2018 00:46
Show Gist options
  • Save codonnell/f15bd9cc324b06f5b2bf07c02f391976 to your computer and use it in GitHub Desktop.
Save codonnell/f15bd9cc324b06f5b2bf07c02f391976 to your computer and use it in GitHub Desktop.
Working but not DRY macro
(defn wrapped-query [from-db to-db query]
(fn [db m]
(->> m to-db (query db) from-db)))
(defn def-wrapped-query* [from-db to-db query]
(let [{:keys [result] :as query-meta} (meta (resolve query))]
(case result
(:1 :one) `(def ~query ~(with-meta
(wrapped-query @(resolve from-db) @(resolve to-db) @(resolve query))
query-meta))
(:* :many) `(def ~query ~(with-meta
(wrapped-query #(mapv @(resolve from-db) %) @(resolve to-db) @(resolve query))
query-meta)))))
(defmacro def-wrapped-queries
"Redefines each query var in queries to be
* (comp from-db query to-db) if the query returns a single result
* (comp #(mapv from-db %) query to-db) if the query returns multiple results
Preserves the query var's metadata and uses it to infer the result type."
[{:keys [from-db to-db queries]
:or {from-db identity
to-db identity}}]
`(do
~@(map #(def-wrapped-query* from-db to-db %) queries)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment