Estructura Cheatsheet

View Source

Setting up structures

use Estructura or use Estructura.Nested

Basic structure definition

defmodule MyStruct do
  use Estructura
  defstruct [:field1, :field2]
end

Nested structure definition with types and validation

defmodule User do
  use Estructura.Nested
  
  defstruct [
    name: "",
    address: %{city: "", postal_code: ""}
  ]
  
  def type(:name), do: Estructura.Nested.Type.String
  def validate(:name, value), do: String.length(value) > 0
end

Type System

Built-in Types

Available types for common data structures

Estructura.Nested.Type.DateTime
Estructura.Nested.Type.Date
Estructura.Nested.Type.Time
Estructura.Nested.Type.URI
Estructura.Nested.Type.IP
Estructura.Nested.Type.String
Estructura.Nested.Type.UUID

Type Scaffolds

Enum Types for predefined values

defmodule Status do
  use Estructura.Nested.Type.Enum,
    elements: [:pending, :active, :completed]
end

Tag Sets for multiple predefined values

defmodule Categories do
  use Estructura.Nested.Type.Tags,
    elements: [:tech, :art, :science]
end

Validation and Coercion

validate/2

Define validation rules for fields

def validate(:age, value), do: value >= 0
def validate("address.postal_code", value), do: String.match?(value, ~r/^\d{5}$/)

coerce/2

Define coercion rules for data transformation

def coerce(:temperature, str) when is_binary(str) do
  case Float.parse(str) do
    {num, ""} -> {:ok, num}
    _ -> {:error, "Invalid number"}
  end
end

Special Features

Lazy Values

Defer computation until needed

defmodule Cache do
  use Estructura.Nested
  defstruct value: Estructura.Lazy.new(&expensive_computation/1)
end

Flattening

Convert nested structures to flat maps

# Enable flattening
use Estructura.Nested, flattenable: true

# Usage
Estructura.Flattenable.flatten(struct)
# => %{"name" => "value", "address_city" => "London"}

Property Testing

Generate test data automatically

property "valid structures are validated" do
  check all struct <- MyStruct.__generator__() do
    assert {:ok, ^struct} = MyStruct.validate(struct)
  end
end

Common Options

  • flattenable: true - Enable structure flattening
  • jason: true - Enable JSON encoding
  • transformer: true - Enable transformation capabilities

Version: 1.9.0