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

Summary

Types

t()

All the types that implement this protocol.

Functions

The function returning the flattened input.

Types

t()

@type t() :: term()

All the types that implement this protocol.

Functions

flatten(input, options \\ [])

@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}

Allowed options are:

  • coupler the string to concatenate nested keys with, default: -
  • only the list of keys to select
  • except the list of keys to ignore
  • jsonify true or a json encoder implementation; if set, the values will be jsonified

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