Skip to content

Commit

Permalink
handle binary in JsonArrayMap type
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp committed Nov 9, 2023
1 parent afeda28 commit d7f34ea
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
37 changes: 36 additions & 1 deletion lib/beacon/types/json_array_map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
15 changes: 11 additions & 4 deletions test/beacon/types/json_array_map_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d7f34ea

Please sign in to comment.