@@ -45,10 +45,14 @@ defmodule DataMapper do
45
45
def map ( input ) do
46
46
input
47
47
|> pre_map_transform ( )
48
+ |> maybe_struct_to_map ( )
48
49
|> map ( unquote ( mappings ) )
49
50
|> post_map_transform ( )
50
51
end
51
52
53
+ def maybe_struct_to_map ( value ) when is_struct ( value ) , do: Map . from_struct ( value )
54
+ def maybe_struct_to_map ( value ) , do: value
55
+
52
56
## Callbacks
53
57
54
58
@ doc """
@@ -57,14 +61,18 @@ defmodule DataMapper do
57
61
@ impl true
58
62
@ spec map ( input :: map ( ) | keyword ( ) , mappings :: map ( ) ) :: map ( )
59
63
def map ( input , mappings ) do
60
- Enum . into ( mappings , % { } , fn
61
- # Handle a nested map.
62
- { from_key , sub_mappings } when is_map ( sub_mappings ) ->
63
- map ( input [ from_key ] , sub_mappings )
64
+ Enum . reduce ( mappings , % { } , fn
65
+ # # Handle a nested map.
66
+ # # This part needs to be re-worked. It's broken.
67
+ # {from_key, {to_key, sub_mappings}}, acc when is_map(sub_mappings) ->
68
+ # Map.put(acc, to_key, map(input[from_key], sub_mappings))
64
69
65
70
# The normal case, mapping the input key to the output key.
66
- { from_key , to_key } ->
67
- map_field ( { to_key , input [ from_key ] } )
71
+ { from_key , to_key } , acc ->
72
+ Map . put ( acc , to_key , map_field ( { from_key , input [ from_key ] } ) )
73
+
74
+ _ , acc ->
75
+ acc
68
76
end )
69
77
end
70
78
@@ -73,7 +81,7 @@ defmodule DataMapper do
73
81
"""
74
82
@ impl true
75
83
@ spec map_field ( { atom , any } ) :: { atom , any }
76
- def map_field ( field ) , do: field
84
+ def map_field ( { _field , value } ) , do: value
77
85
78
86
@ doc """
79
87
Transform the list before mapping.
0 commit comments