crux_structs v0.1.6 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
Converts a string, likely Discord snowflake, to an integer
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
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}
Converts a string, likely Discord snowflake, to an 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
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"
}
}
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