Skip to content

Commit

Permalink
fix: hydrate metadata types
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Aug 29, 2021
1 parent f4899a7 commit 541545b
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/ash/resource/transformers/set_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Ash.Resource.Transformers.SetTypes do
def transform(_resource, dsl_state) do
with {:ok, dsl_state} <- set_attribute_types(dsl_state),
{:ok, dsl_state} <- set_argument_types(dsl_state),
{:ok, dsl_state} <- set_metadata_types(dsl_state),
{:ok, dsl_state} <- set_calculation_types(dsl_state) do
{:ok, dsl_state}
end
Expand Down Expand Up @@ -74,6 +75,44 @@ defmodule Ash.Resource.Transformers.SetTypes do
end)
end

defp set_metadata_types(dsl_state) do
dsl_state
|> Transformer.get_entities([:actions])
|> Enum.filter(&Map.has_key?(&1, :metadata))
|> Enum.reduce_while({:ok, dsl_state}, fn action, {:ok, dsl_state} ->
new_metadatas =
action.metadata
|> Enum.reduce_while({:ok, []}, fn metadata, {:ok, metadatas} ->
type = Ash.Type.get_type(metadata.type)

case validate_constraints(type, metadata.constraints) do
{:ok, constraints} ->
{:cont, {:ok, [%{metadata | type: type, constraints: constraints} | metadatas]}}

{:error, error} ->
{:halt, {:error, error}}
end
end)

case new_metadatas do
{:ok, new_metadatas} ->
{:cont,
{:ok,
Transformer.replace_entity(
dsl_state,
[:actions],
%{action | metadata: Enum.reverse(new_metadatas)},
fn replacing ->
replacing.name == action.name && replacing.type == action.type
end
)}}

{:error, error} ->
{:halt, {:error, error}}
end
end)
end

defp set_calculation_types(dsl_state) do
dsl_state
|> Transformer.get_entities([:calculations])
Expand Down

0 comments on commit 541545b

Please sign in to comment.