[fix] [#143] Don't freeze meta info for types that don't support with-meta

This commit is contained in:
Peter Taoussanis 2023-07-27 16:02:43 +02:00
parent aba153e086
commit fa1cc66bf3
2 changed files with 10 additions and 3 deletions

View file

@ -630,9 +630,9 @@
(defprotocol IFreezable1 (-freeze-without-meta! [x data-output])) (defprotocol IFreezable1 (-freeze-without-meta! [x data-output]))
(defprotocol IFreezable2 (-freeze-with-meta! [x data-output])) (defprotocol IFreezable2 (-freeze-with-meta! [x data-output]))
(extend-protocol IFreezable2 ; Must be a separate protocol (extend-protocol IFreezable2 ; Must be a separate protocol
clojure.lang.IMeta clojure.lang.IObj ; IMeta => `meta` will work, IObj => `with-meta` will work
(-freeze-with-meta! [x ^DataOutput data-output] (-freeze-with-meta! [x ^DataOutput data-output]
(let [m (when *incl-metadata?* (.meta x))] (let [m (when *incl-metadata?* (meta x))]
(when m (when m
(write-id data-output id-meta) (write-id data-output id-meta)
(-freeze-without-meta! m data-output))) (-freeze-without-meta! m data-output)))

View file

@ -326,6 +326,8 @@
;;;; Metadata ;;;; Metadata
(def my-var "Just a string")
(deftest _metadata (deftest _metadata
[(is [(is
(:has-meta? (:has-meta?
@ -355,7 +357,12 @@
{:incl-metadata? true} {:incl-metadata? true}
))) )))
"Metadata successfully excluded by freeze")]) "Metadata successfully excluded by freeze")
(is (var? (nippy/read-quarantined-serializable-object-unsafe!
(nippy/thaw (nippy/freeze #'my-var))))
"Don't try to preserve metadata on vars")])
;;;; Benchmarks ;;;; Benchmarks