Current section

Files

Jump to
smokestack lib smokestack dsl info.ex
Raw

lib/smokestack/dsl/info.ex

defmodule Smokestack.Dsl.Info do
@moduledoc """
Introspection of Smokestack DSLs.
"""
alias Ash.Resource
alias Smokestack.Dsl.Factory
alias Spark.Dsl.Extension
@doc """
Retrieve a variant for a specific resource.
"""
@spec factory(Smokestack.t(), Resource.t(), atom) ::
{:ok, Factory.t()} | {:error, Exception.t()}
def factory(factory, resource, variant) do
factory
|> Extension.get_entities([:smokestack])
|> Enum.find(&(is_struct(&1, Factory) && &1.resource == resource && &1.variant == variant))
|> case do
nil ->
{:error,
ArgumentError.exception(
message: "Factory for `#{inspect(resource)}` variant `#{inspect(variant)}` not found."
)}
factory ->
{:ok, factory}
end
end
@doc "Raising version of `factory/3`"
def factory!(factory, resource, variant) do
case factory(factory, resource, variant) do
{:ok, factory} -> factory
{:error, reason} -> raise reason
end
end
end