Commits

Meikel Brandmeyer committed 09084f0

Add converter helpers

  • Participants
  • Parent commits 982b11f

Comments (0)

Files changed (1)

File src/main/clojure/aspect/core.clj

   (->> (keys converter)
     (map #(vector % (transform (get value %) (get converter %))))
     (into value)))
+
+(defmacro defconversion
+  "Define a conversion for the given converter and the given
+  source type. The conversions is a function specialised with
+  optional parameters and returned by fntail."
+  [converter source-type & fntail]
+  (let [source-typeg (gensym "source-type__")
+        add-type     (fn [[args & body]]
+                       (cons (into [source-typeg] args) body))]
+    `(defmethod ~converter ~source-type
+       ~@(if (seq? (first fntail))
+           (map add-type fntail)
+           (add-type fntail)))))
+
+(defmacro defconverter
+  "Define a new converter. Possibly with the given set of
+  conversions already set up."
+  [converter & initial-conversions]
+  `(do
+     (defmulti ~converter
+       (fn [source-type# & _options#] source-type#))
+     ~@(for [[source-type & fntail] initial-conversions]
+         `(defconversion ~converter ~source-type ~@fntail))))