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

Provides a unified function to create one or a list of structs, invoking their create/1 function if available.

Link to this section Summary

Functions

Creates a struct or a list of structs invoking their create/1 function if available.

Resolves the id of a struct invoking their resolve_id/1 function if available.

Callbacks

Can be implemented by structs to transform the inital data.

Can be implemented by structs to provide a mechanism to resolve their id.

Link to this section Functions

Link to this function

create(data, target) View Source (since 0.1.0)
create(data :: map(), target :: module()) :: struct()
create(data :: list(), target :: module()) :: [struct()]

Creates a struct or a list of structs invoking their create/1 function if available.

Examples

  # A single member
  iex> %{
  ...>   "nick" => "nick",
  ...>   "user" => %{"username" => "space", "discriminator" => "0001", "id" => "218348062828003328", "avatar" => "646a356e237350bf8b8dfde15667dfc4"},
  ...>   "roles" => ["251158405832638465", "373405430589816834"],
  ...>   "mute" => false,
  ...>   "deaf" => false,
  ...>   "joined_at" => "2016-11-02T00:51:21.342000+00:00"
  ...> }
  ...> |> Crux.Structs.create(Crux.Structs.Member)
  %Crux.Structs.Member{
    nick: "nick",
    user: 218348062828003328,
    roles: MapSet.new([251158405832638465, 373405430589816834]),
    mute: false,
    deaf: false,
    joined_at: "2016-11-02T00:51:21.342000+00:00",
    guild_id: nil
  }

  # A single user
  iex> %{"username" => "space", "discriminator" => "0001", "id" => "218348062828003328", "avatar" => "46a356e237350bf8b8dfde15667dfc4"}
  ...> |> Crux.Structs.create(Crux.Structs.User)
  %Crux.Structs.User{username: "space", discriminator: "0001", id: 218348062828003328, avatar: "46a356e237350bf8b8dfde15667dfc4"}

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

  # Does not alter already structs
  iex> Crux.Structs.create(
  ...>   %Crux.Structs.User{username: "space", discriminator: "0001", id: 218348062828003328, avatar: "46a356e237350bf8b8dfde15667dfc4"},
  ...>   Crux.Structs.User
  ...> )
  %Crux.Structs.User{username: "space", discriminator: "0001", id: 218348062828003328, avatar: "46a356e237350bf8b8dfde15667dfc4"}

  # Fallback
  iex> Crux.Structs.create(nil, nil)
  nil
Link to this function

resolve_id(data, target) View Source
resolve_id(nil, target :: module()) :: nil
resolve_id(Crux.Structs.Snowflake.t(), target :: module()) ::
  Crux.Structs.Snowflake.t()
resolve_id(map(), target :: module()) :: Crux.Structs.Snowflake.t() | nil

Resolves the id of a struct invoking their resolve_id/1 function if available.

  # Struct of the concrete type
  iex> %Crux.Structs.Webhook{id: 618733351624507394}
  ...> |> Crux.Structs.resolve_id(Crux.Structs.Webhook)
  618733351624507394

  # Already snowflake
  iex> 222089067028807682
  ...> |> Crux.Structs.resolve_id(Crux.Structs.Role)
  222089067028807682

  # Snowflake string
  iex> "222079895583457280"
  ...> |> Crux.Structs.resolve_id(Crux.Structs.Channel)
  222079895583457280

  # nil
  iex> nil
  ...> |> Crux.Structs.resolve_id(Crux.Structs.Guild)
  nil

  # Inexact type that is a resolvable
  iex> %Crux.Structs.Member{user: 218348062828003328}
  ...> |> Crux.Structs.resolve_id(Crux.Structs.User)
  218348062828003328

  # Incorrect type
  iex> %Crux.Structs.Role{id: 222079439876390922}
  ...> |> Crux.Structs.resolve_id(Crux.Structs.Emoji)
  nil

Link to this section Callbacks

Link to this callback

create(data) View Source (optional)
create(data :: map()) :: struct()

Can be implemented by structs to transform the inital data.

Link to this callback

resolve_id(data) View Source (optional)
resolve_id(data :: map() | Crux.Structs.Snowflake.t()) ::
  Crux.Structs.Snowflake.t() | nil

Can be implemented by structs to provide a mechanism to resolve their id.