Skip to content
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
20 changes: 17 additions & 3 deletions src/seriousdb/wal.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def from_dict(data: dict) -> "WalEntry":
Raises
------
TypeError
If `data` is not a dictionary.
If `data` is not a dictionary, or if key or value is not a string.
ValueError
If `data` does not describe a known entry type.
"""
Expand All @@ -41,10 +41,24 @@ def from_dict(data: dict) -> "WalEntry":
op = data.get("op")
try:
if op == "set":
return SetEntry(key=data["key"], value=data["value"])
key = data["key"]
value = data["value"]

if not isinstance(key, str):
raise TypeError("key must be a string")
if not isinstance(value, str):
raise TypeError("value must be a string")

return SetEntry(key=key, value=value)

if op == "delete":
return DeleteEntry(key=data["key"])
key = data["key"]

if not isinstance(key, str):
raise TypeError("key must be a string")

return DeleteEntry(key=key)

except KeyError as e:
raise ValueError(f"missing required field: {e}") from e

Expand Down
3 changes: 3 additions & 0 deletions tests/test_wal.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def test_replay_drops_and_truncates_a_torn_last_entry(wal, wal_path):
[
b'{"op": "set", "key": "b"}\n', # Missing required field
b"[]\n", # Unexpected top-level type
b'{"op": "set", "key": null, "value": "b"}\n', # Key is null
b'{"op": "set", "key": 5, "value": "b"}\n', # Key is not a string
b'{"op": "set", "key": "b", "value": [1, 2]}\n', # Value is not a string
],
)
def test_replay_recovers_from_malformed_entry(wal, wal_path, malformed):
Expand Down
Loading