Domo.__using__
__using__
, go back to Domo module for more information.
Uses Domo in the current struct's module to add constructor, validation, and reflection functions.
defmodule Model do
use Domo
defstruct [:first_field, :second_field]
@type t :: %__MODULE__{first_field: atom() | nil, second_field: any() | nil}
# have added:
# new!/1
# new/2
# ensure_type!/1
# ensure_type/2
# typed_fields/1
# required_fields/1
end
use Domo
can be called only within the struct module having
t()
type defined because it's used to generate __MODULE__.TypeEnsurer
with validation functions for each field in the definition.
See details about t()
type definition in Elixir
TypeSpecs document.
The macro collects t()
type definitions for the :domo_compiler
which
generates TypeEnsurer
modules during the second pass of the compilation
of the project. Generated validation functions rely on guards appropriate
for the field types.
The generated code of each TypeEnsurer
module can be found
in _build/MIX_ENV/domo_generated_code
folder. However, that is for information
purposes only. The following compilation will overwrite all changes there.
The macro adds the following functions to the current module, that are the
facade for the generated TypeEnsurer
module:
new!/1
, new/2
, ensure_type!/1
, ensure_type/2
, typed_fields/1
,
required_fields/1
.
Options
ensure_struct_defaults
- if set tofalse
, disables the validation of default values given withdefstruct/1
to conform to thet()
type at compile time. Default istrue
.name_of_new_function
- the name of the constructor function added to the module. The raising error function name is generated automatically from the given one by adding trailing!
. Defaults arenew
andnew!
appropriately.unexpected_type_error_as_warning
- if set totrue
, prints warning instead of throwing an error for field type mismatch in the raising functions. Default isfalse
.remote_types_as_any
- keyword list of type lists by modules that should be treated asany()
. F.e.[{ExternalModule, [:t, :name]}, {OtherModule, :t}]
Default isnil
.
The option value given to the macro overrides one set globally in the
configuration with config :domo, option: value
.