Raxol.Utils.MapUtils (Raxol v2.0.1)

View Source

Common utility functions for map operations.

This module consolidates frequently used map transformation functions to avoid code duplication across the codebase.

Summary

Functions

Recursively converts all map keys to atoms.

Safely atomizes keys, only converting strings that already exist as atoms. This prevents atom exhaustion attacks.

Recursively converts all map keys to strings.

Functions

atomize_keys(map)

@spec atomize_keys(any()) :: any()

Recursively converts all map keys to atoms.

Examples

iex> Raxol.Utils.MapUtils.atomize_keys(%{"foo" => "bar", "nested" => %{"key" => "value"}})
%{foo: "bar", nested: %{key: "value"}}

iex> Raxol.Utils.MapUtils.atomize_keys(%{"atom" => [%{"inner" => "value"}]})
%{atom: [%{inner: "value"}]}

safe_atomize_keys(map)

@spec safe_atomize_keys(any()) :: any()

Safely atomizes keys, only converting strings that already exist as atoms. This prevents atom exhaustion attacks.

Examples

iex> Raxol.Utils.MapUtils.safe_atomize_keys(%{"foo" => "bar"})
%{"foo" => "bar"}  # "foo" atom doesn't exist

iex> _ = :existing_atom
iex> Raxol.Utils.MapUtils.safe_atomize_keys(%{"existing_atom" => "value"})
%{existing_atom: "value"}

stringify_keys(map)

@spec stringify_keys(any()) :: any()

Recursively converts all map keys to strings.

Examples

iex> Raxol.Utils.MapUtils.stringify_keys(%{foo: "bar", nested: %{key: "value"}})
%{"foo" => "bar", "nested" => %{"key" => "value"}}

iex> Raxol.Utils.MapUtils.stringify_keys(%{:atom => [%{inner: "value"}]})
%{"atom" => [%{"inner" => "value"}]}