AshLua.Type behaviour (ash_lua v0.1.5)

Copy Markdown View Source

Extends an Ash type with Lua-facing metadata and encoding.

Add use AshLua.Type to a custom type module (typically alongside use Ash.Type or use Ash.Type.NewType) and implement any of the optional callbacks.

type_name/0

Controls the name surfaced in generated documentation (AshLua.Docs) for that type.

defmodule MyApp.Slug do
  use Ash.Type.NewType, subtype_of: :string
  use AshLua.Type

  @impl AshLua.Type
  def type_name, do: "slug"
end

When a module does not implement type_name/0, the default name is derived from the module itself: the last module segment, with Ash.Type. prefixed modules underscored (Ash.Type.UUID"uuid", Ash.Type.Boolean"boolean").

to_lua/2

Controls how a value of this type is encoded for the Lua side. Return any Lua-friendly value (maps/lists/strings/numbers/booleans/nil) — it is run through the standard normalization afterwards, so nested Decimal/Date/ etc. are still handled for you.

defmodule MyApp.Money do
  use Ash.Type.NewType, subtype_of: :integer
  use AshLua.Type

  @impl AshLua.Type
  def to_lua(cents, _constraints) do
    %{"cents" => cents}
  end
end

When a type does not implement to_lua/2, AshLua falls back to its built-in defaults: special handling for the Ash builtin types that need it (CiString, Decimal, dates/times, durations), and a plain JSON-style encoding for everything else.

Summary

Functions

Encodes a value using the type module's to_lua/2 callback, if it defines one.

Returns the Lua-facing type name for the given module.

Callbacks

to_lua(value, constraints)

(optional)
@callback to_lua(value :: term(), constraints :: Keyword.t()) :: term()

type_name()

(optional)
@callback type_name() :: String.t()

Functions

to_lua(module, value, constraints)

@spec to_lua(module() | nil, term(), Keyword.t()) :: {:ok, term()} | :default

Encodes a value using the type module's to_lua/2 callback, if it defines one.

Returns {:ok, encoded} when the module implements to_lua/2, or :default when it doesn't — letting the caller fall back to built-in encoding.

type_name(module)

@spec type_name(module()) :: String.t()

Returns the Lua-facing type name for the given module.

Uses the module's type_name/0 callback when defined, otherwise derives a name from the module itself.