Skip to content

Feature: :allow_nil to use in conjunction with using: SomeSerializer, and option serializes #14

@nicooga

Description

@nicooga

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions