View Source Rivet.Utils.Types (rivet_utils v1.0.2)

Helper module for common type handling needs

Contributors: Brandon Gillespie, Mark Erickson, Lyle Mantooth

Link to this section Summary

Functions

Polymorphic conversion of data into an atom.

iex> as_float(10.52)
{:ok, 10.52}
iex> as_float("10.5")
{:ok, 10.5}
iex> as_float(".55")
{:ok, 0.55}
iex> as_float("tardis")
{:error, "\"tardis\" is not a number"}
iex> as_float!(10.0)
10.0
iex> as_float!("10.0")
10.0
iex> as_float!("tardis", 9.0)
9.0
iex> as_int(10)
{:ok, 10}
iex> as_int("10")
{:ok, 10}
iex> as_int("tardis")
{:error, "\"tardis\" is not a number"}
iex> as_int(:foo)
{:error, ":foo is not a number"}
iex> as_int!(10)
10
iex> as_int!("10")
10
iex> as_int!("tardis", 9)
9
iex> as_int!(:foo, 8)
8

Lowercase, Snakecase atom, from string or atom

iex> lookup_address("127.0.0.1")
{:ok, {127,0,0,1}}

iex> map_to_kvstr(%{a: "b", c: "10", d: "longer with space"}) to_string('a=b c=10 d="longer with space"')

Iterate a map and merge string & atom keys into just strings.

Remove any keys not in allowed_keys list iex> strip_keys_not_in(%{"this" => 1, "that" => 2}, ["this"]) %{"this" => 1}

iex> strip_subdict_values_not(%{"sub" => %{"a" => false, "b" => "not bool"}}, "sub", &is_boolean/1) %{"sub" => %{"a" => false}}

iex> strip_values_not_is(%{"a" => false, "b" => "not bool"}, &is_boolean/1) %{"a" => false}

Link to this section Functions

Polymorphic conversion of data into an atom.

If using, make sure inputs being called are not using user-submitted data, or be vulnerable to exhausting the atoms table.

iex> as_atom("long ugly thing prolly")
:"long ugly thing prolly"
iex> as_atom("as_atom")
:as_atom
iex> as_atom(:atom)
:atom
iex> as_atom(["as_atom", "another"])
[:as_atom, :another]
iex> as_atom({:fugly})
:"{:fugly}"
iex> as_float(10.52)
{:ok, 10.52}
iex> as_float("10.5")
{:ok, 10.5}
iex> as_float(".55")
{:ok, 0.55}
iex> as_float("tardis")
{:error, "\"tardis\" is not a number"}
Link to this function

as_float!(arg, default \\ 0.0)

View Source
iex> as_float!(10.0)
10.0
iex> as_float!("10.0")
10.0
iex> as_float!("tardis", 9.0)
9.0
iex> as_int(10)
{:ok, 10}
iex> as_int("10")
{:ok, 10}
iex> as_int("tardis")
{:error, "\"tardis\" is not a number"}
iex> as_int(:foo)
{:error, ":foo is not a number"}
Link to this function

as_int!(arg, default \\ 0)

View Source
iex> as_int!(10)
10
iex> as_int!("10")
10
iex> as_int!("tardis", 9)
9
iex> as_int!(:foo, 8)
8

Lowercase, Snakecase atom, from string or atom

iex> clean_atom(:value)
:value
iex> clean_atom(:Value)
:value
iex> clean_atom("valueCamelToSnake")
:value_camel_to_snake
iex> clean_atom(:"value-dashed")
:value_dashed
iex> clean_atom("Value-Dashed")
:value_dashed
iex> clean_atom({:narf})
{:narf}
@spec lookup_address(String.t()) :: {:ok, tuple()} | {:error, term()}
iex> lookup_address("127.0.0.1")
{:ok, {127,0,0,1}}

iex> map_to_kvstr(%{a: "b", c: "10", d: "longer with space"}) to_string('a=b c=10 d="longer with space"')

Iterate a map and merge string & atom keys into just strings.

Not recursive, only top level.

Behavior with mixed keys being merged is not guaranteed, as maps are not always ordered.

examples

Examples

iex> string_keys(%{tardis: 1, is: 2, color: "blue"})
%{"tardis" => 1, "is" => 2, "color" => "blue"}
Link to this function

strip_keys_not_in(dict, allowed_keys)

View Source

Remove any keys not in allowed_keys list iex> strip_keys_not_in(%{"this" => 1, "that" => 2}, ["this"]) %{"this" => 1}

Link to this function

strip_subdict_values_not(dict, key, type_test)

View Source

iex> strip_subdict_values_not(%{"sub" => %{"a" => false, "b" => "not bool"}}, "sub", &is_boolean/1) %{"sub" => %{"a" => false}}

Link to this function

strip_values_not_is(dict, type_test)

View Source

iex> strip_values_not_is(%{"a" => false, "b" => "not bool"}, &is_boolean/1) %{"a" => false}