[mod] Don't attach empty metadata

This commit is contained in:
Peter Taoussanis 2024-04-09 09:01:31 +02:00
parent 37cf415c02
commit 82a050b925
2 changed files with 10 additions and 7 deletions

View file

@ -596,7 +596,7 @@
(extend-protocol IFreezableWithMeta
clojure.lang.IObj ; IMeta => `meta` will work, IObj => `with-meta` will work
(-freeze-with-meta! [x ^DataOutput data-output]
(when-let [m (when *incl-metadata?* (meta x))]
(when-let [m (when *incl-metadata?* (not-empty (meta x)))]
(write-id data-output id-meta)
(write-map data-output m :is-metadata))
(-freeze-without-meta! x data-output))
@ -1535,10 +1535,11 @@
id-meta-protocol-key ::meta-protocol-key
id-meta
(let [m (thaw-from-in! in)]
(if *incl-metadata?*
(with-meta (thaw-from-in! in) (dissoc m ::meta-protocol-key))
(do (thaw-from-in! in))))
(let [m (thaw-from-in! in) ; Always consume from stream
x (thaw-from-in! in)]
(if-let [m (when *incl-metadata?* (not-empty (dissoc m ::meta-protocol-key)))]
(with-meta x m)
(do x)))
id-cached-0 (thaw-cached 0 in)
id-cached-1 (thaw-cached 1 in)

View file

@ -97,8 +97,10 @@
(is (enc/throws? :ex-info "Unfreezable type" (nippy/freeze (fn []))))
(testing "Clojure v1.10+ metadata protocol extensions"
[(is (= (meta (nippy/thaw (nippy/freeze (with-meta [] {:a :A, 'b/c (fn [])})))) {:a :A}))
(is (enc/throws? :ex-info "Unfreezable type" (nippy/freeze (with-meta [] {:a :A, 'b (fn [])}))))])
[(is (enc/throws? :ex-info "Unfreezable type" (nippy/freeze (with-meta [] {:a :A, 'b (fn [])}))))
(is (= {:a :A} (meta (nippy/thaw (nippy/freeze (with-meta [] {:a :A, 'b/c (fn [])}))))))
(is (= nil (meta (nippy/thaw (nippy/freeze (with-meta [] { 'b/c (fn [])})))))
"Don't attach empty metadata")])
(is (gen-test 1600 [gen-data] (= gen-data (thaw (freeze gen-data)))) "Generative")])