Skip to content

Commit

Permalink
[mod] Don't attach empty metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Apr 10, 2024
1 parent 37cf415 commit 82a050b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/taoensso/nippy.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions test/taoensso/nippy_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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")])

Expand Down

0 comments on commit 82a050b

Please sign in to comment.