BetterParams v0.5.0 BetterParams View Source

Implementation of the BetterParams plug and its core logic.

See the README for installation and usage instructions.

Link to this section Summary

Functions

Implementation of the Plug

Initializes the Plug

Converts String keys of a Map to Atoms

Link to this section Functions

Implementation of the Plug

This implements the call callback of the Plug. It calls the symbolize_merge/2 method on the Plug.Conn params map, so they are available both with String and Atom keys.

Initializes the Plug.

Link to this function symbolize_merge(map, opts \\ []) View Source
symbolize_merge(map :: map, opts :: Keyword.t) :: map

Converts String keys of a Map to Atoms

Takes a Map with String keys and an optional keyword list of options. Returns a new map with all values accessible by both String and Atom keys. If the map is nested, it symbolizes all sub-maps as well.

The only option supported at this time is drop_string_keys which defaults to false. When set true, it will return a new map with only Atom version of the keys.

Example

map = %{"a" => 1, "b" => %{"c" => 2, "d" => 3}}

mixed_map = BetterParams.symbolize_merge(map)
# => %{:a => 1, :b => %{c: 2, d: 3}, "a" => 1, "b" => %{"c" => 2, "d" => 3}}

atom_map = BetterParams.symbolize_merge(map, drop_string_keys: true)
# => %{a: 1, b: %{c: 2, d: 3}}

mixed_map[:a]           # => 1
mixed_map[:b][:c]       # => 2
mixed_map.b.d           # => 3