[new] Refactor tools ns, embed dynamic *freeze-opts* in wrappers

This commit is contained in:
Peter Taoussanis 2022-11-02 15:24:42 +01:00
parent 064015e874
commit 3ac06b6c87

View file

@ -1,60 +1,74 @@
(ns taoensso.nippy.tools (ns taoensso.nippy.tools
"Utils for 3rd-party tools that want to add user-configurable Nippy support. "Utils for community tools that want to add user-configurable Nippy support.
Used by Carmine, Faraday, etc." Used by Carmine, Faraday, etc."
(:require [taoensso.nippy :as nippy])) (:require [taoensso.nippy :as nippy]))
(def ^:dynamic *freeze-opts* nil) (def ^:dynamic *freeze-opts* nil)
(def ^:dynamic *thaw-opts* nil) (def ^:dynamic *thaw-opts* nil)
(defmacro with-freeze-opts [opts & body] `(binding [*freeze-opts* ~opts] ~@body)) (defn ^:no-doc -merge-opts
(defmacro with-thaw-opts [opts & body] `(binding [*thaw-opts* ~opts] ~@body)) "Private, implementation detail."
([x y ] (if x (conj x y) y))
([x y z] (-merge-opts (-merge-opts x y) z)))
(do
(defmacro with-freeze-opts [opts & body] `(binding [*freeze-opts* ~opts ] ~@body))
(defmacro with-freeze-opts+ [opts & body] `(binding [*freeze-opts* (-merge-opts *freeze-opts* ~opts)] ~@body))
(defmacro with-thaw-opts [opts & body] `(binding [*thaw-opts* ~opts ] ~@body))
(defmacro with-thaw-opts+ [opts & body] `(binding [*thaw-opts* (-merge-opts *thaw-opts* ~opts)] ~@body)))
(deftype WrappedForFreezing [val opts]) (deftype WrappedForFreezing [val opts])
(defn wrapped-for-freezing? [x] (instance? WrappedForFreezing x)) (defn wrapped-for-freezing? [x] (instance? WrappedForFreezing x))
(defn wrap-for-freezing
"Ensures that given arg (any freezable data type) is wrapped so that
(tools/freeze <wrapped-arg>) will serialize as
(nippy/freeze <unwrapped-arg> <opts>).
See also `nippy.tools/freeze`, `nippy.tools/thaw`." (let [-merge-opts -merge-opts]
([x ] (wrap-for-freezing x nil)) (defn wrap-for-freezing
([x opts] "Captures (merge `tools/*thaw-opts*` `wrap-opts`), and returns
(if (instance? WrappedForFreezing x) the given argument in a wrapped form so that `tools/freeze` will
(let [^WrappedForFreezing x x] use the captured options when freezing the wrapper argument.
(if (= (.-opts x) opts)
x
(WrappedForFreezing. (.-val x) opts)))
(WrappedForFreezing. x opts))))
(defn freeze See also `tools/freeze`."
"Like `nippy/freeze` but uses as opts the following merged in order of ([x ] (wrap-for-freezing x nil))
ascending preference: ([x wrap-opts]
(let [captured-opts (-merge-opts *freeze-opts* wrap-opts)] ; wrap > dynamic
(if (instance? WrappedForFreezing x)
(let [^WrappedForFreezing x x]
(if (= (.-opts x) captured-opts)
x
(WrappedForFreezing. (.-val x) captured-opts)))
(WrappedForFreezing. x captured-opts))))))
- Optional `default-opts` arg given to this fn (default nil). (let [-merge-opts -merge-opts]
- Optional `*freeze-opts*` dynamic value (default nil). (defn freeze
- Optional opts provided to `wrap-for-freezing` (default nil)." "Like `nippy/freeze` but uses as options the following, merged in
order of ascending preference:
([x ] (freeze x nil)) 1. `default-opts` given to this fn (default nil).
([x default-opts] 2. `tools/*freeze-opts*` dynamic value (default nil).
(let [default-opts (get default-opts :default-opts default-opts) ; For back compatibility 3. Opts captured by `tools/wrap-for-freezing` (default nil).
merged-opts (conj (or default-opts {}) *freeze-opts*)]
(if (instance? WrappedForFreezing x) See also `tools/wrap-for-freezing`."
(let [^WrappedForFreezing x x] ([x ] (freeze x nil))
(nippy/freeze (.-val x) (conj merged-opts (.-opts x)))) ([x default-opts]
(nippy/freeze x merged-opts))))) (let [default-opts (get default-opts :default-opts default-opts) ; Back compatibility
active-opts (-merge-opts default-opts *freeze-opts*)] ; dynamic > default
(defn thaw (if (instance? WrappedForFreezing x)
"Like `nippy/thaw` but uses as opts the following merged in order of (let [^WrappedForFreezing x x]
ascending preference: (nippy/freeze (.-val x) (-merge-opts active-opts (.-opts x)))) ; captured > active!
(nippy/freeze x active-opts))))))
- Optional `default-opts` arg given to this fn (default nil). (let [-merge-opts -merge-opts]
- Optional `*thaw-opts*` dynamic value (default nil)." (defn thaw
"Like `nippy/thaw` but uses as options the following, merged in
order of ascending preference:
([ba ] (thaw ba nil)) 1. `default-opts` given to this fn (default nil).
([ba default-opts] 2. `tools/*thaw-opts*` dynamic value (default nil)."
(let [default-opts (get default-opts :default-opts default-opts) ; For back compatibility ([ba ] (thaw ba nil))
merged-opts (conj (or default-opts {}) *thaw-opts*)] ([ba default-opts]
(nippy/thaw ba merged-opts)))) (let [default-opts (get default-opts :default-opts default-opts) ; Back compatibility
active-opts (-merge-opts default-opts *thaw-opts*)] ; dynamic > default
(nippy/thaw ba active-opts)))))
(comment (thaw (freeze (wrap-for-freezing "wrapped")))) (comment (thaw (freeze (wrap-for-freezing "wrapped"))))