-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
action.ex
97 lines (84 loc) · 2.73 KB
/
action.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
defmodule Ash.Resource.Actions.Action do
@moduledoc "Represents a custom action on a resource."
defstruct [
:name,
:description,
:returns,
:run,
constraints: [],
touches_resources: [],
arguments: [],
allow_nil?: false,
transaction?: false,
primary?: false,
type: :action
]
@type t :: %__MODULE__{
type: :action,
name: atom,
description: String.t() | nil,
arguments: [Ash.Resource.Actions.Argument.t()],
allow_nil?: boolean,
touches_resources: [Ash.Resource.t()],
constraints: Keyword.t(),
run: {module, Keyword.t()},
returns: Ash.Type.t(),
primary?: boolean,
transaction?: boolean
}
import Ash.Resource.Actions.SharedOptions
def transform(%{returns: original_type, constraints: constraints} = thing) do
type = Ash.Type.get_type(original_type)
ash_type? =
try do
Ash.Type.ash_type?(type)
rescue
_ ->
false
end
unless ash_type? do
raise """
#{inspect(original_type)} is not a valid type.
Valid types include any custom types, or the following short codes (alongside the types they map to):
#{Enum.map_join(Ash.Type.short_names(), "\n", fn {name, type} -> " #{inspect(name)} -> #{inspect(type)}" end)}
"""
end
case Ash.Type.validate_constraints(type, constraints) do
{:ok, constraints} ->
{:ok, %{thing | returns: type, constraints: constraints}}
{:error, error} ->
{:error, error}
end
end
@global_opts shared_options()
@opt_schema [
returns: [
type: Ash.OptionsHelpers.ash_type(),
doc: "The return type of the action. See `Ash.Type` for more."
],
constraints: [
type: :keyword_list,
doc: """
Constraints for the return type. See the [constriants topic](/documentation/topics/constraints.md) for more.
"""
],
allow_nil?: [
type: :boolean,
default: false,
doc: """
Whether or not the action can return nil. Unlike attributes & arguments, this defaults to `false`.
"""
],
run: [
type:
{:spark_function_behaviour, Ash.Resource.Actions.Implementation,
{Ash.Resource.Action.ImplementationFunction, 2}}
]
]
|> Spark.OptionsHelpers.merge_schemas(
@global_opts,
"Action Options"
)
@doc false
def opt_schema, do: @opt_schema
end