View Source Ash.CodeInterface (ash v3.4.54)

Used to define the functions of a code interface for a resource.

Summary

Functions

Defines the code interface for a given resource + domain combination in the current module. For example

Selectively merges default opts into client-provided opts. For most keys, the value in opts will be used instead of the default if provided. However, certain options have special behavior

A common pattern is for a function to have both optional parameters and optional options. This usually comes in the form of two defaults

Functions

define_interface(domain, resource, definitions \\ nil)

(macro)

Defines the code interface for a given resource + domain combination in the current module. For example:

defmodule MyApp.Accounting do
  require Ash.CodeInterface

  Ash.CodeInterface.define_interface(MyApp.Accounting, MyApp.Accounting.Transaction)
  Ash.CodeInterface.define_interface(MyApp.Accounting, MyApp.Accounting.Account)
  Ash.CodeInterface.define_interface(MyApp.Accounting, MyApp.Accounting.Invoice)
end

describe_action(resource, action, args)

describe_calculation(resource, calculation, args)

merge_default_opts(opts, default_opts)

@spec merge_default_opts(keyword(), keyword()) :: keyword()

Selectively merges default opts into client-provided opts. For most keys, the value in opts will be used instead of the default if provided. However, certain options have special behavior:

  • :bulk_options, :page - These options are deep merged, so if the default is a keyword list and the client value is a keyword list, they'll be merged.
  • :load - The default value and the client value will be combined in this case.

Examples

iex> merge_default_opts([key1: 1], key2: 2)
[key2: 2, key1: 1]

iex> merge_default_opts([key2: :client], key1: :default, key2: :default)
[key2: :client, key1: :default]

iex> merge_default_opts([page: false], page: [limit: 100])
[page: false]

iex> merge_default_opts([page: [offset: 2]], page: [limit: 100])
[page: [limit: 100, offset: 2]]

iex> merge_default_opts([load: [:calc2, :rel4]], load: [:calc1, rel1: [:rel2, :rel3]])
[load: [:calc1, {:rel1, [:rel2, :rel3]}, :calc2, :rel4]]

params_and_opts(params, opts)

@spec params_and_opts(
  params_or_opts :: map() | [map()] | keyword(),
  keyword()
) :: {params :: map() | [map()], opts :: keyword()}

A common pattern is for a function to have both optional parameters and optional options. This usually comes in the form of two defaults:

  • An empty map for params.
  • An empty list for options.

With those two defaults in mind, this function will decipher, from two inputs, which should be parameters and which should be options.

Parameters can take one of two primary forms:

  1. A map.
  2. A list of maps for bulk operations.

Additionally, if options are set explicitly (i.e. at least one option has been set), a keyword list will be converted to a map.

Examples

iex> params_and_opts(%{key: :param}, [key: :opt])
{%{key: :param}, [key: :opt]}

iex> params_and_opts([key: :opt], [])
{%{}, [key: :opt]}

iex> params_and_opts([], [])
{[], []}

iex> params_and_opts([%{key: :param}], [])
{[%{key: :param}], []}

iex> params_and_opts([key: :param], [key: :opt])
{%{key: :param}, [key: :opt]}

params_and_opts(params_or_opts, maybe_opts, post_process_opts_fn)

@spec params_and_opts(
  params_or_opts :: map() | [map()] | keyword(),
  keyword(),
  (keyword() -> keyword())
) :: {params :: map() | [map()], opts :: keyword()}

See params_and_opts/2.

Adds a post process function that can takes the opts and can further process, validate, or transform them.

trim_double_newlines(str)

unwrap_calc_interface_args(keys, resource, arguments, function_head? \\ false)

without_optional(keys)