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

Pass function metadata to malli.core/-instrument #680

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
13 changes: 9 additions & 4 deletions src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2425,7 +2425,7 @@
(-instrument props nil nil))
([props f]
(-instrument props f nil))
([{:keys [scope report gen] :or {scope #{:input :output}, report -fail!} :as props} f options]
([{:keys [scope report gen] :or {scope #{:input :output}, report -fail!} :as props} f options & {:keys [original-fn-meta]}]
(let [schema (-> props :schema (schema options))]
(case (type schema)
:=> (let [{:keys [min max input output]} (-function-info schema)
Expand All @@ -2436,13 +2436,18 @@
(let [args (vec args), arity (count args)]
(when wrap-input
(when-not (<= min arity (or max miu/+max-size+))
(report ::invalid-arity {:arity arity, :arities #{{:min min :max max}}, :args args, :input input, :schema schema}))
(report ::invalid-arity
{:arity arity, :arities #{{:min min :max max}}, :args args,
:input input, :schema schema, :original-fn-meta original-fn-meta}))
(when-not (validate-input args)
(report ::invalid-input {:input input, :args args, :schema schema})))
(report ::invalid-input
{:input input, :args args, :schema schema, :original-fn-meta original-fn-meta})))
(let [value (apply f args)]
(when wrap-output
(when-not (validate-output value)
(report ::invalid-output {:output output, :value value, :args args, :schema schema})))
(report ::invalid-output
{:output output, :value value, :args args, :schema schema,
:original-fn-meta original-fn-meta})))
value))))
:function (let [arity->info (->> (children schema)
(map (fn [s] (assoc (-function-info s) :f (-instrument (assoc props :schema s) f options))))
Expand Down
2 changes: 1 addition & 1 deletion src/malli/instrument.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
(true? (:gen d)) (dissoc $ :gen)
:else $))]
(alter-meta! v assoc ::original-fn original-fn)
(alter-var-root v (constantly (m/-instrument dgen original-fn)))
(alter-var-root v (constantly (m/-instrument dgen original-fn nil :original-fn-meta (meta v))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we discussed this briefly with @ikitommi and we'd prefer if you passed in :meta (meta v) as part of dgen, the first argument of -instrument

(println "..instrumented" v))
:unstrument (when-let [original-fn (-original v)]
(alter-meta! v dissoc ::original-fn)
Expand Down