MishkaGervaz.Table.Behaviours.TypeRegistry behaviour (MishkaGervaz v0.0.1-alpha.2)

Copy Markdown View Source

Behaviour for type registry modules.

Ensures consistent API across column, filter, and action type registries. Each registry maps atom names to module implementations.

Usage

Simple format (for filters, actions)

defmodule MyApp.Table.Types.Filter do
  use MishkaGervaz.Table.Behaviours.TypeRegistry,
    builtin: %{
      text: MyApp.Table.Types.Filter.Text,
      select: MyApp.Table.Types.Filter.Select
    },
    default: MyApp.Table.Types.Filter.Text
end

Tuple format with Ash type inference (for columns)

defmodule MyApp.Table.Types.Column do
  use MishkaGervaz.Table.Behaviours.TypeRegistry,
    builtin: %{
      text: {MyApp.Table.Types.Column.Text, []},
      boolean: {MyApp.Table.Types.Column.Boolean, [Ash.Type.Boolean]},
      number: {MyApp.Table.Types.Column.Number, [Ash.Type.Integer, Ash.Type.Float]}
    },
    default: MyApp.Table.Types.Column.Text
end

When using tuple format {Module, [AshTypes]}, the macro automatically generates infer_from_ash_type/1 that maps Ash types to the corresponding module.

Provided Functions

When you use this behaviour, you get:

  • get/1 - Get module by atom name (returns module or nil)
  • builtin_types/0 - List all registered type names
  • builtin?/1 - Check if type name is registered
  • default/0 - Get the default type module
  • get_or_passthrough/1 - Get module, or return type as-is for custom modules
  • infer_from_ash_type/1 - (tuple format only) Infer module from Ash attribute type

Optional Callbacks

  • resolve_type/1 - Resolve type from config map (single context)
  • resolve_type/2 - Resolve type from config map with extra context

Consumers

This behaviour is use-d by:

See MishkaGervaz.Table.Behaviours.ColumnType, MishkaGervaz.Table.Behaviours.FilterType, MishkaGervaz.Table.Behaviours.ActionType, and MishkaGervaz.Form.Behaviours.FieldType.

Summary

Callbacks

Check if a type name is a built-in type.

List all built-in type names.

Get the module for a built-in type.

Resolve type module from configuration.

Resolve type module from configuration with additional context.

Callbacks

builtin?(type)

@callback builtin?(type :: atom()) :: boolean()

Check if a type name is a built-in type.

builtin_types()

@callback builtin_types() :: [atom()]

List all built-in type names.

get(type)

@callback get(type :: atom()) :: module() | nil

Get the module for a built-in type.

Returns the module if found, nil otherwise.

resolve_type(config)

(optional)
@callback resolve_type(config :: map()) :: module()

Resolve type module from configuration.

Used when type needs to be determined from a config map (e.g., filter config with :type key).

resolve_type(config, context)

(optional)
@callback resolve_type(config :: map(), context :: map()) :: module()

Resolve type module from configuration with additional context.

Used when type resolution needs extra context (e.g., column config + resource attributes).