Support decoding user-marshalled objects#30
Merged
gaynetdinov merged 4 commits intogaynetdinov:masterfrom Jul 28, 2022
Merged
Conversation
I plan to extend the state with a new field. Let's make sure that this field gets passed around as expected by making update_references not build a brand new state from scratch. Instead, we can just update a single field.
This isn't used for anything yet, but I plan to pass additional flags to decode() which can be used to influence how user objects are decoded.
This option can be used to specify a map associating atoms with unary
functions. These functions will be considered when trying to decode user
objects. I.e. instead of user objects resulting in 2-tuples {class_name,
attributes}, a custom function is invoked which can then do something
interesting with the attributes.
One example for this would be to parse a Ruby date into an Elixir date.
Owner
|
Hey, sorry for the late reply. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Utype code):Date) as well as any attributes associated with that class (as written out by themarshal_dumpmethod 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_parserswhich 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 likeDateget 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:
ExMemcacheduser 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.CustomParsersor such could be devised which implements custom parser functions for common Ruby types such asDate.