Skip to content

Commit 93c527d

Browse files
authored
fix: Verify Metadata Struct Fields after Compilation (#217)
1 parent c869b86 commit 93c527d

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

lib/spark/dsl/extension.ex

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ defmodule Spark.Dsl.Extension do
406406
@_dsl_patches dsl_patches
407407
@_imports imports
408408
@_add_extensions add_extensions
409+
@after_verify Spark.Dsl.Extension
409410

410411
@doc false
411412
def sections, do: set_docs(@_sections)
@@ -855,22 +856,23 @@ defmodule Spark.Dsl.Extension do
855856
end)
856857
end)
857858

858-
Enum.each(dsl_patches, fn %Spark.Dsl.Patch.AddEntity{
859-
section_path: section_path,
860-
entity: entity
861-
} ->
862-
Spark.Dsl.Extension.async_compile(agent_and_pid, fn ->
863-
Extension.build_entity(
864-
agent_and_pid,
865-
module_prefix,
866-
extension,
867-
section_path,
868-
entity,
869-
[],
870-
[],
871-
[]
872-
)
873-
end)
859+
Enum.each(dsl_patches, fn
860+
%Spark.Dsl.Patch.AddEntity{
861+
section_path: section_path,
862+
entity: entity
863+
} ->
864+
Spark.Dsl.Extension.async_compile(agent_and_pid, fn ->
865+
Extension.build_entity(
866+
agent_and_pid,
867+
module_prefix,
868+
extension,
869+
section_path,
870+
entity,
871+
[],
872+
[],
873+
[]
874+
)
875+
end)
874876
end)
875877

876878
Spark.Dsl.Extension.await_all_tasks(agent)
@@ -1269,15 +1271,6 @@ defmodule Spark.Dsl.Extension do
12691271
nested_key: nested_key,
12701272
mod: mod
12711273
] do
1272-
if !Map.has_key?(entity.target.__struct__(), :__spark_metadata__) do
1273-
Spark.Warning.warn_deprecated(
1274-
"Entity without __spark_metadata__ field",
1275-
"Entity #{inspect(entity.target)} does not define a `__spark_metadata__` field. " <>
1276-
"This field is required to access source annotations. " <>
1277-
"Add `__spark_metadata__: nil` to the defstruct for #{inspect(entity.target)}."
1278-
)
1279-
end
1280-
12811274
def __build__(module, opts, nested_entities, anno, opts_anno) do
12821275
case Spark.Dsl.Entity.build(
12831276
unquote(Macro.escape(entity)),
@@ -2128,4 +2121,33 @@ defmodule Spark.Dsl.Extension do
21282121
else
21292122
defp maybe_set_end_location(anno, _do_block), do: anno
21302123
end
2124+
2125+
def __after_verify__(module) do
2126+
dsl_patch_structs =
2127+
for %Spark.Dsl.Patch.AddEntity{entity: entity} <- module.dsl_patches(), do: entity.target
2128+
2129+
section_structs =
2130+
for section <- module.sections(), entity <- section_entities(section), do: entity.target
2131+
2132+
(dsl_patch_structs ++ section_structs)
2133+
|> Enum.uniq()
2134+
|> Enum.reject(&Map.has_key?(&1.__struct__(), :__spark_metadata__))
2135+
|> Enum.each(
2136+
&Spark.Warning.warn_deprecated(
2137+
"Entity without __spark_metadata__ field",
2138+
"Entity #{inspect(&1)} does not define a `__spark_metadata__` field. " <>
2139+
"This field is required to access source annotations. " <>
2140+
"Add `__spark_metadata__: nil` to the defstruct for #{inspect(&1)}."
2141+
)
2142+
)
2143+
2144+
:ok
2145+
end
2146+
2147+
defp section_entities(%Spark.Dsl.Section{
2148+
entities: entities,
2149+
sections: sections
2150+
}) do
2151+
entities ++ Enum.flat_map(sections, &section_entities/1)
2152+
end
21312153
end

0 commit comments

Comments
 (0)