Skip to content

Commit

Permalink
update/1 on a missing guild should not crash the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Th3-M4jor committed Aug 17, 2024
1 parent e8903db commit 7c1a4c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/nostrum/cache/guild_cache/ets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,19 @@ defmodule Nostrum.Cache.GuildCache.ETS do

@doc "Update the given guild in the cache."
@impl GuildCache
@spec update(map()) :: {Guild.t(), Guild.t()}
@spec update(map()) :: {Guild.t() | nil, Guild.t()}
def update(payload) do
[{_id, old_guild}] = :ets.lookup(@table_name, payload.id)
casted = Util.cast(payload, {:struct, Guild})
new_guild = Guild.merge(old_guild, casted)
true = :ets.update_element(@table_name, payload.id, {2, new_guild})
{old_guild, new_guild}

case :ets.lookup(@table_name, payload.id) do
[{_id, old_guild}] ->
new_guild = Guild.merge(old_guild, casted)
true = :ets.update_element(@table_name, payload.id, {2, new_guild})
{old_guild, new_guild}

[] ->
{nil, casted}
end
end

@doc "Delete the given guild from the cache."
Expand Down
9 changes: 9 additions & 0 deletions test/nostrum/cache/guild_cache_meta_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ defmodule Nostrum.Cache.GuildCacheMetaTest do
expected = Guild.to_struct(@test_guild)
assert ^expected = @cache.create(@test_guild)
end

test "update/1 returns {nil, guild}" do
expected = Guild.to_struct(@test_guild)
assert {nil, ^expected} = @cache.update(@test_guild)
end

test "delete/1 returns nil" do
assert nil == @cache.delete(@test_guild.id)
end
end

describe "with cached guild" do
Expand Down

0 comments on commit 7c1a4c4

Please sign in to comment.