Skip to content

Commit

Permalink
Merge pull request #842 from gpind/gpind/println
Browse files Browse the repository at this point in the history
Make `instrument!` less verbose
  • Loading branch information
ikitommi authored Aug 8, 2023
2 parents f49a9c6 + c760756 commit d72ab6c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
24 changes: 16 additions & 8 deletions src/malli/dev.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"Stops instrumentation for all functions vars and removes clj-kondo type annotations."
[]
(remove-watch @#'m/-function-schemas* ::watch)
(mi/unstrument!)
(->> (mi/unstrument!)
count
(format "unstrumented %d vars")
println)
(clj-kondo/save! {})
(println "stopped instrumentation"))

Expand All @@ -22,13 +25,18 @@
(with-out-str (stop!))
(mi/collect! {:ns (all-ns)})
(let [watch (fn [_ _ old new]
(mi/instrument! (assoc options :data (->> (for [[n d] (:clj new)
:let [no (get-in old [:clj n])]
[s d] d
:when (not= d (get no s))]
[[n s] d])
(into {})
(reduce-kv assoc-in {}))))
(->> (for [[n d] (:clj new)
:let [no (get-in old [:clj n])]
[s d] d
:when (not= d (get no s))]
[[n s] d])
(into {})
(reduce-kv assoc-in {})
(assoc options :data)
mi/instrument!
count
(format "instrumented %d vars")
println)
(clj-kondo/emit!))]
(add-watch @#'m/-function-schemas* ::watch watch))
(mi/instrument! options)
Expand Down
36 changes: 18 additions & 18 deletions src/malli/instrument.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@
(defn -strument!
([] (-strument! nil))
([{:keys [mode data filters gen report] :or {mode :instrument, data (m/function-schemas)} :as options}]
(doseq [[n d] data, [s d] d]
(when (or (not filters) (some #(% n s d) filters))
(when-let [v (-find-var n s)]
(case mode
:instrument (let [original-fn (or (-original v) (deref v))
dgen (as-> (merge (select-keys options [:scope :report :gen]) d) $
(cond-> $ report (update :report (fn [r] (fn [t data] (r t (assoc data :fn-name (symbol (name n) (name s))))))))
(cond (and gen (true? (:gen d))) (assoc $ :gen gen)
(true? (:gen d)) (dissoc $ :gen)
:else $))]
(alter-meta! v assoc ::original-fn original-fn)
(alter-var-root v (constantly (m/-instrument dgen original-fn)))
(println "..instrumented" v))
:unstrument (when-let [original-fn (-original v)]
(alter-meta! v dissoc ::original-fn)
(alter-var-root v (constantly original-fn))
(println "..unstrumented" v))
(mode v d)))))))
(doall
(for [[n d] data, [s d] d]
(when (or (not filters) (some #(% n s d) filters))
(when-let [v (-find-var n s)]
(case mode
:instrument (let [original-fn (or (-original v) (deref v))
dgen (as-> (merge (select-keys options [:scope :report :gen]) d) $
(cond-> $ report (update :report (fn [r] (fn [t data] (r t (assoc data :fn-name (symbol (name n) (name s))))))))
(cond (and gen (true? (:gen d))) (assoc $ :gen gen)
(true? (:gen d)) (dissoc $ :gen)
:else $))]
(alter-meta! v assoc ::original-fn original-fn)
(alter-var-root v (constantly (m/-instrument dgen original-fn))))
:unstrument (when-let [original-fn (-original v)]
(alter-meta! v dissoc ::original-fn)
(alter-var-root v (constantly original-fn)))
(mode v d))
v))))))

(defn -schema [v]
(let [{:keys [malli/schema arglists]} (meta v)]
Expand Down

0 comments on commit d72ab6c

Please sign in to comment.