nice_maps v0.3.0 NiceMaps

NiceMaps provides a single function parse to convert maps into the desired format.

It can build camelcase/snake_case keys, convert string keys to atom keys and vice versa, or convert structs to maps

Link to this section Summary

Functions

The main interface - this is where the magic happens.

Link to this section Functions

Link to this function

parse(map_or_struct, opts \\ [])

The main interface - this is where the magic happens.

Options

  • :keys one of :camelcase or :snake_case
  • :convert_structs one of true or false, default: false
  • :key_type, one of :string, :existing_atom, or :unsave_atom (please use :existing_atom whenever possible)

Examples

Without Options:

iex> NiceMaps.parse(%MyStruct{id: 1, my_key: "bar"})
%{id: 1, my_key: "bar"}

iex> NiceMaps.parse([%MyStruct{id: 1, my_key: "bar"}, %{value: "a"}])
[%{id: 1, my_key: "bar"}, %{value: "a"}]

iex> NiceMaps.parse([%MyStruct{id: 1, my_key: "bar"}, "String"])
[%{id: 1, my_key: "bar"}, "String"]

iex> NiceMaps.parse(%{0 => "0", 1 => "1"})
%{0 => "0", 1 => "1"}

Keys to camelcase:

iex> NiceMaps.parse([%MyStruct{id: 1, my_key: "bar"}, %{value: "a"}], keys: :camelcase)
[%{id: 1, myKey: "bar"}, %{value: "a"}]

iex> NiceMaps.parse(%MyStruct{id: 1, my_key: "foo"}, keys: :camelcase)
%{id: 1, myKey: "foo"}

iex> NiceMaps.parse(%{"string" => "value", "another_string" => "value"}, keys: :camelcase)
%{"string" => "value", "anotherString" => "value"}

# Keys to snake case:

iex> NiceMaps.parse(%MyCamelStruct{id: 1, myKey: "foo"}, keys: :snake_case)
%{id: 1, my_key: "foo"}

iex> NiceMaps.parse(%MyCamelStruct{id: 1, myKey: "foo"}, keys: :snake_case)
%{id: 1, my_key: "foo"}

iex> NiceMaps.parse(%{"string" => "value", "another_string" => "value"}, keys: :camelcase)
%{"string" => "value", "anotherString" => "value"}

Convert all structs into maps

iex> map = %{
...>   list: [
...>     %MyStruct{id: 1, my_key: "foo"}
...>   ],
...>   struct: %MyStruct{id: 2, my_key: "bar"},
...>   other_struct: %MyStruct{id: 3, my_key: %MyStruct{id: 4, my_key: nil}}
...> }
...> NiceMaps.parse(map, convert_structs: true)
%{
  list: [
    %{id: 1, my_key: "foo"}
  ],
  struct: %{id: 2, my_key: "bar"},
  other_struct: %{id: 3, my_key: %{id: 4, my_key: nil}}
}

Convert string keys to existing atom

iex> map = %{
...>   "key1" => "value 1",
...>   "nested" => %{"key2" => "value 2"},
...>   "list" => [%{"key3" => "value 3", "key4" => "value 4"}],
...>    1 => "an integer key",
...>    %MyStruct{} => "a struct key"
...> }
iex> [:key1, :key2, :key3, :key4, :nested, :list] # Make sure atoms exist
iex> NiceMaps.parse(map, key_type: :existing_atom)
%{
  :key1 => "value 1",
  :nested => %{key2: "value 2"},
  :list => [%{key3: "value 3", key4: "value 4"}],
  1 => "an integer key",
  %MyStruct{} => "a struct key"
}

Mix it all together

iex> map = %{
...>   "hello_there" => [%{"aA" => "asdf"}, %{"a_a" => "bhjk"}, "a string", 1],
...>   thingA: "thing A",
...>   thing_b: "thing B"
...> }
iex> NiceMaps.parse(map, keys: :camelcase, key_type: :string)
%{"helloThere" => [%{"aA" => "asdf"}, %{"aA" => "bhjk"}, "a string", 1], "thingA" => "thing A", "thingB" => "thing B"}

iex> map = %{
...>   "helloThere" => [%{"aA" => "asdf"}, %{"a_a" => "bhjk"}, "a string", 1],
...>   thingA: "thing A",
...>   thing_b: "thing B"
...> }
iex> [:hello_there, :thing_a, :thing_b] # make sure atoms exist
iex> NiceMaps.parse(map, keys: :snake_case, key_type: :existing_atom)
%{:hello_there => [%{:a_a => "asdf"}, %{:a_a => "bhjk"}, "a string", 1], :thing_a => "thing A", :thing_b => "thing B"}