-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add uuid and enum support to malli schema generator #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
(ns donut.dbxray.generate.malli | ||
(:require | ||
[clojure.string :as str] | ||
[camel-snake-kebab.core :as csk] | ||
[inflections.core :as inflections])) | ||
|
||
|
@@ -9,7 +10,8 @@ | |
:clob 'string? | ||
:text 'string? | ||
:varchar 'string? | ||
:timestamp 'inst?}) | ||
:timestamp 'inst? | ||
:uuid 'uuid?}) | ||
|
||
(defn- table-spec-name | ||
[table-name] | ||
|
@@ -22,28 +24,36 @@ | |
(keyword (name table-name) (name column-name))) | ||
|
||
(defn- column-spec | ||
[xray table-name column-name] | ||
(let [{:keys [column-type primary-key? nullable? refers-to]} (get-in xray [table-name :columns column-name])] | ||
[(column-spec-name table-name column-name) | ||
{:optional? (boolean nullable?)} | ||
([xray table-name column-name] | ||
(column-spec nil xray table-name column-name)) | ||
|
||
(cond | ||
refers-to | ||
(last (column-spec xray (first refers-to) (second refers-to))) | ||
([enums xray table-name column-name] | ||
(let [{:keys [column-type primary-key? nullable? refers-to]} (get-in xray [table-name :columns column-name])] | ||
[(column-spec-name table-name column-name) | ||
{:optional? (boolean nullable?)} | ||
|
||
(and (= :integer column-type) primary-key?) | ||
(:integer-pk column-types) | ||
(cond | ||
refers-to | ||
(last (column-spec xray (first refers-to) (second refers-to))) | ||
|
||
:else | ||
(column-type column-types [:TODO/column-type-not-recognized column-type]))])) | ||
(and (= :integer column-type) primary-key?) | ||
(:integer-pk column-types) | ||
|
||
;; :enum type definition | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Main change for I'm assuming here that types will always be identifyable by the pattern There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah this doesn't seem like the best way to identify enums, but I currently don't have any better ideas either. Maybe add a comment here to make it clear that we are saying any column type with a |
||
(str/includes? column-type ".") | ||
(let [column (-> column-type (name) (str/replace #"\"" ""))] | ||
(get enums column)) | ||
|
||
:else | ||
(column-type column-types [:TODO/column-type-not-recognized column-type]))]))) | ||
|
||
(defn generate | ||
[{:keys [tables table-order]}] | ||
[{:keys [enums tables table-order]}] | ||
(->> table-order | ||
(mapv (fn [table-name] | ||
(let [{:keys [column-order]} (table-name tables)] | ||
(list 'def | ||
(table-spec-name table-name) | ||
(->> column-order | ||
(map #(column-spec tables table-name %)) | ||
(map #(column-spec enums tables table-name %)) | ||
(into [:map])))))))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the one-liner for
uuid
support