taoensso / faraday

Amazon DynamoDB client for Clojure
https://www.taoensso.com/faraday
Eclipse Public License 1.0
238 stars 84 forks source link

Avoid capturing serialize value in clj-item->db-item #162

Closed gusbicalho closed 2 years ago

gusbicalho commented 2 years ago

The goal of the ISerializable protocol is to allow extending the serialization mechanism to custom types, via extend-protocol. For example, we can (extend-protocol ISerializable UUID (serialize [id] ...)) to implement serialization for UUIDs.

This extend-protocol call works by redefining the serialize function, i.e. creating a new function and changing the #'serialize var to point to this new object. The new function knows how to dispatch to the correct implementation for the types specified in the extend-protocol.

However, the implementation of clj-item->db-item using partial captures the value of the serialize function at the moment the namespace is loaded. Therefore, it will ignore any further extend-protocol calls - these will replaced the value referenced by the #'serialize var, but the clj-item->db-item function will still hold a reference to the original serialize function object.

To solve this, we have to ensure we always go through the #'serialize var when we run clj-item->db-item. This can be done in one of two ways:

I choose the latter solution because it seemed easier to understand.