Created
December 10, 2018 23:10
-
-
Save cddr/d70423a270e9675d535b22fbf643f81b to your computer and use it in GitHub Desktop.
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
(ns cddr.avro2ddl | |
(:require | |
[clojure.string :as string] | |
[clojure.java.io :as io] | |
[clojure.data.json :as json]) | |
(:gen-class)) | |
(def home (System/getProperty "user.home")) | |
(def loan (-> (format "%s/Projects/avro-schemas/schemas/loan-3.json" | |
home) | |
slurp | |
json/read-str)) | |
(defn optional? | |
[t] | |
(and (vector? t) | |
(= "null" (first t)))) | |
(defn enum? | |
[t] | |
(and (map? t) | |
(= "enum" (get t "type")))) | |
(def type-map | |
{"string" "text" | |
"double" "double precision" | |
"long" "integer" | |
"int" "integer"}) | |
(defn supported? | |
[t] | |
(contains? #{"string" "long" "int"} t)) | |
(defn column-for | |
[{:strs [name type default]}] | |
(cond | |
(type-map type) {:name name | |
:type (get type-map type)} | |
(optional? type) {:name name | |
:type (second type) | |
:nullable? true | |
:default default} | |
(enum? type) {:name name | |
:type "text"})) | |
(defn table-for | |
[{:strs [type name fields]}] | |
{:name name | |
:columns (map column-for fields)}) | |
(defn format-column | |
[column] | |
(with-out-str | |
(print " ") | |
(pr (:name column)) | |
(print (format " %s" (:type column))) | |
(when-not (:nullable? column) | |
(print " not null")) | |
(when-let [default (:default column)] | |
(print (format " default %s" default))))) | |
(defn format-table | |
[table] | |
(format " | |
create table %s ( | |
%s | |
); | |
" | |
(:name table) | |
(string/join ",\n" (map format-column (:columns table))))) | |
(-> (table-for loan) | |
format-table | |
print) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment