Skip to content

Instantly share code, notes, and snippets.

@olivergeorge
Created March 7, 2018 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olivergeorge/a3caad8d013bee026e27d6151ab8edde to your computer and use it in GitHub Desktop.
Save olivergeorge/a3caad8d013bee026e27d6151ab8edde to your computer and use it in GitHub Desktop.
(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