View Source Restruct (restruct v0.1.0)

Restruct is a library for migrating struct values to conform to their current definitions.

This is useful when you have a struct value that was created with an older version of the defining module and you want to update it to match the current definition.

Summary

Functions

Ensure a struct value conforms to its current definition. Any keys that have been removed from the definition will be removed, and any new keys will be added and initialized to their defined defaults.

Functions

Link to this function

migrate(value, opts \\ [])

View Source

Ensure a struct value conforms to its current definition. Any keys that have been removed from the definition will be removed, and any new keys will be added and initialized to their defined defaults.

Warning: There is a potential for data loss when using this function. In addition to dropping values for any keys that have been removed from the definition, plain maps that use structs for keys may end up with fewer entries if any of those keys become equal after having fields removed (the :keep option can be used to prevent this).

Options

  • :recursive - If true, the function will recursively migrate structs in nested data structures. Setting this option to false and passing a value other than a struct is a no-op. Defaults to true.
  • :keep - If true, the function will keep extra fields that are not defined in the struct. Defaults to false.

Examples

iex> defmodule X do
iex>   defstruct foo: nil, bar: 8
iex> end
iex> value = %{__struct__: X, foo: 1, baz: 2}
iex> Restruct.migrate(value)
%{__struct__: X, foo: 1, bar: 8}
iex> Restruct.migrate(value, keep: true)
%{__struct__: X, foo: 1, bar: 8, baz: 2}
iex> :code.purge(X)
iex> :code.delete(X)