Skip to content

Commit

Permalink
improve json key preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
woylie authored and rrrene committed Jan 15, 2022
1 parent c52b985 commit 6856b8b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 9 additions & 1 deletion lib/credo/cli/output/formatter/json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule Credo.CLI.Output.Formatter.JSON do

def prepare_for_json(%{} = term) do
Enum.into(term, %{}, fn {key, value} ->
{prepare_for_json(key), prepare_for_json(value)}
{prepare_key_for_json(key), prepare_for_json(value)}
end)
end

Expand All @@ -44,6 +44,14 @@ defmodule Credo.CLI.Output.Formatter.JSON do
inspect(term)
end

defp prepare_key_for_json(k) when is_atom(k) or is_binary(k) or is_number(k) do
k
end

defp prepare_key_for_json(k) do
inspect(k)
end

def issue_to_json(
%Issue{
check: check,
Expand Down
10 changes: 8 additions & 2 deletions test/credo/cli/output/formatter/json_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@ defmodule Credo.CLI.Output.Formatter.JsonTest do
"number" => 5,
"regex" => ~r/foo/,
"string" => "a",
"tuple" => {"a", 2, ~r/foo/}
"tuple" => {"a", 2, ~r/foo/},
:atom_key => 0,
{"tuple", "key"} => 1,
0 => 2
}) == %{
"bool" => true,
"list" => ["a", %{"a" => "b", "b" => "~r/foo/"}],
"map" => %{"c" => "d", "e" => ["f", 8, "~r/foo/"]},
"number" => 5,
"regex" => "~r/foo/",
"string" => "a",
"tuple" => ["a", 2, "~r/foo/"]
"tuple" => ["a", 2, "~r/foo/"],
:atom_key => 0,
"{\"tuple\", \"key\"}" => 1,
0 => 2
}
end
end

0 comments on commit 6856b8b

Please sign in to comment.