Skip to content

Commit 0339c55

Browse files
authored
Merge pull request #6 from redwirelabs/defect/5-maplist
Fix issue with coercing a list of maps with a single attribute
2 parents 2310bea + dae3500 commit 0339c55

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

lib/mix/tasks/compiler.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ defmodule Mix.Tasks.Compile.Speck do
152152
{name, [:map], [], Enum.map(attributes_ast, &build_attribute/1)}
153153
end
154154

155+
defp build_attribute({:attribute, _, [[name], [do: attributes_ast]]}) do
156+
{name, [:map], [], [build_attribute(attributes_ast)]}
157+
end
158+
155159
defp build_attribute({:attribute, _, [name, opts_ast, [do: {:__block__, _, attributes_ast}]]}) do
156160
{opts, _} = Code.eval_quoted(opts_ast)
157161
{name, :map, opts, Enum.map(attributes_ast, &build_attribute/1)}

protocol/test/map_list_single.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct TestSchema.MapListSingleAttribute
2+
3+
name "map_list_single_attribute"
4+
5+
attribute [:devices] do
6+
attribute :type, :string
7+
end

test/speck_test.exs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,26 @@ defmodule Speck.Test do
137137
}}
138138
end
139139

140-
test "can coerce a list of maps" do
140+
test "can coerce a list of maps with a single attribute" do
141+
params = %{
142+
"devices" => [
143+
%{"type" => "imx6"},
144+
%{"type" => "imx8"},
145+
%{"type" => "am62"},
146+
]
147+
}
148+
149+
assert Speck.validate(TestSchema.MapListSingleAttribute, params) ==
150+
{:ok, %TestSchema.MapListSingleAttribute{
151+
devices: [
152+
%{type: "imx6"},
153+
%{type: "imx8"},
154+
%{type: "am62"},
155+
]
156+
}}
157+
end
158+
159+
test "can coerce a list of maps with multiple attributes" do
141160
params = %{
142161
"devices" => [
143162
%{"id" => 1, "type" => "valid"},

0 commit comments

Comments
 (0)