Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added keys_to_atoms option for display maps #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ajardolino3
Copy link

@ajardolino3 ajardolino3 commented Mar 30, 2017

[WIP] We use lots of maps with string keys instead of atoms because we have a lot of dynamic data. I wanted to be able to view these maps using your library for debugging purposes, but its annoying to see string keys as [0] [1] pairs. So I added an option keys_to_atoms which when set to true, automatically converts the string keys to atoms. Would never use this in production code, because atoms would take up memory, but great for testing.

Only problem is, I could not figure out how to modify the code to allow this option to be set in the config, similar to how the numbers option works. So could use some help/guidance on best way to do that with your code.

Thanks and I look forward to hearing from you.

Copy link
Owner

@BjRo BjRo left a comment

Choose a reason for hiding this comment

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

Hey there,

this is the first batch of review findings. I have to leave now, but I thought I might already post some feedback back.

I'll add the part regarding the configuration later

options[:keys_to_atoms] == true
end

defp convert_keys_to_atoms(data, options) do
Copy link
Owner

Choose a reason for hiding this comment

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

So you can directly pattern match in the function head of convert_keys_to_atoms on the settings and spare the keys_to_atoms? function. I would also probably strip of the convert_ prefix.

def keys_to_atoms(data, shouldConvert)
def keys_to_atoms(data, true) do
  #rest of the impl
end
def keys_to_atoms(data, _), do: data

@@ -116,13 +116,35 @@ defimpl Apex.Format, for: Map do
end

def format(data, options) do
data = convert_keys_to_atoms(data, options)
Copy link
Owner

Choose a reason for hiding this comment

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

Please reformat this as a pipe

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

end

defp convert_keys_to_atoms(data, options) do
data =
Copy link
Owner

Choose a reason for hiding this comment

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

The change above should simplify the body of the function to

def keys_to_atoms(data, true) do
   Map.new(data, fn {k,v} -> {convert_to_atom(k), v} end)
end

Please also take into account that the code should also be able to handle non atom and non string keys. Currently the code would try to invoke String.to_atom also on other types and would blow up.

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

@BjRo
Copy link
Owner

BjRo commented Apr 1, 2017

Regarding the configuration question, if I'm not mistaken numbers can't be currently set via application config (since the only place I'm reading from it is in Colors https://github.com/BjRo/apex/search?utf8=%E2%9C%93&q=Application.get_env&type=

The way I would approach this is by reading the apex configuration section here merge it with the user supplied configuration and pass it down.

  def ap(data, options \\ []) do
    ap_options = :apex
                            |> Application.get_env([])
                            |> Keyword.merge(options)
    
    formatted = Apex.Format.format(data, ap_options)
    IO.puts(formatted)
    data
  end

Something like this. This would then also work for numbers. So you could set

use Mix.Config

config :apex, numbers: false, keys_to_atoms: true

I guess that would work. Does this help?

@ajardolino3
Copy link
Author

ajardolino3 commented Apr 11, 2017 via email

@ajardolino3
Copy link
Author

Suggested changes were made. Now keys_to_atoms works with config. Ready for review!

Copy link
Owner

@BjRo BjRo left a comment

Choose a reason for hiding this comment

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

Only minor nitpicks. Other then that looks good 👍

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).

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 keys_to_atoms(data, shouldConvert)
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

|> 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants