LiveFilter.TypeUtils (LiveFilter v0.1.0)

View Source

Utility functions for type conversion in LiveFilter.

Provides safe conversion utilities for values that come from URL parameters or other external sources.

Summary

Functions

Safely converts a value to the specified type.

Converts string values to atoms safely, preserving non-string values.

Converts values to string safely.

Functions

convert_to_type(value, type)

Safely converts a value to the specified type.

Supports common type conversions needed by LiveFilter:

  • :atom - Convert strings to atoms
  • :string - Convert to string
  • :integer - Parse integers
  • :float - Parse floats
  • :boolean - Parse boolean values

Returns {:ok, converted_value} or {:error, reason}.

Examples

iex> LiveFilter.TypeUtils.convert_to_type("pending", :atom)
{:ok, :pending}

iex> LiveFilter.TypeUtils.convert_to_type("123", :integer)
{:ok, 123}

iex> LiveFilter.TypeUtils.convert_to_type("true", :boolean)
{:ok, true}

safe_to_atom(list)

Converts string values to atoms safely, preserving non-string values.

Used for enum field values that come from URL parameters as strings but need to be atoms for Ecto queries.

Examples

iex> LiveFilter.TypeUtils.safe_to_atom("pending")
:pending

iex> LiveFilter.TypeUtils.safe_to_atom(:already_atom)
:already_atom

iex> LiveFilter.TypeUtils.safe_to_atom(["pending", "active"])
[:pending, :active]

safe_to_string(value)

Converts values to string safely.

Handles atoms, strings, and other types gracefully.

Examples

iex> LiveFilter.TypeUtils.safe_to_string(:pending)
"pending"

iex> LiveFilter.TypeUtils.safe_to_string("already_string")
"already_string"

iex> LiveFilter.TypeUtils.safe_to_string(123)
"123"