improvement: storage_type/0 -> storage_type/1

This commit is contained in:
Zach Daniel 2023-08-17 17:51:44 -04:00
parent 025f75a1aa
commit b51dbe49c9
30 changed files with 58 additions and 41 deletions

View file

@ -119,7 +119,7 @@ defmodule Ash.Actions.Sort do
!Ash.DataLayer.data_layer_can?(
resource,
{:sort, Ash.Type.storage_type(attribute.type)}
{:sort, Ash.Type.storage_type(attribute.type, attribute.constraints)}
) ->
{sorts,
[
@ -191,7 +191,8 @@ defmodule Ash.Actions.Sort do
if Ash.DataLayer.data_layer_can?(resource, :aggregate_sort) &&
Ash.DataLayer.data_layer_can?(
resource,
{:sort, Ash.Type.storage_type(type)}
# do we need to get actual constraints for aggregates here?
{:sort, Ash.Type.storage_type(type, [])}
) do
{sorts ++ [{field, order}], errors}
else

View file

@ -145,7 +145,7 @@ defmodule Ash.EmbeddableType do
quote location: :keep do
alias Ash.EmbeddableType.ShadowApi
def storage_type, do: :map
def storage_type(_), do: :map
def cast_input(%{__struct__: __MODULE__} = input, _constraints), do: {:ok, input}

View file

@ -88,7 +88,7 @@ defmodule Ash.Resource do
use Ash.Type
@impl true
def storage_type, do: :map
def storage_type(_), do: :map
@impl Ash.Type
def cast_input(nil, _), do: {:ok, nil}

View file

@ -58,7 +58,7 @@ defmodule Ash.Resource.Verifiers.ValidateRelationshipAttributesMatch do
Types are considered compatible if:
1. They are exactly the same
2. Their `storage_type/0` callback returns the same value
2. Their `storage_type/1` callback returns the same value
3. The storage types are `:text` and `:string`
4. The relationship has `validate_destination_attribute?` set to `false`.
5. They are explicitly configured as compatible. To do so in this instance, add it to your config like so:
@ -71,9 +71,12 @@ defmodule Ash.Resource.Verifiers.ValidateRelationshipAttributesMatch do
"""
end
defp compatible_types?(%{type: source}, %{type: dest}) do
left_storage_type = Ash.Type.storage_type(source)
right_storage_type = Ash.Type.storage_type(dest)
defp compatible_types?(%{type: source, constraints: source_constraints}, %{
type: dest,
constraints: dest_constraints
}) do
left_storage_type = Ash.Type.storage_type(source, source_constraints)
right_storage_type = Ash.Type.storage_type(dest, dest_constraints)
cond do
source == dest ->

View file

@ -17,7 +17,7 @@ defmodule Ash.Type.Atom do
use Ash.Type
@impl true
def storage_type, do: :string
def storage_type(_), do: :string
@impl true
def constraints, do: @constraints

View file

@ -8,7 +8,7 @@ defmodule Ash.Type.Binary do
use Ash.Type
@impl true
def storage_type, do: :binary
def storage_type(_), do: :binary
@impl true
def generator(_constraints) do

View file

@ -7,7 +7,7 @@ defmodule Ash.Type.Boolean do
use Ash.Type
@impl true
def storage_type, do: :boolean
def storage_type(_), do: :boolean
@impl true
def generator(_constraints) do

View file

@ -51,7 +51,7 @@ defmodule Ash.Type.CiString do
use Ash.Type
@impl true
def storage_type, do: :string
def storage_type(_), do: :string
@impl true
def constraints, do: @constraints

View file

@ -7,7 +7,7 @@ defmodule Ash.Type.Date do
use Ash.Type
@impl true
def storage_type, do: :date
def storage_type(_), do: :date
@impl true
def generator(_constraints) do

View file

@ -58,7 +58,7 @@ defmodule Ash.Type.Decimal do
end
@impl true
def storage_type, do: :decimal
def storage_type(_), do: :decimal
@impl true
def constraints, do: @constraints

View file

@ -46,7 +46,7 @@ defmodule Ash.Type.Enum do
def values, do: @values
@impl Ash.Type
def storage_type, do: :string
def storage_type(_), do: :string
@impl Ash.Type
def generator(_constraints) do
@ -120,7 +120,7 @@ defmodule Ash.Type.Enum do
:error
end
defoverridable storage_type: 0
defoverridable storage_type: 1
end
end
end

View file

@ -22,7 +22,7 @@ defmodule Ash.Type.Float do
use Ash.Type
@impl true
def storage_type, do: :float
def storage_type(_), do: :float
@impl true
def generator(constraints) do

View file

@ -19,7 +19,7 @@ defmodule Ash.Type.Function do
]
@impl true
def storage_type, do: :binary
def storage_type(_), do: :binary
@impl true
def constraints, do: @constraints

View file

@ -21,7 +21,7 @@ defmodule Ash.Type.Integer do
use Ash.Type
@impl true
def storage_type, do: :integer
def storage_type(_), do: :integer
@impl true
def generator(constraints) do

View file

@ -67,7 +67,7 @@ defmodule Ash.Type.Keyword do
def constraints, do: @constraints
@impl true
def storage_type, do: :map
def storage_type(_), do: :map
@impl true
def cast_input("", _), do: {:ok, nil}

View file

@ -68,7 +68,7 @@ defmodule Ash.Type.Map do
def constraints, do: @constraints
@impl true
def storage_type, do: :map
def storage_type(_), do: :map
@impl true
def cast_input("", _), do: {:ok, nil}

View file

@ -22,7 +22,7 @@ defmodule Ash.Type.Module do
use Ash.Type
@impl true
def storage_type, do: :string
def storage_type(_), do: :string
@impl true
def constraints, do: @constraints

View file

@ -7,7 +7,7 @@ defmodule Ash.Type.NaiveDatetime do
use Ash.Type
@impl true
def storage_type, do: :naive_datetime
def storage_type(_), do: :naive_datetime
@impl true
def generator(_constraints) do

View file

@ -256,8 +256,8 @@ defmodule Ash.Type.NewType do
end
@impl Ash.Type
def storage_type do
unquote(subtype_of).storage_type()
def storage_type(constraints) do
unquote(subtype_of).storage_type(constraints)
end
@impl Ash.Type

View file

@ -39,7 +39,7 @@ defmodule Ash.Type.String do
use Ash.Type
@impl true
def storage_type, do: :string
def storage_type(_), do: :string
@impl true
def constraints, do: @constraints

View file

@ -19,7 +19,7 @@ defmodule Ash.Type.Struct do
def constraints, do: @constraints
@impl true
def storage_type, do: :map
def storage_type(_), do: :map
@impl true
def cast_input(nil, _), do: {:ok, nil}

View file

@ -7,7 +7,7 @@ defmodule Ash.Type.Term do
use Ash.Type
@impl true
def storage_type, do: :binary
def storage_type(_), do: :binary
@impl true
def cast_input(value, _), do: {:ok, value}

View file

@ -7,7 +7,7 @@ defmodule Ash.Type.Time do
use Ash.Type
@impl true
def storage_type, do: :time
def storage_type(_), do: :time
@impl true
def generator(_constraints) do

View file

@ -97,7 +97,7 @@ defmodule Ash.Type do
use Ash.Type
@impl Ash.Type
def storage_type, do: :float
def storage_type(_), do: :float
@impl Ash.Type
def cast_input(value, _) do
@ -147,7 +147,7 @@ defmodule Ash.Type do
authorize?: boolean | nil
}
@callback storage_type() :: Ecto.Type.t()
@callback storage_type(constraints) :: Ecto.Type.t()
@doc """
Useful for typed data layers (like ash_postgres) to instruct them not to attempt to cast input values.
@ -354,8 +354,9 @@ defmodule Ash.Type do
Returns the *underlying* storage type (the underlying type of the *ecto type* of the *ash type*)
"""
@spec storage_type(t()) :: Ecto.Type.t()
def storage_type({:array, type}), do: {:array, type.storage_type()}
def storage_type(type), do: type.storage_type()
def storage_type(type, constraints \\ [])
def storage_type({:array, type}, constraints), do: {:array, storage_type(type, constraints)}
def storage_type(type, constraints), do: type.storage_type(constraints)
@doc """
Returns the ecto compatible type for an Ash.Type.
@ -919,8 +920,8 @@ defmodule Ash.Type do
end
@impl true
def type(_) do
@parent.storage_type()
def type(constraints) do
@parent.storage_type(constraints)
end
@impl true
@ -1104,6 +1105,18 @@ defmodule Ash.Type do
def equal?(left, right), do: left == right
end
cond do
Module.defines?(__MODULE__, {:storage_type, 0}) &&
Module.defines?(__MODULE__, {:storage_type, 1}) ->
raise "Must only define storage_type/0 or storage_type/1 but not both"
Module.defines?(__MODULE__, {:storage_type, 0}) ->
def storage_type(_constraints), do: storage_type()
true ->
:ok
end
unless Module.defines?(__MODULE__, {:can_load?, 1}, :def) do
@impl Ash.Type
if Module.defines?(__MODULE__, {:load, 4}, :def) do

View file

@ -109,7 +109,7 @@ defmodule Ash.Type.Union do
## Find the minimal supported type?
@impl true
def storage_type, do: :map
def storage_type(_), do: :map
@impl true
def load(unions, load, constraints, context) do

View file

@ -8,7 +8,7 @@ defmodule Ash.Type.UrlEncodedBinary do
use Ash.Type
@impl true
def storage_type, do: :binary
def storage_type(_), do: :binary
@impl true
def generator(_constraints) do

View file

@ -9,7 +9,7 @@ defmodule Ash.Type.UtcDatetime do
@beginning_of_day Time.new!(0, 0, 0)
@impl true
def storage_type, do: :utc_datetime
def storage_type(_), do: :utc_datetime
@impl true
def generator(_constraints) do

View file

@ -9,7 +9,7 @@ defmodule Ash.Type.UtcDatetimeUsec do
@beginning_of_day Time.new!(0, 0, 0)
@impl true
def storage_type, do: :utc_datetime_usec
def storage_type(_), do: :utc_datetime_usec
@impl true
def generator(_constraints) do

View file

@ -8,7 +8,7 @@ defmodule Ash.Type.UUID do
use Ash.Type
@impl true
def storage_type, do: :uuid
def storage_type(_), do: :uuid
@impl true
def generator(_constraints) do

View file

@ -7,7 +7,7 @@ defmodule Ash.Test.Type.TypeTest do
@moduledoc false
use Ash.Type
def storage_type, do: :string
def storage_type(_), do: :string
def constraints do
[