From 38c34352aca628d4a357561044e0ccc6fad90966 Mon Sep 17 00:00:00 2001 From: aardolino Date: Thu, 30 Mar 2017 11:41:15 -0500 Subject: [PATCH 1/4] Added keys_to_atoms option for display maps --- lib/apex/format.ex | 14 +++++++++ test/format_test.exs | 72 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/lib/apex/format.ex b/lib/apex/format.ex index b33c17e..1bd2939 100644 --- a/lib/apex/format.ex +++ b/lib/apex/format.ex @@ -116,6 +116,7 @@ defimpl Apex.Format, for: Map do end def format(data, options) do + data = convert_keys_to_atoms(data, options) Apex.Format.Seq.format( Map.to_list(data), options, @@ -123,6 +124,19 @@ defimpl Apex.Format, for: Map do end_token: "}", numbers: false) |> colorize(data, options) end + + defp keys_to_atoms?(options) do + options[:keys_to_atoms] == true + end + + defp convert_keys_to_atoms(data, options) do + data = + if keys_to_atoms?(options) do + Map.new(data, fn {k,v} -> {String.to_atom(k), v} end) + else + data + end + end end defimpl Apex.Format, for: Any do diff --git a/test/format_test.exs b/test/format_test.exs index 4807459..8410bcc 100644 --- a/test/format_test.exs +++ b/test/format_test.exs @@ -172,4 +172,76 @@ 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 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 From 453c1a344b08b7312d08afbbdd20650b497ece49 Mon Sep 17 00:00:00 2001 From: aardolino Date: Thu, 30 Mar 2017 12:08:32 -0500 Subject: [PATCH 2/4] Fixed to work with maps that had keys as both atoms and strings --- lib/apex/format.ex | 10 +++++++++- test/format_test.exs | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/apex/format.ex b/lib/apex/format.ex index 1bd2939..180f20e 100644 --- a/lib/apex/format.ex +++ b/lib/apex/format.ex @@ -132,7 +132,15 @@ defimpl Apex.Format, for: Map do defp convert_keys_to_atoms(data, options) do data = if keys_to_atoms?(options) do - Map.new(data, fn {k,v} -> {String.to_atom(k), v} end) + Map.new(data, fn {k,v} -> + { + case is_atom(k) do + true -> k + false -> String.to_atom(k) + end, + v + } + end) else data end diff --git a/test/format_test.exs b/test/format_test.exs index 8410bcc..99d4adf 100644 --- a/test/format_test.exs +++ b/test/format_test.exs @@ -198,6 +198,33 @@ defmodule Apex.Format.Test do """ 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" => [ From 018c2813fa44fe0ec94fa156efd79ed345569df5 Mon Sep 17 00:00:00 2001 From: aardolino Date: Tue, 11 Apr 2017 09:19:24 -0500 Subject: [PATCH 3/4] Made suggested changes to pull request. Now supports keys_to_atoms using options or config. --- lib/apex/format.ex | 53 +++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/lib/apex/format.ex b/lib/apex/format.ex index 180f20e..c0797ce 100644 --- a/lib/apex/format.ex +++ b/lib/apex/format.ex @@ -116,35 +116,30 @@ defimpl Apex.Format, for: Map do end def format(data, options) do - data = convert_keys_to_atoms(data, options) - Apex.Format.Seq.format( - Map.to_list(data), - options, - start_token: "\%{", - end_token: "}", - numbers: false) |> colorize(data, options) - end - - defp keys_to_atoms?(options) do - options[:keys_to_atoms] == true - end - - defp convert_keys_to_atoms(data, options) do - data = - if keys_to_atoms?(options) do - Map.new(data, fn {k,v} -> - { - case is_atom(k) do - true -> k - false -> String.to_atom(k) - end, - v - } - end) - else - data - end - end + ap_options = :apex + |> 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, options) + end + + defp keys_to_atoms(data, shouldConvert) + defp keys_to_atoms(data, true) do + Map.new(data, fn {k,v} -> { convert_to_atom(k), v} end) + end + defp keys_to_atoms(data, _), do: data + + defp convert_to_atom(key) + 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 From 8a70f91417cfc3bc9cf53a1bf52601800fc7ecec Mon Sep 17 00:00:00 2001 From: aardolino Date: Tue, 11 Apr 2017 09:24:50 -0500 Subject: [PATCH 4/4] Small revision to use ap_options --- lib/apex/format.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/apex/format.ex b/lib/apex/format.ex index c0797ce..e71300a 100644 --- a/lib/apex/format.ex +++ b/lib/apex/format.ex @@ -128,7 +128,7 @@ defimpl Apex.Format, for: Map do start_token: "\%{", end_token: "}", numbers: false) - |> colorize(data, options) + |> colorize(data, ap_options) end defp keys_to_atoms(data, shouldConvert)