Skip to content

Commit

Permalink
More careful map delete handling
Browse files Browse the repository at this point in the history
  • Loading branch information
point committed May 6, 2024
1 parent 6ba9f8f commit 8ca66b1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/y/type/map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule Y.Type.Map do

def get_item(%Y.Type.Map{map: map}, key, default \\ nil) do
case Map.fetch(map, key) do
{:ok, [%Item{deleted?: false} = item | _]} -> item
{:ok, [%Item{} = item | _]} -> item
_ -> default
end
end
Expand Down Expand Up @@ -113,12 +113,12 @@ defmodule Y.Type.Map do
{:ok, new_map_type, transaction}
else
%Item{deleted?: true} ->
Logger.warning("Item already deleted",
Logger.info("Item already deleted",
map: map,
key: key
)

{:error, map, transaction}
{:ok, map, transaction}

err ->
Logger.warning("Fail to delete map item by key #{inspect(key)}. Error: #{inspect(err)}",
Expand Down
8 changes: 7 additions & 1 deletion test/map_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ defmodule Y.MapTest do
assert {:ok, map0} = Doc.get(doc, "map0")
assert %TMap{} = map1 = TMap.get(map0, "m1")
assert nil == TMap.get(map1, "number key")
assert nil == TMap.get_item(map1, "number key")
assert %Item{deleted?: true} = TMap.get_item(map1, "number key")

Doc.transact(doc, fn transaction ->
{:ok, map1} = Doc.get(transaction, "map1")
assert {:ok, _, transaction} = TMap.delete(map1, transaction, "number key")
{:ok, transaction}
end)
end
end

0 comments on commit 8ca66b1

Please sign in to comment.