-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: :allow_nil
to use in conjunction with using: SomeSerializer
, and option serializes
#14
Comments
:allow_nil
to use in conjunction with using: SomeSerializer
:allow_nil
to use in conjunction with using: SomeSerializer
, and option serializes
I think expose :post, using: PostEntity
defmodule PostEntity do
serializers Post
end what will happen when we pass |
I'd expect defmodule Dog do
defstruct [:color, :age]
end
defmodule DogEntity do
use Maru.Entity
serializes Dog
end
# Option 1, Not the "elixir way" but keeps the API the same
DogEntity.serialize(%Dog{}) # returns a serialized dog
DogEntity.serialize(%NotADog{}) # raises Maru.SerializationError
# Option 2, modifies API but I think is better
DogEntity.serialize(%Dog{}) # returns {:ok, serialized_dog}
DogEntity.serialize(%NotADog{}) # returns: {:error, :invalid_module_type}
DogEntity.serialize!(%Dog{}) # returns dog
DogEntity.serialize!(%NotADog{}) # raises Maru.SerializationError |
Hmm, I don't think we need verify check like this. |
I know not anyone needs it but it would be nice to have. You could also pass a list of modules to allow multiple modules. I think we need more opinions here. Additionally, I've noticed you cannot pass a captured function to the expose :some_attr, if: &some_func/2 ... which also limits your options. Anyway, there may be better things to work on at the moment. |
Yes, I'm using it to parse data from mongodb, I also think I missed some important features. |
Reading tests I see that the only way to avoid an error when serializing a nested record that may not be present is using the
:if
option. I think is a good default for the library to raise an error when the associated record is not present. But it would also be nice to have anallow_nil
or similar named option that served as a shortcut to achieve this behavior. This way we could go from writing this:To this:
Yes I know that's a lot of of unneeded pattern matching. But I think that the more pattern match you can do the better, since you are ensuring that your serializer is not passed something it does not expect. In that sense I think it may also good idea to add a
serializes SomeModule
top level option to specify what struct or structs this entity can accept:The text was updated successfully, but these errors were encountered: