Created
March 27, 2023 07:02
-
-
Save opqdonut/9a197baa671e042b6e38821be0093705 to your computer and use it in GitHub Desktop.
unparsing in clojure
This file contains hidden or 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
(declare unparse) | |
(defn unparse-args [lang args] | |
(cond | |
(map? (first args)) | |
(update-vals (first args) (partial unparse lang)) | |
:else | |
(mapv (partial unparse lang) args))) | |
(defn unparse [lang data] | |
(if-not (coll? data) | |
data | |
(let [[prim & args ] data | |
unparse-fn (get lang prim)] | |
(assert unparse-fn (pr-str [prim args])) | |
(unparse-fn (unparse-args lang args))))) | |
(defn binary-operation [op] | |
(fn [[a b]] | |
(str a " " op " " b))) | |
(def lang | |
{:select #(str "SELECT * FROM " | |
(:table %) | |
" WHERE " | |
(:where %)) | |
:and (binary-operation "AND") | |
:= (binary-operation "=")}) | |
#_ | |
(unparse lang | |
[:select {:table "moi" | |
:where [:and [:= "a" 1] [:= "b" 2]]}]) | |
;; => "SELECT * FROM moi WHERE a = 1 AND b = 2" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment