mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 13:33:20 +12:00
improvement: add :struct
(Ash.Type.struct
) type
This commit is contained in:
parent
c73630097c
commit
b94a57d1bd
3 changed files with 50 additions and 1 deletions
48
lib/ash/type/struct.ex
Normal file
48
lib/ash/type/struct.ex
Normal file
|
@ -0,0 +1,48 @@
|
|||
defmodule Ash.Type.Struct do
|
||||
@moduledoc """
|
||||
Represents a struct.
|
||||
|
||||
This cannot be loaded from a database, it can only be used to cast input.
|
||||
|
||||
Use the `instance_of` constraint to specify that it must be an instance of a specific struct.
|
||||
"""
|
||||
use Ash.Type
|
||||
|
||||
@constraints [
|
||||
instance_of: [
|
||||
type: :atom,
|
||||
doc: "The module the struct should be an instance of",
|
||||
default: false
|
||||
]
|
||||
]
|
||||
|
||||
@impl true
|
||||
def constraints, do: @constraints
|
||||
|
||||
@impl true
|
||||
def storage_type, do: :map
|
||||
|
||||
@impl true
|
||||
def cast_input(nil, _), do: {:ok, nil}
|
||||
|
||||
def cast_input(%struct{} = value, constraints) do
|
||||
case constraints[:instance_of] do
|
||||
nil ->
|
||||
{:ok, value}
|
||||
|
||||
^struct ->
|
||||
{:ok, value}
|
||||
|
||||
_ ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
def cast_input(_, _), do: :error
|
||||
|
||||
@impl true
|
||||
def cast_stored(_, _), do: :error
|
||||
|
||||
@impl true
|
||||
def dump_to_native(_, _), do: :error
|
||||
end
|
|
@ -25,6 +25,7 @@ defmodule Ash.Type do
|
|||
duration_name: Ash.Type.DurationName,
|
||||
function: Ash.Type.Function,
|
||||
boolean: Ash.Type.Boolean,
|
||||
struct: Ash.Type.Struct,
|
||||
uuid: Ash.Type.UUID,
|
||||
binary: Ash.Type.Binary,
|
||||
date: Ash.Type.Date,
|
||||
|
|
2
mix.exs
2
mix.exs
|
@ -220,7 +220,7 @@ defmodule Ash.MixProject do
|
|||
# Run "mix help deps" to learn about dependencies.
|
||||
defp deps do
|
||||
[
|
||||
{:spark, "~> 0.2 and >= 0.2.18"},
|
||||
{:spark, "~> 0.2.18"},
|
||||
{:ecto, "~> 3.7"},
|
||||
{:ets, "~> 0.8.0"},
|
||||
{:decimal, "~> 2.0"},
|
||||
|
|
Loading…
Reference in a new issue