Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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