AshIntrospection.Codegen.ActionIntrospection (AshIntrospection v0.4.0)

Copy Markdown View Source

Provides helper functions for analyzing Ash actions.

This module contains language-agnostic utilities for determining action characteristics, enabling code generators to produce appropriate client code for different action types.

Features

  • Pagination Analysis - Detect offset, keyset, required, and countable pagination
  • Input Requirements - Determine if actions require, optionally accept, or have no input
  • Return Type Classification - Identify field-selectable return types for generic actions

Usage

Pagination Support

alias AshIntrospection.Codegen.ActionIntrospection

action = AshIntrospection.ResourceInfo.action(MyApp.Post, :list)

if ActionIntrospection.action_supports_pagination?(action) do
  # Generate paginated response type
  if ActionIntrospection.action_supports_offset_pagination?(action) do
    # Include offset/limit parameters
  end
  if ActionIntrospection.action_supports_keyset_pagination?(action) do
    # Include after/before parameters
  end
end

Input Requirements

case ActionIntrospection.action_input_type(resource, action) do
  :required -> # Generate required input parameter
  :optional -> # Generate optional input parameter
  :none     -> # No input parameter needed
end

# Get specific field lists
required_fields = ActionIntrospection.get_required_inputs(resource, action)
optional_fields = ActionIntrospection.get_optional_inputs(resource, action)

Generic Action Return Types

case ActionIntrospection.action_returns_field_selectable_type?(action) do
  {:ok, :resource, MyApp.User} ->
    # Returns a single User, can select fields

  {:ok, :array_of_resource, MyApp.User} ->
    # Returns array of Users, can select fields

  {:ok, :typed_map, fields} ->
    # Returns map with typed fields, can select from field list

  {:ok, :unconstrained_map, nil} ->
    # Returns map without constraints, no field selection

  {:error, :not_field_selectable_type} ->
    # Returns primitive type, no field selection

  {:error, :not_generic_action} ->
    # Not a generic action (use standard CRUD handling)
end

Design Notes

The return type analysis uses a type-driven classification pattern with classify_return_type/2 for consistent handling of all type variants including resources, typed maps, typed structs, and primitives.

Summary

Functions

Returns true if the action has a default limit configured.

Returns :required | :optional | :none

Returns true if the action requires pagination.

Checks if a generic action returns a field-selectable type.

Returns true if the action supports countable pagination.

Returns true if the action returns a type that supports field selection.

Returns true if the action supports keyset-based pagination.

Returns true if the action supports offset-based pagination.

Returns true if the action supports pagination.

Returns the default limit for the action, or nil if not configured.

Returns the max page size for the action, or nil if not configured.

Returns the list of optional input fields for an action.

Gets the pagination configuration for an action.

Returns the list of required input fields for an action.

Returns true if the action has pagination configuration.

Functions

action_has_default_limit?(action)

Returns true if the action has a default limit configured.

action_input_type(resource, action, config \\ %{})

Returns :required | :optional | :none

Determines whether an action requires input, has optional input, or has no input. This is based on the action's public arguments and accepted attributes.

action_requires_pagination?(action)

Returns true if the action requires pagination.

action_returns_field_selectable_type?(action, config \\ %{})

Checks if a generic action returns a field-selectable type.

Returns:

  • {:ok, :resource, resource_module} - Single resource
  • {:ok, :array_of_resource, resource_module} - Array of resources
  • {:ok, :typed_map, fields} - Typed map with constraints
  • {:ok, :array_of_typed_map, fields} - Array of typed maps
  • {:ok, :typed_struct, {module, fields}} - Type with field constraints (TypedStruct or similar)
  • {:ok, :array_of_typed_struct, {module, fields}} - Array of types with field constraints
  • {:ok, :unconstrained_map, nil} - Map without field constraints
  • {:error, :not_generic_action} - Not a generic action
  • {:error, reason} - Other errors

action_supports_countable?(action)

Returns true if the action supports countable pagination.

action_supports_field_selection?(action, config \\ %{})

Returns true if the action returns a type that supports field selection.

This is a convenience wrapper that returns a boolean.

action_supports_keyset_pagination?(action)

Returns true if the action supports keyset-based pagination.

action_supports_offset_pagination?(action)

Returns true if the action supports offset-based pagination.

action_supports_pagination?(action)

Returns true if the action supports pagination.

Examples

iex> action_supports_pagination?(%{type: :read, get?: false, pagination: %{offset?: true}})
true

iex> action_supports_pagination?(%{type: :read, get?: true})
false

get_default_limit(action)

Returns the default limit for the action, or nil if not configured.

get_max_page_size(action)

Returns the max page size for the action, or nil if not configured.

get_optional_inputs(resource, action, config \\ %{})

Returns the list of optional input fields for an action.

This returns the field names that can be provided but are not required.

get_pagination_config(action)

Gets the pagination configuration for an action.

get_required_inputs(resource, action, config \\ %{})

Returns the list of required input fields for an action.

This returns the field names that must be provided (non-nil, no default).

has_pagination_config?(action)

Returns true if the action has pagination configuration.