ToStruct

Build StatusHex.pm

Package helps you transform maps (ex. external data) to structs (internal data).

Example

Define a structs:

defmodule Product do
  defstruct name: nil
end

defmodule Cart do
  defstruct id: nil,
    products: nil
end

Define a transformation schema and then transform a map:

def to_struct() do
  map = %{
    "id" => 1,
    "products" => [
      %{
        "name" => "milk"
      },
      %{
        "name" => "potato",
      }
    ]
  }

  schema = %Cart{
    products: [%Product{}],
  }

  ToStruct.transform(schema, map)
end

Result:

 %Cart{
  id: 1,
  products: [
    %Product{name: "milk"},
    %Product{name: "potato"}
  ]
 }

Installation

def deps do
  [
    {:to_struct, "~> 0.1.0"}
  ]
end