Nestru.Encoder protocol (Nestru v0.3.2) View Source

Link to this section Summary

Functions

Returns the fields map from the given struct to be encoded to map recursively.

Link to this section Types

Link to this section Functions

Link to this function

gather_fields_from_struct(struct, context)

View Source

Returns the fields map from the given struct to be encoded to map recursively.

It can be used to adapt the keys of the map to target shape.

The first argument is the encodable struct value adopting the protocol.

The second argument is the context given to encode_to_map/2 or encode_to_list_of_maps/2 functions.

Nestru calls this function as the first step of the encoding procedure.

If the function returns {:ok, map}, then encoding continues, and each map value that is struct is encoded into a map, the same is done recursively for lists, and other values are kept as is.

If the function returns {:error, message} tuple, then encoding stops, and the error is bypassed to the caller.

Any other return value raises an error.

To generate the default implementation of the function, add the @derive Nestru.Encoder attribute to the struct. The :only and :except options are supported to filter the fields.

The default implementation gathers keys from the struct by calling Map.from_struct/1.

Examples

def Supplier do
  # Derive the default implementation
  @derive Nestru.Encoder

  defstruct [:id, :name]
end

def Purchaser do
  # Encode only one field
  @derive {Nestru.Encoder, only: [:id]}

  defstruct [:id, :name, :address]
end

defmodule FruitBox do
  defstruct [:items]

  # Implement the function returning the map explicitly
  defimpl Nestru.Encoder do
    def gather_fields_from_struct(struct, _context) do
      # Rename the key in the result map
      {:ok, %{elements: &Map.get(struct, :items)}}
    end
  end
end