Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema generation for composite specs #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/provisdom/spectomic/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,41 @@
:att [s {}]
:att-and-schema s)))))

(defn merge-opt-keys
"Merges optional keys into requried keys (for specs which are created using `clojure.spec.alpha/keys`) using a spec's form/description"
[fspec]
(let [keymap (into {} (map (fn [pair] (vec pair)) (partition 2 (rest fspec))))]
(->> (cond-> {}
(contains? keymap :opt)
(assoc :req (into (keymap :req []) (keymap :opt)))
(contains? keymap :opt-un)
(assoc :req-un (into (keymap :req-un []) (keymap :opt-un))))
(mapcat identity)
(cons 'clojure.spec.alpha/keys))))

(defn regenerate-spec
"Regenerates a spec with its optional keys merged into required keys (for specs which are created using `clojure.spec.alpha/keys`)"
[spec]
(let [fspec (s/form spec)
spec-keys-fn 'clojure.spec.alpha/keys]
(if (coll? fspec)
(if (= (first fspec) spec-keys-fn)
(merge-opt-keys fspec)
(map
(fn [elem]
(if (coll? elem)
(if (= (first elem) spec-keys-fn)
(merge-opt-keys elem)
elem)
elem))
fspec))
(throw (AssertionError. "Spec should be composite")))))

(defn with-map-keys
"Extract out all the keys of composite spec"
[spec]
(keys (sgen/generate (s/gen (eval (regenerate-spec spec))))))

(defn datomic-schema
([specs] (datomic-schema specs nil))
([specs {:keys [custom-type-resolver]
Expand Down
25 changes: 24 additions & 1 deletion test/provisdom/spectomic/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,27 @@
(deftest datascript-schema-transaction-test
(let [schema (spectomic/datascript-schema test-schema-specs)]
(testing "able to transact Spectomic generated schema to DataScript"
(is (ds/create-conn schema)))))
(is (ds/create-conn schema)))))


(s/def :book/name string?)
(s/def :book/description string?)
(s/def ::book (s/keys :req [:book/name] :opt [:book/description]))

(deftest datomic-schema-composite-spec-test
(let [expected-output [{:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/ident :book/name}
{:db/ident :book/description
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}]
actual-output (spectomic/datomic-schema (spectomic/with-map-keys ::book))]
(is (= actual-output expected-output))))

(deftest datomic-schema-composite-spec-fail-test
(let [expected-output "Spec should be composite"
actual-output (try
(spectomic/datomic-schema (spectomic/with-map-keys :book/name))
(catch AssertionError e (.getMessage e)))]
(is (= actual-output expected-output))))