Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions lib/apex/format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,30 @@ defimpl Apex.Format, for: Map do
end

def format(data, options) do
Apex.Format.Seq.format(
Map.to_list(data),
options,
start_token: "\%{",
end_token: "}",
numbers: false) |> colorize(data, options)
end
ap_options = :apex
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably not do the config reading here. Doing it here would only apply it for the Map protocol implementation.

A better position would be in ap before the protocol invocation here.

Then I would also probably extract it into defp configuration(options).

|> Application.get_all_env
|> Keyword.merge(options)

data
|> keys_to_atoms(ap_options[:keys_to_atoms])
|> Map.to_list
|> Apex.Format.Seq.format(
ap_options,
start_token: "\%{",
end_token: "}",
numbers: false)
|> colorize(data, ap_options)
end

defp keys_to_atoms(data, shouldConvert)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also only a small nitpick. Please use "snake_case" for parameter names, so should_convert here.

defp keys_to_atoms(data, true) do
Map.new(data, fn {k,v} -> { convert_to_atom(k), v} end)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's inconsistent whitespace usage here before convert_to_atom:

fn {k,v} -> { convert_to_atom(k), v} end

Please remove the whitespace for the sake of consistency

end
defp keys_to_atoms(data, _), do: data

defp convert_to_atom(key)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

defp convert_to_atom(key) when is_binary(key), do: String.to_atom(key)
defp convert_to_atom(key), do: key
end

defimpl Apex.Format, for: Any do
Expand Down
99 changes: 99 additions & 0 deletions test/format_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,103 @@ defmodule Apex.Format.Test do
]
"""
end

test "Can format maps and change keys to atoms" do
data = %{"foo" => "bar", "bar" => "baz",
"mappings" => [
%{"foo1" => "bar1", "bar1" => "baz1" },
%{"foo2" => "bar2", "bar2" => "baz2" }
]
}
assert format(data, keys_to_atoms: true, color: false) == """
%{
bar: "baz"
foo: "bar"
mappings: [
[0] %{
bar1: "baz1"
foo1: "bar1"
}
[1] %{
bar2: "baz2"
foo2: "bar2"
}
]
}
"""
end

test "Can format maps and change keys to atoms (mixed)" do
data = %{
foo: "bar",
bar: "baz",
mappings: [
%{ "foo1" => "bar1", "bar1" => "baz1" },
%{ "foo2" => "bar2", "bar2" => "baz2" }
]
}
assert format(data, keys_to_atoms: true, color: false) == """
%{
bar: "baz"
foo: "bar"
mappings: [
[0] %{
bar1: "baz1"
foo1: "bar1"
}
[1] %{
bar2: "baz2"
foo2: "bar2"
}
]
}
"""
end

test "Can format maps and leave keys as is" do
data = %{"foo" => "bar", "bar" => "baz",
"mappings" => [
%{"foo1" => "bar1", "bar1" => "baz1" },
%{"foo2" => "bar2", "bar2" => "baz2" }
]
}
assert format(data, keys_to_atoms: false, color: false) == """
%{
{
[0] "bar"
[1] "baz"
}
{
[0] "foo"
[1] "bar"
}
{
[0] "mappings"
[1] [
[0] %{
{
[0] "bar1"
[1] "baz1"
}
{
[0] "foo1"
[1] "bar1"
}
}
[1] %{
{
[0] "bar2"
[1] "baz2"
}
{
[0] "foo2"
[1] "bar2"
}
}
]
}
}
"""
end

end