AshDispatch.ContentMap (AshDispatch v0.5.0)

View Source

Helper module for accessing content maps with mixed atom/string keys.

Content maps may have atom keys when created in-memory, but string keys when loaded from the database (PostgreSQL JSONB columns return string keys). This module provides safe access that handles both cases.

Why This Exists

When you store a map like %{title: "Hello"} in a JSONB column, PostgreSQL returns it as %{"title" => "Hello"}. This module eliminates the content[:title] || content["title"] pattern scattered throughout transport code.

Usage

import AshDispatch.ContentMap

# Get a value (checks atom key first, then string)
title = get_content(content, :title)

# Get with default
type = get_content(content, :notification_type, :info)

Key Resolution Order

  1. Atom key (e.g., :title)
  2. String key (e.g., "title")
  3. Default value (if provided)

Summary

Functions

Gets a value from a content map, handling both atom and string keys.

Gets a value from a content map with a default if not found.

Functions

get_content(content, key)

@spec get_content(map(), atom()) :: any()

Gets a value from a content map, handling both atom and string keys.

Checks atom key first, then string key. Returns nil if not found.

Examples

iex> get_content(%{title: "Hello"}, :title)
"Hello"

iex> get_content(%{"title" => "Hello"}, :title)
"Hello"

iex> get_content(%{}, :title)
nil

get_content(content, key, default)

@spec get_content(map(), atom(), any()) :: any()

Gets a value from a content map with a default if not found.

Examples

iex> get_content(%{}, :notification_type, :info)
:info

iex> get_content(%{notification_type: :success}, :notification_type, :info)
:success