From 8ca66b13ed525701ecab3d2ee75b2c9c81d1de4e Mon Sep 17 00:00:00 2001 From: point Date: Mon, 6 May 2024 15:53:58 +0300 Subject: [PATCH] More careful map delete handling --- lib/y/type/map.ex | 6 +++--- test/map_test.exs | 8 +++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/y/type/map.ex b/lib/y/type/map.ex index e9dc9d7..5250970 100644 --- a/lib/y/type/map.ex +++ b/lib/y/type/map.ex @@ -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 @@ -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)}", diff --git a/test/map_test.exs b/test/map_test.exs index 21e1c6a..8f2ddba 100644 --- a/test/map_test.exs +++ b/test/map_test.exs @@ -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