AttrEngine.Transform behaviour (attr_engine v0.5.0)

Copy Markdown View Source

Render-time value transforms.

Transforms modify attribute values at resolution/render time before they reach consumers. Declare a transform on any attribute via its data_config["transform"] key (a string name or list of string names).

Built-in transforms

  • "redact" — replaces any non-nil value with "••••••••"
  • "redact_secret" — replaces non-nil with "[provided]", returns "[not set]" for nil

Registering custom transforms

Implement the AttrEngine.Transform behaviour and register via application config:

config :attr_engine, transforms: %{
  "currency" => MyApp.Transforms.Currency
}

Or supply a function (arity 1 or 2):

config :attr_engine, transforms: %{
  "upcase" => &String.upcase/1,
  "prefix" => fn value, opts -> opts["prefix"] <> value end
}

External transforms take precedence over built-ins with the same name.

Behaviour

Modules implementing this behaviour must define apply/2:

defmodule MyApp.Transforms.Currency do
  @behaviour AttrEngine.Transform

  @impl true
  def apply(value, _opts) when is_number(value) do
    :erlang.float_to_binary(value / 1.0, decimals: 2)
  end
  def apply(value, _opts), do: value
end

The second argument (opts) is the full data_config map for the attribute, allowing transforms to read additional configuration keys.

Summary

Callbacks

Apply a transform to a value. Receives the raw value and the attribute's data_config.

Functions

Applies transforms across a map of data using resolved attrs_meta.

Looks up a named transform. Returns the module or function, or nil.

Applies the declared transform(s) for an attribute to its value.

Callbacks

apply(value, opts)

@callback apply(value :: any(), opts :: map()) :: any()

Apply a transform to a value. Receives the raw value and the attribute's data_config.

Functions

apply_transforms(attrs_meta, data)

@spec apply_transforms([map()], map()) :: map()

Applies transforms across a map of data using resolved attrs_meta.

For each attribute in attrs_meta that declares a transform in its data_config, the corresponding value in data is transformed.

Returns the data map with transformed values.

lookup(name)

@spec lookup(String.t()) :: module() | function() | nil

Looks up a named transform. Returns the module or function, or nil.

Resolution order: external config > built-ins.

resolve(value, data_config)

@spec resolve(any(), map()) :: any()

Applies the declared transform(s) for an attribute to its value.

Reads data_config["transform"] which may be:

  • a string (single transform name)
  • a list of strings (pipeline, applied left-to-right)
  • nil (no-op, returns value unchanged)

Returns the transformed value.