View Source Estructura.Flattenable protocol (estructura v1.2.12)

Summary

Types

t()

All the types that implement this protocol.

Functions

The function returning the flattened input.

Types

@type t() :: term()

All the types that implement this protocol.

Functions

Link to this function

flatten(input, options \\ [])

View Source
@spec flatten(
  t(),
  keyword()
) :: term()

The function returning the flattened input.

This protocol is explicitly handful when deeply nested structs are to be serialized.

iex|%_{}|1  %Estructura.User{}
%Estructura.User{
  address: %Estructura.User.Address{
    city: nil,
    street: %Estructura.User.Address.Street{house: nil, name: []}
  },
  data: %Estructura.User.Data{age: nil},
  name: nil
}

iex|%_{}|2  Estructura.Flattenable.flatten(%Estructura.User{}, coupler: "-", except: ~w|address-street data-age|)
%{"address-city" => nil, "birthday" => nil, "created_at" => nil, "name" => nil}
iex|%_{}|3  Estructura.Flattenable.flatten(%Estructura.User{}, only: ~w|address_street data_age|)
%{"address_street_house" => nil, "data_age" => nil}
iex|%_{}|4  Estructura.Flattenable.flatten(%Estructura.User{}, only: ~w|address|)
%{"address_city" => nil, "address_street_house" => nil}

To enable it for your struct, use @derive Estructura.Flattenable or @derive {Estructura.Flattenable, options}. Estructura implementations derive it be default.