Normandy.ParameterizedType behaviour
(normandy v0.6.1)
View Source
Parameterized types are Normandy types that can be customized per field.
Parameterized types allow a set of options to be specified in the schema which are initialized on compilation and passed to the callback functions as the last argument.
For example, field :foo, :string behaves the same for every field.
On the other hand, a parameterized enum type like
field :foo, MyEnum, values: [:foo, :bar, :baz] will have a different
set of values per field.
Note that options are specified as a keyword, but it is idiomatic to
convert them to maps inside init/1 for easier pattern matching in
other callbacks.
Parameterized types are a superset of regular types. In other words,
with parameterized types you can do everything a regular type does,
and more. For example, parameterized types can handle nil values
in both load and dump callbacks, they can customize cast behavior
per query and per changeset, and also control how values are embedded.
However, parameterized types are also more complex. Therefore, if everything you need to achieve can be done with basic types, they should be preferred to parameterized ones.
Examples
To create a parameterized type, create a module as shown below:
defmodule MyApp.MyType do
use Normandy.ParameterizedType
def type(_params), do: :string
def init(opts) do
validate_opts(opts)
Enum.into(opts, %{})
end
def cast(data, params) do
...
{:ok, cast_data}
end
def load(data, _loader, params) do
...
{:ok, loaded_data}
end
def dump(data, dumper, params) do
...
{:ok, dumped_data}
end
def equal?(a, b, _params) do
a == b
end
endTo use this type in a schema field, specify the type and parameters like this:
schema do
field :bar, MyApp.MyType, opt1: :baz, opt2: :boo
endTo use this type in places where you need it to be initialized manually,
you can use init/2.
use Normandy.ParameterizedType
When you use Normandy.ParameterizedType, it will set
@behaviour Normandy.ParameterizedType and define default, overridable
implementations for embed_as/2 and equal?/3.
Summary
Types
The parameters for the ParameterizedType
Callbacks
Casts the given input to the ParameterizedType with the given parameters.
Dumps the given term into an Ecto native type.
Dictates how the type should be treated inside embeds.
Checks if two terms are semantically equal.
Formats output when a ParameterizedType is printed in exceptions and other logs.
Callback to convert the options specified in the field macro into parameters to be used in other callbacks.
Returns the underlying schema type for the ParameterizedType.
Functions
Inits a parameterized type given by type with opts.
Types
Callbacks
Casts the given input to the ParameterizedType with the given parameters.
If the parameterized type is also a composite type,
the inner type can be cast by calling Normandy.Type.cast/2
directly.
For more information on casting, see Normandy.Type.cast/1.
Dumps the given term into an Ecto native type.
It receives a dumper function in case the parameterized
type is also a composite type. In order to dump the inner
type, the dumper must be called with the inner type and
the inner value as argument.
For more information on dumping, see Normandy.Type.dump/1.
Note that this callback will be called when dumping a nil
value, unlike Normandy.Type.dump/1.
Dictates how the type should be treated inside embeds.
By default, the type is sent as itself, without calling dumping to
keep the higher level representation. But it can be set to :dump
so that it is dumped before being encoded.
use Normandy.ParameterizedType injects an overridable default that
returns :self.
Checks if two terms are semantically equal.
Formats output when a ParameterizedType is printed in exceptions and other logs.
Note this callback is not used when constructing validation errors.
See the :message option of most Normandy.Validate validation
functions for how to customize error messaging.
Callback to convert the options specified in the field macro into parameters to be used in other callbacks.
This function is called at compile time, and should raise if invalid values are
specified. It is idiomatic that the parameters returned from this are a map.
field and schema will be injected into the options automatically.
For example, this schema specification
schema do
field :my_field, MyParameterizedType, opt1: :foo, opt2: nil
endwill result in the call:
MyParameterizedType.init([field: :my_field, opt1: :foo, opt2: nil])
@callback type(params()) :: Normandy.Type.t()
Returns the underlying schema type for the ParameterizedType.
For more information on schema types, see Normandy.Type.type/0