apex icon indicating copy to clipboard operation
apex copied to clipboard

Added keys_to_atoms option for display maps

Open ajardolino3 opened this issue 7 years ago • 3 comments

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

ajardolino3 avatar Mar 30 '17 16:03 ajardolino3

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?

BjRo avatar Apr 01 '17 15:04 BjRo

Thanks for your suggestions. Very good. I will make your suggested changes and resubmit the pull request.

Art

On Sat, Apr 1, 2017 at 10:19 AM Björn Rochel [email protected] wrote:

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 https://github.com/BjRo/apex/blob/1e7ac2bca194b47ddd31c61e3ccaf8e667a8374c/lib/apex.ex#L7-L10 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?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/BjRo/apex/pull/28#issuecomment-290926589, or mute the thread https://github.com/notifications/unsubscribe-auth/AYnN3pqcEByZ1d-4hQATBNJyaVll63Q_ks5rrmsAgaJpZM4MuoLr .

ajardolino3 avatar Apr 11 '17 01:04 ajardolino3

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

ajardolino3 avatar Apr 11 '17 14:04 ajardolino3