Created
March 7, 2018 00:28
-
-
Save olivergeorge/a3caad8d013bee026e27d6151ab8edde to your computer and use it in GitHub Desktop.
This file contains 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 olivergeorge.schema | |
"generate datomic schema from simple spec forms" | |
(:require [clojure.spec.alpha :as s])) | |
(s/def ::schema-type | |
(s/or :db.type/string #{'string?} | |
:db.type/long #{'int? 'pos-int? 'neg-int? 'nat-int?} | |
:db.type/boolean #{'boolean?} | |
:db.type/float #{'float?} | |
:db.type/double #{'double?} | |
:db.type/instant #{'inst?} | |
:db.type/uuid #{'uuid?} | |
:db.type/url #{'uri?} | |
:db.type/ref keyword?)) | |
(s/def ::schema-form | |
(s/or :many (s/cat :m #{'coll-of} :t ::schema-type) | |
:one ::schema-type)) | |
(defn spec->schema | |
[ident] | |
(assert (s/form ident)) | |
(let [s (s/get-spec ident) | |
f (s/abbrev (s/form s)) | |
c (s/conform ::schema-form f)] | |
(if-not (= c ::s/invalid) | |
(case (first c) | |
:many (let [{:keys [t]} (second c)] | |
{:db/ident ident | |
:db/valueType (first t) | |
:db/cardinality :db.cardinality/many}) | |
:one (let [t (second c)] | |
{:db/ident ident | |
:db/valueType (first t) | |
:db/cardinality :db.cardinality/one})) | |
(throw (ex-info "Unable to confirm spec" (s/explain-data ::schema-form f)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment