Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/ash/type/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,10 @@ defmodule Ash.Type do
"""
@spec storage_type(t()) :: Ecto.Type.t()
def storage_type(type, constraints \\ [])
def storage_type({:array, type}, constraints), do: {:array, storage_type(type, constraints)}

def storage_type({:array, type}, constraints),
do: {:array, storage_type(type, item_constraints(constraints))}

def storage_type(type, constraints), do: type.storage_type(constraints)

@doc """
Expand Down
19 changes: 19 additions & 0 deletions test/type/type_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ defmodule Ash.Test.Type.TypeTest do

alias Ash.Test.Domain, as: Domain

defmodule StorageByConstraint do
@moduledoc false
use Ash.Type

def storage_type(constraints), do: if(constraints[:as_binary], do: :binary, else: :string)

def cast_input(value, _), do: {:ok, value}
def cast_stored(value, _), do: {:ok, value}
def dump_to_native(value, _), do: {:ok, value}
end

defmodule PostTitle do
@moduledoc false
use Ash.Type
Expand Down Expand Up @@ -91,6 +102,14 @@ defmodule Ash.Test.Type.TypeTest do
end)
end

test "storage_type/2 forwards item constraints to inner type for array types" do
assert {:array, :binary} =
Ash.Type.storage_type({:array, StorageByConstraint}, items: [as_binary: true])

assert {:array, :string} =
Ash.Type.storage_type({:array, StorageByConstraint}, [])
end

test "returns error for invalid keys" do
foo = {:foo, [type: :string]}
bar = {:bar, [type: :integer]}
Expand Down
Loading