Skip to content

Commit

Permalink
reorganize core impl code
Browse files Browse the repository at this point in the history
  • Loading branch information
ikitommi committed Aug 30, 2020
1 parent d456606 commit e79f770
Showing 1 changed file with 47 additions and 43 deletions.
90 changes: 47 additions & 43 deletions src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
;; impl
;;

(declare schema schema? into-schema into-schema? eval default-registry -schema-schema -registry)
(declare schema schema? into-schema into-schema? eval default-registry -predicate-schema -schema-schema -registry)

(defn -keyword->string [x]
(if (keyword? x)
Expand Down Expand Up @@ -126,6 +126,52 @@
{:enter (build :enter)
:leave (build :leave)}))

(defn- -properties-and-children [[x :as xs]]
(if ((some-fn map? nil?) x)
[x (rest xs)]
[nil xs]))

(defn- -register-var [registry v]
(let [name (-> v meta :name)
schema (-predicate-schema name @v)]
(-> registry
(assoc name schema)
(assoc @v schema))))

(defn -registry
([] default-registry)
([{:keys [registry]}] (or (mr/registry registry) default-registry)))

(defn- -lookup [?schema options]
(let [registry (-registry options)]
(or (mr/-schema registry ?schema)
(some-> registry (mr/-schema (clojure.core/type ?schema)) (-into-schema nil [?schema] options)))))

(defn- -schema [?schema options]
(or (and (or (schema? ?schema) (into-schema? ?schema)) ?schema)
(-lookup ?schema options)
(-fail! ::invalid-schema {:schema ?schema})))

(defn -into-transformer [x]
(cond
(satisfies? Transformer x) x
(fn? x) (-into-transformer (x))
:else (-fail! ::invalid-transformer {:value x})))

(defn- -property-registry [m options f]
(let [options (assoc options ::allow-invalid-refs true)]
(reduce-kv (fn [acc k v] (assoc acc k (f (schema v options)))) {} m)))

(defn -properties-and-options [properties options f]
(if-let [r (some-> properties :registry)]
(let [options (update options :registry #(mr/composite-registry r (or % (-registry options))))]
[(assoc properties :registry (-property-registry r options f)) options])
[properties options]))

;;
;; Schemas
;;

(defn -leaf-schema [type ->validator-and-children]
^{:type ::into-schema}
(reify IntoSchema
Expand Down Expand Up @@ -310,11 +356,6 @@
([schema properties]
(-into-schema (-entry-schema) properties [schema] (-options schema))))

(defn- -properties-and-children [[x :as xs]]
(if ((some-fn map? nil?) x)
[x (rest xs)]
[nil xs]))

(defn -parse-entry-syntax [children naked-keys options]
(let [-parse (fn [e] (let [[[k ?p ?v] f] (cond
(qualified-keyword? e) (if naked-keys [[e nil e] e])
Expand Down Expand Up @@ -932,43 +973,6 @@
(defn -qualified-symbol-schema [] (-simple-schema {:type :qualified-symbol, :pred qualified-symbol?}))
(defn -uuid-schema [] (-simple-schema {:type :uuid, :pred uuid?}))

(defn- -register-var [registry v]
(let [name (-> v meta :name)
schema (-predicate-schema name @v)]
(-> registry
(assoc name schema)
(assoc @v schema))))

(defn -registry
([] default-registry)
([{:keys [registry]}] (or (mr/registry registry) default-registry)))

(defn- -lookup [?schema options]
(let [registry (-registry options)]
(or (mr/-schema registry ?schema)
(some-> registry (mr/-schema (clojure.core/type ?schema)) (-into-schema nil [?schema] options)))))

(defn- -schema [?schema options]
(or (and (or (schema? ?schema) (into-schema? ?schema)) ?schema)
(-lookup ?schema options)
(-fail! ::invalid-schema {:schema ?schema})))

(defn -into-transformer [x]
(cond
(satisfies? Transformer x) x
(fn? x) (-into-transformer (x))
:else (-fail! ::invalid-transformer {:value x})))

(defn- -property-registry [m options f]
(let [options (assoc options ::allow-invalid-refs true)]
(reduce-kv (fn [acc k v] (assoc acc k (f (schema v options)))) {} m)))

(defn -properties-and-options [properties options f]
(if-let [r (some-> properties :registry)]
(let [options (update options :registry #(mr/composite-registry r (or % (-registry options))))]
[(assoc properties :registry (-property-registry r options f)) options])
[properties options]))

;;
;; public api
;;
Expand Down

0 comments on commit e79f770

Please sign in to comment.