GamendWeb.Helpers.ParamParser (gamend_web v1.0.1216)

Copy Markdown View Source

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

maybe_put_bool_filter(filters, key, value)

@spec maybe_put_bool_filter(map(), atom() | String.t(), term()) :: map()

maybe_put_bool_opt(opts, key, value)

@spec maybe_put_bool_opt(keyword(), atom(), term()) :: keyword()

maybe_put_filter(filters, key, value)

@spec maybe_put_filter(map(), atom() | String.t(), term()) :: map()

Put a non-empty filter value into a map.

maybe_put_int_filter(filters, key, value)

@spec maybe_put_int_filter(map(), atom() | String.t(), term()) :: map()

maybe_put_int_opt(opts, key, value)

@spec maybe_put_int_opt(keyword(), atom(), term()) :: keyword()

maybe_put_param_filter(filters, key, params)

@spec maybe_put_param_filter(map(), atom(), map()) :: map()

Put a non-empty filter value from params into a map using an atom key.

maybe_put_string_filter(filters, key, value)

@spec maybe_put_string_filter(map(), atom() | String.t(), term()) :: map()

maybe_put_string_opt(opts, key, value)

@spec maybe_put_string_opt(keyword(), atom(), term()) :: keyword()

param_value(params, string_key, atom_key)

@spec param_value(map(), String.t(), atom()) :: term()

Fetch a value from either string or atom keyed params.

parse_id(val)

@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

parse_id!(val)

@spec parse_id!(term()) :: {:ok, Ecto.UUID.t()} | :error

Like parse_id/1 but returns {:ok, uuid} or :error.

parse_int(val)

@spec parse_int(term()) :: integer() | nil

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.