Allow freeze, thaw opts to override bindings
This commit is contained in:
parent
57eae96c7b
commit
b6c1c09419
1 changed files with 140 additions and 105 deletions
|
|
@ -994,14 +994,46 @@
|
||||||
(with-cache (-freeze-with-meta! x dos))
|
(with-cache (-freeze-with-meta! x dos))
|
||||||
(.toByteArray baos)))
|
(.toByteArray baos)))
|
||||||
|
|
||||||
|
(defn- call-with-bindings
|
||||||
|
"Allow opts to override config bindings. Undocumented."
|
||||||
|
[opts f]
|
||||||
|
(let [opt->bindings
|
||||||
|
(fn [bindings id var]
|
||||||
|
(if-let [o (find opts id)]
|
||||||
|
(assoc bindings var (val o))
|
||||||
|
(do bindings)))
|
||||||
|
|
||||||
|
bindings
|
||||||
|
(-> nil
|
||||||
|
(opt->bindings :freeze-fallback #'*freeze-fallback*)
|
||||||
|
(opt->bindings :auto-freeze-compressor #'*auto-freeze-compressor*)
|
||||||
|
(opt->bindings :custom-readers #'*custom-readers*))]
|
||||||
|
|
||||||
|
(if-not bindings
|
||||||
|
(f) ; Common case
|
||||||
|
(try
|
||||||
|
(push-thread-bindings bindings)
|
||||||
|
(f)
|
||||||
|
(finally
|
||||||
|
(pop-thread-bindings))))))
|
||||||
|
|
||||||
|
(comment
|
||||||
|
(enc/qb 1e4
|
||||||
|
(call-with-bindings {} (fn [] *freeze-fallback*))
|
||||||
|
(call-with-bindings {:freeze-fallback "foo"} (fn [] *freeze-fallback*))))
|
||||||
|
|
||||||
(defn freeze
|
(defn freeze
|
||||||
"Serializes arg (any Clojure data type) to a byte array. To freeze custom
|
"Serializes arg (any Clojure data type) to a byte array. To freeze custom
|
||||||
types, extend the Clojure reader or see `extend-freeze`."
|
types, extend the Clojure reader or see `extend-freeze`."
|
||||||
([x] (freeze x nil))
|
([x] (freeze x nil))
|
||||||
([x {:keys [compressor encryptor password]
|
([x {:as opts
|
||||||
|
:keys [compressor encryptor password]
|
||||||
:or {compressor :auto
|
:or {compressor :auto
|
||||||
encryptor aes128-gcm-encryptor}
|
encryptor aes128-gcm-encryptor}}]
|
||||||
:as opts}]
|
|
||||||
|
(call-with-bindings opts
|
||||||
|
(fn []
|
||||||
|
|
||||||
(let [;; Intentionally undocumented:
|
(let [;; Intentionally undocumented:
|
||||||
no-header? (or (get opts :no-header?)
|
no-header? (or (get opts :no-header?)
|
||||||
(get opts :skip-header?))
|
(get opts :skip-header?))
|
||||||
|
|
@ -1052,7 +1084,7 @@
|
||||||
(when-let [e encryptor]
|
(when-let [e encryptor]
|
||||||
(or (encryption/standard-header-ids
|
(or (encryption/standard-header-ids
|
||||||
(encryption/header-id e))
|
(encryption/header-id e))
|
||||||
:else))}))))))))
|
:else))}))))))))))
|
||||||
|
|
||||||
;;;; Thawing
|
;;;; Thawing
|
||||||
|
|
||||||
|
|
@ -1388,14 +1420,17 @@
|
||||||
|
|
||||||
([ba] (thaw ba nil))
|
([ba] (thaw ba nil))
|
||||||
([^bytes ba
|
([^bytes ba
|
||||||
{:keys [v1-compatibility? compressor encryptor password]
|
{:as opts
|
||||||
|
:keys [v1-compatibility? compressor encryptor password]
|
||||||
:or {compressor :auto
|
:or {compressor :auto
|
||||||
encryptor :auto}
|
encryptor :auto}}]
|
||||||
:as opts}]
|
|
||||||
|
|
||||||
(assert (not (get opts :headerless-meta))
|
(assert (not (get opts :headerless-meta))
|
||||||
":headerless-meta `thaw` opt removed in Nippy v2.7+")
|
":headerless-meta `thaw` opt removed in Nippy v2.7+")
|
||||||
|
|
||||||
|
(call-with-bindings opts
|
||||||
|
(fn []
|
||||||
|
|
||||||
(let [v2+? (not v1-compatibility?)
|
(let [v2+? (not v1-compatibility?)
|
||||||
no-header? (get opts :no-header?) ; Intentionally undocumented
|
no-header? (get opts :no-header?) ; Intentionally undocumented
|
||||||
ex (fn ex
|
ex (fn ex
|
||||||
|
|
@ -1461,7 +1496,7 @@
|
||||||
;; Well-formed header definitely not present
|
;; Well-formed header definitely not present
|
||||||
(if v2+?
|
(if v2+?
|
||||||
(ex err-msg-unknown-thaw-failure)
|
(ex err-msg-unknown-thaw-failure)
|
||||||
(thaw-v1-data ba (fn [_] (ex err-msg-unknown-thaw-failure)))))))))
|
(thaw-v1-data ba (fn [_] (ex err-msg-unknown-thaw-failure)))))))))))
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
(thaw (freeze "hello"))
|
(thaw (freeze "hello"))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue