Skip to content

Commit 23706c2

Browse files
committed
Ignore unsafe string-to-atoms
1 parent 3db7e30 commit 23706c2

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

lib/atomic_map.ex

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
5662
end

mix.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule AtomicMap.Mixfile do
55
def project do
66
[app: :atomic_map,
77
version: @version,
8-
elixir: "~> 1.2",
8+
elixir: "~> 1.4",
99
build_embedded: Mix.env == :prod,
1010
start_permanent: Mix.env == :prod,
1111
package: package(),
@@ -31,9 +31,9 @@ defmodule AtomicMap.Mixfile do
3131
# Type "mix help deps" for more examples and options
3232
defp deps do
3333
[
34-
{:earmark, "~> 0.2", only: :dev},
35-
{:ex_doc, "~> 0.11", only: :dev},
36-
{:benchfella, "0.3.2", only: :dev},
34+
{:earmark, "~> 1.1", only: :dev},
35+
{:ex_doc, "~> 0.14", only: :dev},
36+
{:benchfella, "~> 0.3", only: :dev},
3737
]
3838
end
3939

mix.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
%{"benchfella": {:hex, :benchfella, "0.3.2", "b9648e77fa8d8b8b9fe8f54293bee63f7de03909b3af6ab22a0e546716a396fb", [:mix], []},
2-
"earmark": {:hex, :earmark, "0.2.1", "ba6d26ceb16106d069b289df66751734802777a3cbb6787026dd800ffeb850f3", [:mix], []},
3-
"ex_doc": {:hex, :ex_doc, "0.11.4", "a064bdb720594c3745b94709b17ffb834fd858b4e0c1f48f37c0d92700759e02", [:mix], [{:earmark, "~> 0.1.17 or ~> 0.2", [hex: :earmark, optional: true]}]}}
2+
"earmark": {:hex, :earmark, "1.1.1", "433136b7f2e99cde88b745b3a0cfc3fbc81fe58b918a09b40fce7f00db4d8187", [:mix], []},
3+
"ex_doc": {:hex, :ex_doc, "0.15.0", "e73333785eef3488cf9144a6e847d3d647e67d02bd6fdac500687854dd5c599f", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, optional: false]}]}}

test/atomic_map_test.exs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ defmodule AtomicMapTest do
6060
assert AtomicMap.convert(input) == expected
6161
end
6262

63-
test "convertes keys to underscore by default (attention: safe: false needed here)" do
63+
test "converts keys to underscore by default (attention: safe: false needed here)" do
6464
input = %{ "firstKey" => {1,2}, :secondKey => 4}
6565
expected = %{first_key: {1, 2}, second_key: 4}
6666
assert AtomicMap.convert(input, safe: false) == expected
@@ -72,10 +72,9 @@ defmodule AtomicMapTest do
7272
assert AtomicMap.convert(input, safe: false) == expected
7373
end
7474

75-
test "raises for not existing atoms" do
76-
assert_raise ArgumentError, fn ->
77-
input = %{"a" => 2, "b" => %{"c" => 4}, "__not___existing__" => 5}
78-
AtomicMap.convert(input, safe: true)
79-
end
75+
test "skips not existing atoms" do
76+
input = %{"unknow_val" => 2}
77+
expected = %{"unknow_val" => 2}
78+
assert AtomicMap.convert(input, %{safe: true}) == expected
8079
end
8180
end

0 commit comments

Comments
 (0)