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

[with-temp] Delay pprint in do-with-temp #197

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 13 additions & 3 deletions src/toucan2/tools/with_temp.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

(comment types/keep-me)

(set! *warn-on-reflection* true)

(m/defmulti with-temp-defaults
{:arglists '([model])
:defmethod-arities #{1}
Expand Down Expand Up @@ -46,6 +48,13 @@
:dispatch-value-spec (s/nonconforming ::types/dispatch-value.model)}
u/dispatch-on-first-arg)

(defn delayed-string
"Return a CharSequence that only executes `(f)` when toString is called. Caches the result."
[f]
(let [d (delay (f))]
(reify CharSequence
(toString [_] @d))))

(m/defmethod do-with-temp* :default
[model explicit-attributes f]
(assert (some? model) (format "%s model cannot be nil." `with-temp))
Expand All @@ -61,9 +70,10 @@
(let [temp-object (first (insert/insert-returning-instances! model merged-attributes))]
(log/debugf "[with-temp] => %s" temp-object)
(try
(t/testing (format "\nwith temporary %s with attributes\n%s\n"
(pr-str model)
(with-out-str (pprint/pprint merged-attributes)))
(t/testing (delayed-string
#(format "\nwith temporary %s with attributes\n%s\n"
(pr-str model)
(with-out-str (pprint/pprint merged-attributes))))
(f temp-object))
(finally
(delete/delete! model :toucan/pk ((model/select-pks-fn model) temp-object))))))))
Expand Down