mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 13:33:20 +12:00
improvement: add warnings to DSL transformer returns
improvement: warn on empty registries closes #359
This commit is contained in:
parent
d7d7c58b87
commit
c0404af740
5 changed files with 49 additions and 5 deletions
|
@ -169,7 +169,8 @@ defmodule Ash.Dsl do
|
|||
|> Ash.Dsl.Extension.run_transformers(
|
||||
transformers_to_run,
|
||||
Module.get_attribute(__MODULE__, :ash_dsl_config),
|
||||
false
|
||||
false,
|
||||
__ENV__
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -479,12 +479,13 @@ defmodule Ash.Dsl.Extension do
|
|||
|> Ash.Dsl.Extension.run_transformers(
|
||||
transformers_to_run,
|
||||
ash_dsl_config,
|
||||
true
|
||||
true,
|
||||
__ENV__
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def run_transformers(mod, transformers, ash_dsl_config, store?) do
|
||||
def run_transformers(mod, transformers, ash_dsl_config, store?, env) do
|
||||
Enum.reduce_while(transformers, ash_dsl_config, fn transformer, dsl ->
|
||||
result =
|
||||
try do
|
||||
|
@ -506,6 +507,17 @@ defmodule Ash.Dsl.Extension do
|
|||
:halt ->
|
||||
{:halt, dsl}
|
||||
|
||||
{:warn, new_dsl, warnings} ->
|
||||
warnings
|
||||
|> List.wrap()
|
||||
|> Enum.each(&IO.warn(&1, Macro.Env.stacktrace(env)))
|
||||
|
||||
if store? do
|
||||
Module.put_attribute(mod, :ash_dsl_config, new_dsl)
|
||||
end
|
||||
|
||||
{:cont, new_dsl}
|
||||
|
||||
{:ok, new_dsl} ->
|
||||
if store? do
|
||||
Module.put_attribute(mod, :ash_dsl_config, new_dsl)
|
||||
|
|
|
@ -14,7 +14,12 @@ defmodule Ash.Dsl.Transformer do
|
|||
point in returning a new dsl structure from `transform/2` if `after_compile/0` is defined. Instead,
|
||||
simply return `:ok` or `{:error, error}`
|
||||
"""
|
||||
@callback transform(module, map) :: :ok | {:ok, map} | {:error, term} | :halt
|
||||
@callback transform(module, map) ::
|
||||
:ok
|
||||
| {:ok, map}
|
||||
| {:error, term}
|
||||
| {:warn, map, String.t() | list(String.t())}
|
||||
| :halt
|
||||
@callback before?(module) :: boolean
|
||||
@callback after?(module) :: boolean
|
||||
@callback after_compile?() :: boolean
|
||||
|
|
|
@ -30,14 +30,27 @@ defmodule Ash.Registry.Dsl do
|
|||
],
|
||||
entities: [
|
||||
@entry
|
||||
],
|
||||
schema: [
|
||||
warn_on_empty?: [
|
||||
type: :boolean,
|
||||
doc: "Set to `false` to ignore warnings about an empty registry",
|
||||
default: true
|
||||
]
|
||||
]
|
||||
}
|
||||
|
||||
@sections [@entries]
|
||||
|
||||
@transformers [Ash.Registry.Transformers.WarnOnEmpty]
|
||||
|
||||
@moduledoc """
|
||||
A small DSL for declaring an `Ash.Registry`.
|
||||
"""
|
||||
|
||||
use Ash.Dsl.Extension, sections: @sections
|
||||
use Ash.Dsl.Extension, sections: @sections, transformers: @transformers
|
||||
|
||||
def warn_on_empty?(registry) do
|
||||
Extension.get_opt(registry, [:entries], :warn_on_empty?, false, true)
|
||||
end
|
||||
end
|
||||
|
|
13
lib/ash/registry/transformers/warn_on_empty.ex
Normal file
13
lib/ash/registry/transformers/warn_on_empty.ex
Normal file
|
@ -0,0 +1,13 @@
|
|||
defmodule Ash.Registry.Transformers.WarnOnEmpty do
|
||||
use Ash.Dsl.Transformer
|
||||
|
||||
def transform(registry, dsl) do
|
||||
case Ash.Registry.entries(registry) do
|
||||
[] ->
|
||||
{:warn, dsl, "#{inspect(registry)} has no entries."}
|
||||
|
||||
_ ->
|
||||
{:ok, dsl}
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue