Support decoding user-marshalled objects #30
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements two enhancements which permit decoding user-marshalled objects (indicated by the
U
type code):Date
) as well as any attributes associated with that class (as written out by themarshal_dump
method of the Ruby class). For example, here's the result of parsing the data marshalled by runningMarshal.dump(Date.today)
in Ruby:ExMarshal.decode
(andExMarshal.Decoder.decode
) now support taking options. The only supported option is calleduser_object_parsers
which is expected to take a map associating Ruby class names (i.e. Elixir atoms) with unary functions processing the attributes written by Ruby. This is useful to customise the handling of user objects such that instead of getting a 2-tuple as shown above, custom Ruby types likeDate
get automatically converted to appropriate Elixir types. Here's an example showing how it can be used to convert Ruby dates to Elixir dates:Two plausible follow-up improvements come to mind:
ExMemcached
user myself, but I suppose it would be plausible to also permit specifying custom parsers via configuration files such that the transcoder of Memcached would transparently map types using user object parsers.ExMarshal.CustomParsers
or such could be devised which implements custom parser functions for common Ruby types such asDate
.