Skip to content

Commit

Permalink
Fix issue with coercing a list of maps with a single attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
amclain committed Aug 20, 2024
1 parent 2310bea commit dae3500
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/mix/tasks/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ defmodule Mix.Tasks.Compile.Speck do
{name, [:map], [], Enum.map(attributes_ast, &build_attribute/1)}
end

defp build_attribute({:attribute, _, [[name], [do: attributes_ast]]}) do
{name, [:map], [], [build_attribute(attributes_ast)]}
end

defp build_attribute({:attribute, _, [name, opts_ast, [do: {:__block__, _, attributes_ast}]]}) do
{opts, _} = Code.eval_quoted(opts_ast)
{name, :map, opts, Enum.map(attributes_ast, &build_attribute/1)}
Expand Down
7 changes: 7 additions & 0 deletions protocol/test/map_list_single.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
struct TestSchema.MapListSingleAttribute

name "map_list_single_attribute"

attribute [:devices] do
attribute :type, :string
end
21 changes: 20 additions & 1 deletion test/speck_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,26 @@ defmodule Speck.Test do
}}
end

test "can coerce a list of maps" do
test "can coerce a list of maps with a single attribute" do
params = %{
"devices" => [
%{"type" => "imx6"},
%{"type" => "imx8"},
%{"type" => "am62"},
]
}

assert Speck.validate(TestSchema.MapListSingleAttribute, params) ==
{:ok, %TestSchema.MapListSingleAttribute{
devices: [
%{type: "imx6"},
%{type: "imx8"},
%{type: "am62"},
]
}}
end

test "can coerce a list of maps with multiple attributes" do
params = %{
"devices" => [
%{"id" => 1, "type" => "valid"},
Expand Down

0 comments on commit dae3500

Please sign in to comment.