Phoenix Dev Blog - Sounds Like a Bug The benefits of using Elixir for backend development: Our perspective

New library - `map_with_indifferent_access`

Have you ever found it difficult to work with maps that sometimes have string keys (if it’s controller params coming from outside) and sometimes atom keys (if it’s params defined inside of the code)?

If so, consider using our newly published map_with_indifferent_access library, which gives you functions such as MapWithIndifferentAccess.get/3, MapWithIndifferentAccess.put/3, that mimic the Map.* functions.

The advantage of using them is that you don’t have to worry if the map currently uses string or atom keys. You, as the caller, only use atom keys. The underlying code will automatically deduce if the map uses string or atom keys, and depending on it, convert the key to string or leave it as atom, before handing it further to the original Map.* function.

This will prevent you from reaching a Ecto.CastError saying it received a map with mixed key types.

(Inspired by Ruby on Rails’ HashWithIndifferentAccess.)