Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
frenchy64 committed Dec 19, 2023
1 parent de2ab0b commit 326b018
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
[metosin/schema-tools "0.12.2"]]
:global-vars {*warn-on-reflection* true}
:profiles {:dev
{:dependencies [[org.clojure/test.check "1.0.0"]
{:dependencies [[org.clojure/test.check "1.1.1"]
[metosin/malli "0.13.0"]]}})
69 changes: 36 additions & 33 deletions src/flanders/malli.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
SetOfType SignatureType StringType]))

(defprotocol MalliNode
(->malli' [node f]))

(defn ->malli [node opts]
(->malli' node #(->malli % opts)))
(->malli' [node opts]))

(defn- describe [schema description]
schema)
Expand All @@ -27,16 +24,18 @@
;; Branches

EitherType
(->malli' [{:keys [choices tests]} f]
(->malli' [{:keys [choices tests]} opts]
;;TODO `choices` allows dispatch like :multi
(let [choice-schemas (map f choices)]
(let [f #(->malli' % opts)
choice-schemas (map f choices)]
(into [:or] choice-schemas)))

MapEntry
(->malli' [{:keys [key type required?] :as entry} f]
(->malli' [{:keys [key type required?] :as entry} opts]
(assert (some? type) (str "Type nil for MapEntry with key " key))
(assert (some? key) (str "Key nil for MapEntry with type " type))
(let [optional? (and (not required?)
(let [f #(->malli' % opts)
optional? (and (not required?)
(not (:open? key))
(seq (:values key)))
description (some :description [key entry])
Expand All @@ -48,26 +47,31 @@
(conj (f type)))))

MapType
(->malli' [{:keys [description entries]} f]
(describe
(into [:merge] (map (fn [e] [:map (f e)])) entries)
description))
(->malli' [{:keys [description entries]} opts]
(let [f #(->malli' % opts)]
(describe
(into [:merge] (map (fn [e] [:map (f e)])) entries)
description)))

ParameterListType
(->malli' [{:keys [parameters]} f]
(into [:cat] (map f) parameters))
(->malli' [{:keys [parameters]} opts]
(let [f #(->malli' % opts)]
(into [:cat] (map f) parameters)))

SequenceOfType
(->malli' [{:keys [type]} f]
[:sequential (f type)])
(->malli' [{:keys [type]} opts]
(let [f #(->malli' % opts)]
[:sequential (f type)]))

SetOfType
(->malli' [{:keys [type]} f]
[:set (f type)])
(->malli' [{:keys [type]} opts]
(let [f #(->malli' % opts)]
[:set (f type)]))

SignatureType
(->malli' [{:keys [parameters rest-parameter return name]} f]
(let [parameters (f parameters)
(->malli' [{:keys [parameters rest-parameter return name]} opts]
(let [f #(->malli' % opts)
parameters (f parameters)
parameters (if rest-parameter
[:cat parameters [:* (f rest-parameter)]]
parameters)]
Expand All @@ -76,25 +80,25 @@
;; Leaves

AnythingType
(->malli' [{:keys [description]} _]
(->malli' [{:keys [description]} opts]
(describe
:any
description))

BooleanType
(->malli' [{:keys [open? default description]} _]
(->malli' [{:keys [open? default description]} opts]
(describe
(match [open? default]
[true _] :boolean
[_ d] [:enum d])
description))

InstType
(->malli' [{:keys [description]} _]
(->malli' [{:keys [description]} opts]
(describe inst? description))

IntegerType
(->malli' [{:keys [description open? values]} _]
(->malli' [{:keys [description open? values]} opts]
(describe
(match [open? (seq values)]
[true _ ] :int
Expand All @@ -103,7 +107,7 @@
description))

KeywordType
(->malli' [{:keys [description key? open? values] :as node} _]
(->malli' [{:keys [description key? open? values] :as node} opts]
(let [kw-schema
(match [key? open? (seq values)]
[_ true _ ] :keyword
Expand All @@ -115,7 +119,7 @@
(describe kw-schema description))))

NumberType
(->malli' [{:keys [description open? values]} _]
(->malli' [{:keys [description open? values]} opts]
(describe
(match [open? (seq values)]
[true _ ] number?
Expand All @@ -124,7 +128,7 @@
description))

StringType
(->malli' [{:keys [description open? values]} _]
(->malli' [{:keys [description open? values]} opts]
(describe
(match [open? (seq values)]
[true _ ] :string
Expand All @@ -134,12 +138,11 @@

(def ^:private registry (merge (m/default-schemas) (mu/schemas)))

;;FIXME merge with ->malli
(defn ctim->malli
(defn ->malli
"Convert a ctim schema to malli. Malli opts must contain a registry
with support for :merge (usually via malli.util/schemas)."
([ctim-schema] (ctim->malli ctim-schema {:registry registry}))
([ctim-schema malli-opts]
([ctim-schema] (->malli ctim-schema {:registry registry}))
([ctim-schema opts]
(-> ctim-schema
(flanders/->malli {:registry malli-opts})
(m/schema malli-opts))))
(->malli' opts)
(m/schema opts))))

0 comments on commit 326b018

Please sign in to comment.