AshLua.Fields (ash_lua v0.1.0)

Copy Markdown View Source

Manifest-driven field selection for Lua-side queries.

Translates a fields tree supplied by a Lua script into the three things Ash needs:

The Lua side accepts a list of strings and one-key maps:

fields = {
  "id", "title",
  { author = { "id", "name" } },                          -- nested relationship
  { comments = { "id", "body" } },                        -- has_many
  { title_prefixed = { args = { prefix = "x: " } } },     -- calc with args
  { metadata = { "priority", "category" } },              -- typed map sub-selection
  { coordinates = { "latitude", "longitude" } },          -- typed tuple
  { content = { text = { "body" } } },                    -- union member sub-selection
}

Default (no fields provided) returns primary-key attributes only for resource records. For non-resource return types (typed map, tuple, primitive, etc.) the whole value is rendered.

Type dispatch and template construction are driven entirely from Ash.Info.Manifest.generate/1 IR — no Ash.Resource.Info.* traversal here.

Summary

Functions

Builds {select, load, template} for the given resource + action.

Parses a Lua-decoded fields input into the internal AST.

Functions

for_action(manifest, resource, action, requested)

@spec for_action(Ash.Info.Manifest.t(), atom(), Ash.Info.Manifest.Action.t(), term()) ::
  {:ok, {[atom()], term(), term()}} | {:error, term()}

Builds {select, load, template} for the given resource + action.

requested is the already-decoded Lua input (the value of the fields key) — either nil/empty (defaults apply) or a list of items produced by AshLua.Encoder.decode_input/1.

parse(list)

@spec parse(term()) :: {:ok, :default | list()} | {:error, term()}

Parses a Lua-decoded fields input into the internal AST.

Returns {:ok, :default} when no fields were supplied, {:ok, ast} otherwise.