Packages
reactor
1.0.1
1.0.2
1.0.1
1.0.0
0.17.0
0.16.0
0.15.6
0.15.5
0.15.4
0.15.3
0.15.2
0.15.1
0.15.0
0.14.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.10.3
0.10.2
0.10.1
0.10.0
0.9.1
0.9.0
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.0
0.6.0
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.0
An asynchronous, graph-based execution engine
Current section
Files
Jump to
Current section
Files
lib/reactor/info.ex
# SPDX-FileCopyrightText: 2023 James Harton, Zach Daniel, Alembic Pty and contributors
# SPDX-FileCopyrightText: 2023 reactor contributors <https://github.com/ash-project/reactor/graphs/contributors>
#
# SPDX-License-Identifier: MIT
defmodule Reactor.Info do
@moduledoc """
Introspection for the Reactor DSL.
"""
use Spark.InfoGenerator, sections: [:reactor], extension: Reactor.Dsl
alias Reactor.{Builder, Dsl}
alias Spark.Dsl.Extension
import Reactor.Utils
@doc """
Convert a reactor DSL module into a reactor struct.
"""
@spec to_struct(module | Reactor.t() | Spark.Dsl.t()) :: {:ok, Reactor.t()} | {:error, any}
def to_struct(reactor) when is_struct(reactor, Reactor), do: {:ok, reactor}
def to_struct(module) do
with {:ok, reactor} <- entities_to_struct(module),
{:ok, reactor} <- maybe_set_return(module, reactor),
{:ok, reactor} <- maybe_set_description(module, reactor) do
add_middleware(module, reactor)
end
end
@doc """
Raising version of `to_struct/1`.
"""
@spec to_struct!(module | Reactor.t() | Spark.Dsl.t()) :: Reactor.t() | no_return
def to_struct!(reactor) do
case to_struct(reactor) do
{:ok, reactor} -> reactor
{:error, reason} -> raise reason
end
end
defp entities_to_struct(module) when is_atom(module) do
module
|> reactor()
|> reduce_while_ok(Builder.new(module), &Dsl.Build.build/2)
end
defp entities_to_struct(dsl_state) when is_map(dsl_state) do
module = Extension.get_persisted(dsl_state, :module)
dsl_state
|> reactor()
|> reduce_while_ok(Builder.new(module), &Dsl.Build.build/2)
end
defp maybe_set_return(module, reactor) do
case reactor_return(module) do
{:ok, value} -> {:ok, %{reactor | return: value}}
:error -> {:ok, reactor}
end
end
defp maybe_set_description(module, reactor) do
case reactor_description(module) do
{:ok, value} when byte_size(value) > 0 -> {:ok, %{reactor | description: value}}
_ -> {:ok, reactor}
end
end
defp add_middleware(module, reactor) do
module
|> reactor_middlewares()
|> reduce_while_ok(reactor, &Dsl.Build.build/2)
end
end