Bunnyx.Params (Bunnyx v0.4.0)

Copy Markdown View Source

Shared helpers for converting snake_case attrs to API-format maps.

Used internally by all API modules to convert attrs (keyword list or map, e.g. name: "my-zone" or %{name: "my-zone"}) into the PascalCase maps the bunny.net API expects (e.g. %{"Name" => "my-zone"}). Unknown keys raise ArgumentError with the valid set listed.

Summary

Types

Attributes accepted by create/update functions: keyword list or atom-keyed map.

Functions

Like map_keys!/2 but only includes keys present in the mapping, ignoring extras. Useful for query param conversion where unknown keys should be silently dropped.

Converts attrs (keyword list or map) to a map using the given key mapping.

Puts a key-value pair into a keyword list only if the value is not nil.

Puts a key-value pair into a map only if the value is not nil.

Types

attrs()

@type attrs() :: keyword() | %{optional(atom()) => term()}

Attributes accepted by create/update functions: keyword list or atom-keyed map.

Functions

map_keys(attrs, mapping)

@spec map_keys(attrs(), %{required(atom()) => String.t()}) :: map()

Like map_keys!/2 but only includes keys present in the mapping, ignoring extras. Useful for query param conversion where unknown keys should be silently dropped.

map_keys!(attrs, mapping)

@spec map_keys!(attrs(), %{required(atom()) => String.t()}) :: map()

Converts attrs (keyword list or map) to a map using the given key mapping.

Raises ArgumentError with a clear message if an unknown key is passed.

Examples

iex> Bunnyx.Params.map_keys!([name: "test"], %{name: "Name"})
%{"Name" => "test"}

iex> Bunnyx.Params.map_keys!(%{name: "test"}, %{name: "Name"})
%{"Name" => "test"}

iex> Bunnyx.Params.map_keys!([bad: "x"], %{name: "Name"})
** (ArgumentError) unknown key :bad. Valid keys: [:name]

maybe_put(opts, key, value)

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

Puts a key-value pair into a keyword list only if the value is not nil.

maybe_put_map(map, key, value)

@spec maybe_put_map(map(), String.t(), term()) :: map()

Puts a key-value pair into a map only if the value is not nil.