Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implement memory optimizations in user struct #567

Merged
merged 2 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions lib/nostrum/struct/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ defmodule Nostrum.Struct.User do
:global_name,
:avatar,
:bot,
:mfa_enabled,
:verified,
:email,
:public_flags
]

Expand All @@ -60,17 +57,12 @@ defmodule Nostrum.Struct.User do
@typedoc "Whether the user is a bot"
@type bot :: boolean | nil

@typedoc "Whether the user has two factor enabled"
@type mfa_enabled :: boolean | nil
@typedoc """
The user's public flags, as a bitset.

@typedoc "Whether the email on the account has been verified"
@type verified :: boolean | nil

@typedoc "The user's email"
@type email :: String.t() | nil

@typedoc "The user's public flags"
@type public_flags :: Flags.t()
To parse these, use `Nostrum.Struct.User.Flags.from_integer/1`.
"""
@type public_flags :: Flags.raw_flags()

@type t :: %__MODULE__{
id: id,
Expand All @@ -79,9 +71,6 @@ defmodule Nostrum.Struct.User do
global_name: global_name,
avatar: avatar,
bot: bot,
mfa_enabled: mfa_enabled,
verified: verified,
email: email,
public_flags: public_flags
}

Expand Down Expand Up @@ -185,7 +174,6 @@ defmodule Nostrum.Struct.User do
map
|> Map.new(fn {k, v} -> {Util.maybe_to_atom(k), v} end)
|> Map.update(:id, nil, &Util.cast(&1, Snowflake))
|> Map.update(:public_flags, %Flags{}, &Flags.from_integer(&1))

struct(__MODULE__, new)
end
Expand Down
7 changes: 5 additions & 2 deletions lib/nostrum/struct/user/flags.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ defmodule Nostrum.Struct.User.Flags do

@type t :: flags

@typedoc "Raw user flags as sent by the Discord API"
@type raw_flags :: integer()

@flag_values [
staff: 1 <<< 0,
partner: 1 <<< 1,
Expand Down Expand Up @@ -142,7 +145,7 @@ defmodule Nostrum.Struct.User.Flags do
}
```
"""
@spec from_integer(integer()) :: t
@spec from_integer(raw_flags()) :: t
def from_integer(flag_value) do
boolean_list =
Enum.map(@flag_values, fn {flag, value} ->
Expand Down Expand Up @@ -177,7 +180,7 @@ defmodule Nostrum.Struct.User.Flags do
131842
```
"""
@spec to_integer(t) :: integer()
@spec to_integer(t) :: raw_flags()
def to_integer(flag_struct) do
booleans =
flag_struct
Expand Down
8 changes: 2 additions & 6 deletions test/nostrum/cache/user_cache_meta_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,15 @@ defmodule Nostrum.Cache.UserCacheMetaTest do
username: "test",
discriminator: "1234",
avatar: nil,
bot: true,
mfa_enabled: nil,
verified: nil
bot: true
}
@test_user_two %{
id: 54321,
username: "test two",
discriminator: "0",
global_name: "test_two",
avatar: nil,
bot: true,
mfa_enabled: nil,
verified: nil
bot: true
}

setup do
Expand Down
Loading