AshPhoenixGenApi.Utils (ash_phoenix_gen_api v1.3.1)

Copy Markdown View Source

Internal helpers shared across transformers, verifiers, and Info modules.

These functions consolidate logic that was previously duplicated across AshPhoenixGenApi.Transformers.* and AshPhoenixGenApi.Verifiers.*.

Summary

Functions

Extracts a value from a Spark.InfoGenerator accessor result.

Formats a source annotation as a "Defined at" suffix for error messages.

Returns a list of human-readable problems with an MFA tuple.

Checks whether a value has the shape of a valid MFA tuple {module, function, args} where module and function are atoms and args is a list.

Functions

extract_opt(value, default)

@spec extract_opt({:ok, term()} | :error | term(), term()) :: term()

Extracts a value from a Spark.InfoGenerator accessor result.

Spark.InfoGenerator generates two versions of each accessor:

  • gen_api_foo/1 returns {:ok, value} or :error
  • gen_api_foo!/1 returns the value or raises
  • Predicate functions (ending with ?) return the value directly

This helper unwraps the {:ok, value} tuple, falls back to the provided default when the option is not configured (:error), and passes through direct values (for predicate functions).

format_source_location(anno)

@spec format_source_location(term()) :: String.t()

Formats a source annotation as a "Defined at" suffix for error messages.

Supports tuple ({line, column}), property-list, and other annotation shapes produced by :erl_anno. Returns an empty string when no usable annotation is available.

mfa_errors(other)

@spec mfa_errors(term()) :: [String.t()]

Returns a list of human-readable problems with an MFA tuple.

Used by verifiers to explain exactly which parts of the tuple are invalid.

valid_mfa?(arg1)

@spec valid_mfa?(term()) :: boolean()

Checks whether a value has the shape of a valid MFA tuple {module, function, args} where module and function are atoms and args is a list.