Domo.__using__

You're seeing just the macro __using__, go back to Domo module for more information.
Link to this macro

__using__(opts)

View Source (macro)

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_ok/2
  # ensure_type!/1
  # ensure_type_ok/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_ok/2, ensure_type!/1, ensure_type_ok/2, typed_fields/1, required_fields/1.

Options

  • ensure_struct_defaults - if set to false, disables the validation of default values given with defstruct/1 to conform to the t() type at compile time. Default is true.

  • name_of_new_function - the name of the constructor function added to the module. The ok function name is generated automatically from the given one by omitting trailing ! if any, and appending _ok. Defaults are new! and new_ok appropriately.

  • unexpected_type_error_as_warning - if set to true, prints warning instead of throwing an error for field type mismatch in the raising functions. Default is false.

  • remote_types_as_any - keyword list of type lists by modules that should be treated as any(). F.e. [{ExternalModule, [:t, :name]}, {OtherModule, :t}] Default is nil.

The option value given to the macro overrides one set globally in the configuration with config :domo, option: value.