@@ -11,7 +11,7 @@ defmodule AtomicMap do
1111
1212 def convert ( struct = % { __struct__: type } , opts = % AtomicMap.Opts { } ) do
1313 struct
14- |> Map . from_struct
14+ |> Map . from_struct ( )
1515 |> convert ( opts )
1616 |> Map . put ( :__struct__ , type )
1717 end
@@ -26,7 +26,7 @@ defmodule AtomicMap do
2626 list |> Enum . map ( fn ( x ) -> convert ( x , opts ) end )
2727 end
2828 def convert ( tuple , opts = % AtomicMap.Opts { } ) when is_tuple ( tuple ) do
29- tuple |> Tuple . to_list |> convert ( opts ) |> List . to_tuple
29+ tuple |> Tuple . to_list |> convert ( opts ) |> List . to_tuple ( )
3030 end
3131 def convert ( v , _opts = % AtomicMap.Opts { } ) , do: v
3232
@@ -40,17 +40,23 @@ defmodule AtomicMap do
4040 |> as_atom ( opts . safe )
4141 end
4242
43- defp as_atom ( s , true ) when is_binary ( s ) , do: s |> String . to_existing_atom
44- defp as_atom ( s , false ) when is_binary ( s ) , do: s |> String . to_atom
43+ defp as_atom ( s , true ) when is_binary ( s ) do
44+ try do
45+ s |> String . to_existing_atom ( )
46+ rescue
47+ ArgumentError -> s
48+ end
49+ end
50+ defp as_atom ( s , false ) when is_binary ( s ) , do: s |> String . to_atom ( )
4551 defp as_atom ( s , _ ) , do: s
4652
47- defp as_underscore ( s , true ) when is_binary ( s ) , do: s |> do_undescore
48- defp as_underscore ( s , true ) when is_atom ( s ) , do: s |> Atom . to_string |> as_underscore ( true )
53+ defp as_underscore ( s , true ) when is_binary ( s ) , do: s |> do_underscore ( )
54+ defp as_underscore ( s , true ) when is_atom ( s ) , do: s |> Atom . to_string ( ) |> as_underscore ( true )
4955 defp as_underscore ( s , false ) , do: s
5056
51- defp do_undescore ( s ) do
57+ defp do_underscore ( s ) do
5258 s
53- |> Macro . underscore
59+ |> Macro . underscore ( )
5460 |> String . replace ( ~r/ -/ , "_" )
5561 end
5662end
0 commit comments