taoensso / nippy

The fastest serialization library for Clojure
https://www.taoensso.com/nippy
Eclipse Public License 1.0
1.04k stars 60 forks source link

[Bug] Freezing `deftype` instances fail with fields that have dash in the name #145

Closed Outrovurt closed 2 years ago

Outrovurt commented 2 years ago

I've just gotten to the bottom of what appeared at first glance to be a strange issue with custom types.

If I have the following:

(deftype MyType [a b c])

then call:

(nippy/freeze (->MyType 1 2 3))

it works fine, and I can then thaw it.

First question, why does this even work, that is, why don't I have to define a custom freeze/thaw for it? Not that I'm complaining, I'm just curious as to know when I need to use extend-freeze, extend-thaw.

I have a lot of custom types, and there is only one for which freezing was failing. It looks something like this:

(deftype MyFailingType [vs eval-func spec])

When I then removed the dash in eval-func to make evalfunc, freezing, thawing worked.

So my second question is, assuming I can't change the name of the field, to get this to work do I then need to write extend-freeze, extend-thaw code for MyFailingType? If so, I am not entirely sure how to go about doing it as the example just show how to do so for a type with a single field.

Thanks.

ptaoussanis commented 2 years ago

@Outrovurt Can confirm that this is a bug, will be fixed in the next release. Thanks for the report, apologies for the trouble! Cheers :-)

ptaoussanis commented 2 years ago

Sorry, missed your questions:

First question, why does this even work, that is, why don't I have to define a custom freeze/thaw for it?

It works since Nippy can freeze deftype instances by automatically detecting and serializing the relevant fields, see https://github.com/ptaoussanis/nippy/pull/112.

Your second example wasn't working due to a bug in Nippy. Basically the "eval-func" field name needed to be munged to "eval_func" to be compatible with Java's field name requirements. This'll now be done properly as of [com.taoensso/nippy "3.2.0-RC3"].

I'm just curious as to know when I need to use extend-freeze, extend-thaw.

So long as your field values are types that Nippy knows how to freeze, the automatic deftype serialization should be fine for most folks. You may be able to get better performance by writing a custom freeze/thaw implementation (for one this would avoid the need for automatic field detection) - but in most cases the difference probably wouldn't be significant unless you're freezing a very large number of such types.

Hope that helps!