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

allow m/-proxy-schema child to be a delay #1090

Merged
merged 2 commits into from
Aug 27, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Malli is in well matured [alpha](README.md#alpha).

* Fix ClojureScript [arithmetic warning](https://github.com/metosin/malli/issues/1093)
* Distribute `:merge` over `:multi` [#1086](https://github.com/metosin/malli/pull/1086), see [documentation](README.md#distributive-schemas)
* allow `m/-proxy-schema` child to be a `delay`

## 0.16.3 (2024-08-05)

Expand Down
45 changes: 23 additions & 22 deletions src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2007,17 +2007,18 @@
(-into-schema [parent properties children options]
(-check-children! type properties children min max)
(let [[children forms schema] (fn properties (vec children) options)
schema (delay (force schema))
form (delay (-create-form type properties forms options))
cache (-create-cache options)]
^{:type ::schema}
(reify
Schema
(-validator [_] (-validator schema))
(-explainer [_ path] (-explainer schema (conj path ::in)))
(-parser [_] (-parser schema))
(-unparser [_] (-unparser schema))
(-validator [_] (-validator @schema))
(-explainer [_ path] (-explainer @schema (conj path ::in)))
(-parser [_] (-parser @schema))
(-unparser [_] (-unparser @schema))
(-transformer [this transformer method options]
(-parent-children-transformer this [schema] transformer method options))
(-parent-children-transformer this [@schema] transformer method options))
(-walk [this walker path options]
(let [children (if childs (subvec children 0 childs) children)]
(when (-accept walker this path options)
Expand All @@ -2031,38 +2032,38 @@
(-cache [_] cache)
LensSchema
(-keep [_])
(-get [_ key default] (if (= ::in key) schema (get children key default)))
(-get [_ key default] (if (= ::in key) @schema (get children key default)))
(-set [_ key value] (into-schema type properties (assoc children key value)))
DistributiveSchema
(-distributive-schema? [_] (-distributive-schema? schema))
(-distribute-to-children [_ f options] (-distribute-to-children schema f options))
FunctionSchema
(-function-schema? [_] (-function-schema? schema))
(-function-info [_] (-function-info schema))
(-function-schema-arities [_] (-function-schema-arities schema))
(-instrument-f [_ props f options] (-instrument-f schema props f options))
(-function-schema? [_] (-function-schema? @schema))
(-function-info [_] (-function-info @schema))
(-function-schema-arities [_] (-function-schema-arities @schema))
(-instrument-f [_ props f options] (-instrument-f @schema props f options))
RegexSchema
(-regex-op? [_] (-regex-op? schema))
(-regex-validator [_] (-regex-validator schema))
(-regex-explainer [_ path] (-regex-explainer schema path))
(-regex-unparser [_] (-regex-unparser schema))
(-regex-parser [_] (-regex-parser schema))
(-regex-transformer [_ transformer method options] (-regex-transformer schema transformer method options))
(-regex-min-max [_ nested?] (-regex-min-max schema nested?))
(-regex-op? [_] (-regex-op? @schema))
(-regex-validator [_] (-regex-validator @schema))
(-regex-explainer [_ path] (-regex-explainer @schema path))
(-regex-unparser [_] (-regex-unparser @schema))
(-regex-parser [_] (-regex-parser @schema))
(-regex-transformer [_ transformer method options] (-regex-transformer @schema transformer method options))
(-regex-min-max [_ nested?] (-regex-min-max @schema nested?))
RefSchema
(-ref [_])
(-deref [_] schema))))))
(-deref [_] @schema))))))

(defn -->-schema
"Experimental simple schema for :=> schema. AST and explain results subject to change."
[_]
(-proxy-schema {:type :->
:fn (fn [{:keys [guard] :as p} c o]
(-check-children! :-> p c 1 nil)
(let [c (mapv #(schema % o) c)
cc (cond-> [(into [:cat] (pop c)) (peek c)]
guard (conj [:fn guard]))]
[c (map -form c) (into-schema :=> (dissoc p :guard) cc o)]))}))
(let [c (mapv #(schema % o) c)]
[c (map -form c) (delay (let [cc (cond-> [(into [:cat] (pop c)) (peek c)]
guard (conj [:fn guard]))]
(into-schema :=> (dissoc p :guard) cc o)))]))}))

(defn- regex-validator [schema] (re/validator (-regex-validator schema)))

Expand Down
4 changes: 2 additions & 2 deletions src/malli/util.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,13 @@
(defn -reducing [f]
(fn [_ [first & rest :as children] options]
(let [children (mapv #(m/schema % options) children)]
[children (mapv m/form children) (reduce #(f %1 %2 options) first rest)])))
[children (mapv m/form children) (delay (reduce #(f %1 %2 options) first rest))])))

(defn -applying [f]
(fn [_ children options]
[(clojure.core/update children 0 #(m/schema % options))
(clojure.core/update children 0 #(m/form % options))
(apply f (conj children options))]))
(delay (apply f (conj children options)))]))

(defn -util-schema [m] (m/-proxy-schema m))

Expand Down