-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
inputs.ex
79 lines (72 loc) · 2.06 KB
/
inputs.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
defmodule Ash.Reactor.Dsl.Inputs do
@moduledoc """
The `inputs` entity for the `Ash.Reactor` reactor extension.
"""
defstruct __identifier__: nil,
template: %{},
transform: nil
@type t :: %__MODULE__{
__identifier__: any,
template:
%{optional(atom) => Reactor.Template.t()}
| Keyword.t(Reactor.Template.t()),
transform: nil | (any -> any) | {module, keyword} | mfa
}
@doc false
@spec __entity__ :: Spark.Dsl.Entity.t()
def __entity__ do
template_type = Reactor.Template.type()
input_template_type =
{:or,
[
{:map, :atom, template_type},
{:keyword_list, [*: [type: template_type]]}
]}
%Spark.Dsl.Entity{
name: :inputs,
describe: "Specify the inputs for an action",
docs: """
Used to build a map for input into an action.
You can provide the template value as either a map or keyword list and
multiple instances of `inputs` will be merged together.
""",
examples: [
"""
inputs %{
author: result(:get_user),
title: input(:title),
body: input(:body)
}
""",
"""
inputs(author: result(:get_user))
"""
],
target: __MODULE__,
args: [:template],
identifier: {:auto, :unique_integer},
imports: [Reactor.Dsl.Argument],
schema: [
template: [
type: input_template_type,
required: true
],
transform: [
type:
{:or,
[{:spark_function_behaviour, Reactor.Step, {Reactor.Step.TransformAll, 1}}, nil]},
required: false,
default: nil,
doc:
"An optional transformation function which will transform the inputs before executing the action."
]
],
transform: {__MODULE__, :__transform__, []}
}
end
@doc false
@spec __transform__(t) :: {:ok, t} | {:error, any}
def __transform__(entity) do
{:ok, %{entity | template: Map.new(entity.template)}}
end
end