Shared helpers for safely parsing controller parameters.
Import into controllers that need safe integer parsing:
import GamendWeb.Helpers.ParamParser
Summary
Functions
Put a non-empty filter value into a map.
Put a non-empty filter value from params into a map using an atom key.
Fetch a value from either string or atom keyed params.
Safely parse a value into a UUID id. Returns the UUID string or nil.
Like parse_id/1 but returns {:ok, uuid} or :error.
Safely parse a value into an integer. Returns the integer or nil.
Functions
Put a non-empty filter value into a map.
Put a non-empty filter value from params into a map using an atom key.
Fetch a value from either string or atom keyed params.
@spec parse_id(term()) :: Ecto.UUID.t() | nil
Safely parse a value into a UUID id. Returns the UUID string or nil.
Examples
iex> parse_id("0198c0de-1234-7abc-8def-0123456789ab")
"0198c0de-1234-7abc-8def-0123456789ab"
iex> parse_id("abc")
nil
iex> parse_id(nil)
nil
@spec parse_id!(term()) :: {:ok, Ecto.UUID.t()} | :error
Like parse_id/1 but returns {:ok, uuid} or :error.
Safely parse a value into an integer. Returns the integer or nil.
Unlike parse_id/1, this accepts strings with trailing data because some
filter endpoints historically used Integer.parse/1 without requiring an
exact match.