diff --git a/lib/beacon/types/json_array_map.ex b/lib/beacon/types/json_array_map.ex index 334bd2c2..0c10f0bc 100644 --- a/lib/beacon/types/json_array_map.ex +++ b/lib/beacon/types/json_array_map.ex @@ -5,7 +5,7 @@ defmodule Beacon.Types.JsonArrayMap do use Ecto.Type - def type, do: :mine + def type, do: {:array, :map} def cast(term) when is_map(term), do: {:ok, [term]} @@ -19,6 +19,13 @@ defmodule Beacon.Types.JsonArrayMap do end end + def cast(term) when is_binary(term) do + case decode(term) do + {:ok, term} -> cast(term) + {:error, message} -> {:error, message: message} + end + end + def cast(term) do {:error, message: "expected a list of map or a map, got: #{inspect(term)}"} end @@ -35,9 +42,26 @@ defmodule Beacon.Types.JsonArrayMap do end end + def dump(term) when is_binary(term) do + case decode(term) do + {:ok, term} -> dump(term) + {:error, _message} -> :error + end + end + def dump(_site), do: :error + def load(term) when is_map(term), do: {:ok, [term]} + def load(term) when is_list(term), do: {:ok, term} + + def load(term) when is_binary(term) do + case decode(term) do + {:ok, term} -> load(term) + {:error, _message} -> :error + end + end + def load(_term), do: :error defp validate(term) when is_list(term) do @@ -54,4 +78,15 @@ defmodule Beacon.Types.JsonArrayMap do end defp validate(term), do: {false, term} + + defp decode(term) when is_binary(term) do + case Jason.decode(term) do + {:ok, term} -> + {:ok, term} + + {:error, error} -> + message = Exception.message(error) + {:error, "expected a list of map or a map, got error: #{message}"} + end + end end diff --git a/test/beacon/types/json_array_map_test.exs b/test/beacon/types/json_array_map_test.exs index 059fb4e0..34ecb327 100644 --- a/test/beacon/types/json_array_map_test.exs +++ b/test/beacon/types/json_array_map_test.exs @@ -3,26 +3,33 @@ defmodule Beacon.Types.JsonArrayMapTest do alias Beacon.Types.JsonArrayMap - @map %{"foo" => :bar} + @map %{"foo" => "bar"} test "cast" do + assert JsonArrayMap.cast([]) == {:ok, []} assert JsonArrayMap.cast(@map) == {:ok, [@map]} assert JsonArrayMap.cast([@map]) == {:ok, [@map]} - assert JsonArrayMap.cast([]) == {:ok, []} + assert JsonArrayMap.cast(~s|[{"foo": "bar"}]|) == {:ok, [@map]} assert JsonArrayMap.cast(nil) == {:error, [{:message, "expected a list of map or a map, got: nil"}]} assert JsonArrayMap.cast([1]) == {:error, [{:message, "expected a list of map or a map, got: [1]"}]} + assert JsonArrayMap.cast("") == {:error, [message: "expected a list of map or a map, got error: unexpected end of input at position 0"]} end test "dump" do + assert JsonArrayMap.dump([]) == {:ok, []} assert JsonArrayMap.dump(@map) == {:ok, [@map]} assert JsonArrayMap.dump([@map]) == {:ok, [@map]} - assert JsonArrayMap.dump([]) == {:ok, []} + assert JsonArrayMap.dump(~s|[{"foo": "bar"}]|) == {:ok, [@map]} assert JsonArrayMap.dump(nil) == :error assert JsonArrayMap.dump([1]) == :error + assert JsonArrayMap.dump("") == :error end test "load" do + assert JsonArrayMap.load(@map) == {:ok, [@map]} assert JsonArrayMap.load([@map]) == {:ok, [@map]} - assert JsonArrayMap.load(@map) == :error + assert JsonArrayMap.load(~s|[{"foo": "bar"}]|) == {:ok, [@map]} + assert JsonArrayMap.load(nil) == :error + assert JsonArrayMap.load("") == :error end end