Unified field extraction for different data structures.
This module provides a consistent interface for extracting field values from different Elixir data structures (maps, keyword lists, tuples, structs).
Strategy
All data structures are normalized to maps before extraction, providing a single code path for field access. This eliminates type-specific extraction logic and makes the code easier to understand and maintain.
Supported Types
- Maps: Used as-is
- Structs: Converted to maps via
Map.from_struct/1 - Keyword lists: Converted to maps
- Tuples: Converted to maps using extraction template indices
Summary
Functions
Extracts a field value from normalized data.
Normalizes a data structure for field extraction.
Functions
Extracts a field value from normalized data.
Parameters
normalized_data- Map representation of data (fromnormalize_for_extraction/2)field_atom- The field name to extract
Returns
The field value, or nil if the field doesn't exist.
Examples
iex> data = %{foo: 1, bar: 2}
iex> extract_field(data, :foo)
1
iex> data = %{foo: 1, bar: 2}
iex> extract_field(data, :baz)
nil
Normalizes a data structure for field extraction.
Converts all supported data types to maps, using the extraction template when needed (e.g., for tuple index mapping).
Parameters
data- The data structure to normalize (map, struct, keyword list, or tuple)extraction_template- Template containing field metadata (used for tuples)
Returns
A map representation of the data suitable for field extraction.
Examples
# Map - returned as-is
iex> normalize_for_extraction(%{foo: 1, bar: 2}, [])
%{foo: 1, bar: 2}
# Keyword list - converted to map
iex> normalize_for_extraction([foo: 1, bar: 2], [])
%{foo: 1, bar: 2}
# Tuple - converted using template indices
iex> template = [%{field_name: :foo, index: 0}, %{field_name: :bar, index: 1}]
iex> normalize_for_extraction({1, 2}, template)
%{foo: 1, bar: 2}