Crux v2020-03-17 17:55:51Z Crux.Structs.Util View Source

Collection of util functions.

Link to this section Summary

Functions

Atomifies all keys in a passed list or map to avoid the mess of mixed string and atom keys the gateway sends.

id_to_int(str) deprecated

Converts a string, likely Discord snowflake, to not negative integer

Returns a function converting a passed map to an id, using the specified key as key.

Converts a list of raw api data to structs keyed under the passed key.

Converts a string to an atom.

Link to this section Functions

Link to this function

atomify(input) View Source (since 0.1.0)
atomify(input :: map() | list()) :: map() | list()

Atomifies all keys in a passed list or map to avoid the mess of mixed string and atom keys the gateway sends.

Examples

# A map
iex> %{"username" => "space", "discriminator" => "0001", "id" => "218348062828003328", "avatar" => "46a356e237350bf8b8dfde15667dfc4"}
...> |> Crux.Structs.Util.atomify()
%{username: "space", discriminator: "0001", id: "218348062828003328", avatar: "46a356e237350bf8b8dfde15667dfc4"}

# A list
iex> [
...>   %{"username" => "space", "discriminator" => "0001", "id" => "218348062828003328", "avatar" => "46a356e237350bf8b8dfde15667dfc4"},
...>   %{"username" => "Drahcirius", "discriminator" => "1336", "id" => "130175406673231873", "avatar" => "c896aebec82c90f590b08cfebcdc4e3b"}
...> ]
...> |> Crux.Structs.Util.atomify()
[
  %{username: "space", discriminator: "0001", id: "218348062828003328", avatar: "46a356e237350bf8b8dfde15667dfc4"},
  %{username: "Drahcirius", discriminator: "1336", id: "130175406673231873", avatar: "c896aebec82c90f590b08cfebcdc4e3b"}
]

# A nested map
iex> %{"foo" => "bar", "bar" => %{"baz" => "foo"}}
...> |> Crux.Structs.Util.atomify()
%{foo: "bar", bar: %{baz: "foo"}}

# A nested list
iex> [[%{"foo" => "bar"}], %{"bar" => "foo"}]
...> |> Crux.Structs.Util.atomify()
[[%{foo: "bar"}], %{bar: "foo"}]

# A struct
iex> %Crux.Structs.Overwrite{id: 448394877194076161, type: "role", allow: 0, deny: 0}
...> |> Crux.Structs.Util.atomify()
%{id: 448394877194076161, type: "role", allow: 0, deny: 0}
Link to this function

id_to_int(str) View Source (since 0.1.0)
id_to_int(id :: String.t() | integer() | nil) :: integer() | nil | no_return()

This function is deprecated. Use Snowflake.to_snowflake/1 instead.

Converts a string, likely Discord snowflake, to not negative integer

Examples

# A string
iex> "218348062828003328" |> Crux.Structs.Util.id_to_int()
218348062828003328

# Already a number
iex> 218348062828003328 |> Crux.Structs.Util.id_to_int()
218348062828003328

# Fallback
iex> nil |> Crux.Structs.Util.id_to_int()
nil
Link to this function

map_to_id(key \\ :id) View Source
map_to_id(key :: term()) :: (map() -> Crux.Structs.Snowflake.t() | nil)

Returns a function converting a passed map to an id, using the specified key as key.

Examples

# Id is already a number
iex> Crux.Structs.Util.map_to_id(:foo).(%{foo: 123})
123

# Id is a string
iex> Crux.Structs.Util.map_to_id(:foo).(%{foo: "123"})
123

# No id exists
iex> Crux.Structs.Util.map_to_id(:foo).(%{"foo" => "123"})
nil

# Example using [`Enum.map/2`](https://hexdocs.pm/elixir/Enum.html#map/2)
iex> [
...> %{"username" => "space", "discriminator" => "0001", "id" => "218348062828003328", "avatar" => "46a356e237350bf8b8dfde15667dfc4"},
...> %{"username" => "Drahcirius", "discriminator" => "1336", "id" => "130175406673231873", "avatar" => "c896aebec82c90f590b08cfebcdc4e3b"}
...> ]
...> |> Enum.map(Crux.Structs.Util.map_to_id("id"))
[218348062828003328, 130175406673231873]
Link to this macro

modulesince(version) View Source (macro)

Link to this function

raw_data_to_map(data, target, key \\ :id) View Source (since 0.1.0)
raw_data_to_map(data :: list(), target :: module(), key :: atom()) :: map()

Converts a list of raw api data to structs keyed under the passed key.

Examples

iex> [
...> %{"username" => "space", "discriminator" => "0001", "id" => "218348062828003328", "avatar" => "46a356e237350bf8b8dfde15667dfc4"},
...> %{"username" => "Drahcirius", "discriminator" => "1336", "id" => "130175406673231873", "avatar" => "c896aebec82c90f590b08cfebcdc4e3b"}
...> ]
...> |> Crux.Structs.Util.raw_data_to_map(Crux.Structs.User)
%{
  130175406673231873 => %Crux.Structs.User{
    username: "Drahcirius",
    discriminator: "1336",
    id: 130175406673231873,
    avatar: "c896aebec82c90f590b08cfebcdc4e3b"
  },
  218348062828003328 => %Crux.Structs.User{
    username: "space",
    discriminator: "0001",
    id: 218348062828003328,
    avatar: "46a356e237350bf8b8dfde15667dfc4"
  }
}

iex> [
...> %{"username" => "space", "discriminator" => "0001", "id" => "218348062828003328", "avatar" => "46a356e237350bf8b8dfde15667dfc4"},
...> %{"username" => "Drahcirius", "discriminator" => "1336", "id" => "130175406673231873", "avatar" => "c896aebec82c90f590b08cfebcdc4e3b"}
...> ]
...> |> Crux.Structs.Util.raw_data_to_map(Crux.Structs.User, :username)
%{
  "Drahcirius" => %Crux.Structs.User{
    username: "Drahcirius",
    discriminator: "1336",
    id: 130175406673231873,
    avatar: "c896aebec82c90f590b08cfebcdc4e3b"
  },
  "space" => %Crux.Structs.User{
    username: "space",
    discriminator: "0001",
    id: 218348062828003328,
    avatar: "46a356e237350bf8b8dfde15667dfc4"
  }
}
Link to this macro

since(version) View Source (macro)

Link to this function

string_to_atom(string) View Source (since 0.1.0)
string_to_atom(input :: String.t() | atom()) :: atom()

This function is deprecated. There will be no replacement.

Converts a string to an atom.

Returns an already converted atom as is instead of raising

Examples

# A string
iex> "id" |> Crux.Structs.Util.string_to_atom()
:id

# Already an atom
iex> :id |> Crux.Structs.Util.string_to_atom()
:id
Link to this macro

typesince(version) View Source (macro)