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 an allow_nil or similar named option that served as a shortcut to achieve this behavior. This way we could go from writing this:
expose :post,
using: PostEntity,
if: fn
(%Comment{post: %Post{}}, _opts) -> true
(_, _) -> false
end
To this:
expose :post, using: PostEntity, allow_nil: true
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:
defmodule PostEntity do
use Maru.entity
serializes Post
expose :id
end
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
:ifoption. 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_nilor 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 SomeModuletop level option to specify what struct or structs this entity can accept: