Skip to content

Commit a937051

Browse files
committed
Add struct support as the builder entries
1 parent b77ed4d commit a937051

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lib/macros/guarded_struct/derive/parser.ex

+6
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ defmodule MishkaDeveloperTools.Macros.GuardedStruct.Derive.Parser do
107107

108108
def convert_to_atom_map({:ok, map}) when is_map(map), do: convert_to_atom_map(map)
109109

110+
def convert_to_atom_map(map) when is_struct(map) do
111+
for {key, value} <- Map.from_struct(map),
112+
into: %{},
113+
do: {convert_key(key), convert_value(value)}
114+
end
115+
110116
def convert_to_atom_map(map) when is_map(map) do
111117
for {key, value} <- map, into: %{}, do: {convert_key(key), convert_value(value)}
112118
end

lib/macros/guarded_struct/guarded_struct.ex

+11-3
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,10 @@ defmodule GuardedStruct do
13171317
def builder(attrs, error \\ false)
13181318

13191319
def builder({key, attrs} = input, error)
1320-
when is_tuple(input) and is_map(attrs) and (is_list(key) or is_atom(key)) do
1320+
when is_tuple(input) and (is_map(attrs) or is_struct(attrs)) and
1321+
(is_list(key) or is_atom(key)) do
1322+
attrs = if(is_struct(attrs), do: Map.from_struct(attrs), else: attrs)
1323+
13211324
GuardedStruct.builder(
13221325
%{attrs: attrs, module: unquote(module), revaluation: unquote(escaped_list)},
13231326
key,
@@ -1327,7 +1330,10 @@ defmodule GuardedStruct do
13271330
end
13281331

13291332
def builder({key, attrs, type} = input, error)
1330-
when is_tuple(input) and is_map(attrs) and (is_list(key) or is_atom(key)) do
1333+
when is_tuple(input) and (is_map(attrs) or is_struct(attrs)) and
1334+
(is_list(key) or is_atom(key)) do
1335+
attrs = if(is_struct(attrs), do: Map.from_struct(attrs), else: attrs)
1336+
13311337
GuardedStruct.builder(
13321338
%{attrs: attrs, module: unquote(module), revaluation: unquote(escaped_list)},
13331339
key,
@@ -1336,7 +1342,9 @@ defmodule GuardedStruct do
13361342
)
13371343
end
13381344

1339-
def builder(attrs, error) when is_map(attrs) do
1345+
def builder(attrs, error) when is_map(attrs) or is_struct(attrs) do
1346+
attrs = if(is_struct(attrs), do: Map.from_struct(attrs), else: attrs)
1347+
13401348
GuardedStruct.builder(
13411349
%{attrs: attrs, module: unquote(module), revaluation: unquote(escaped_list)},
13421350
:root,

0 commit comments

Comments
 (0)