-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
struct.ex
48 lines (36 loc) · 908 Bytes
/
struct.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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