Skip to content

Commit

Permalink
async to_obj
Browse files Browse the repository at this point in the history
  • Loading branch information
TLCFEM committed Jun 6, 2024
1 parent 47502fc commit 588f0fe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/msglc/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ def to_obj(v):
return v.to_obj() if isinstance(v, LazyItem) else v


async def async_to_obj(v):
"""
Ensure the given value is JSON serializable.
"""
if isinstance(v, LazyItem):
return await asyncio.to_thread(v.to_obj)

return v


async def async_get(v, key):
if isinstance(v, LazyItem):
return await v.async_get(key)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_msglc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from msglc import LazyWriter, FileInfo, combine, append
from msglc.config import config, increment_gc_counter, decrement_gc_counter, configure
from msglc.reader import LazyStats, LazyReader
from msglc.reader import LazyStats, LazyReader, async_to_obj
from msglc.utility import MockIO


Expand Down Expand Up @@ -142,6 +142,7 @@ async def test_async_msglc(monkeypatch, tmpdir, json_before, json_after, target,
assert await reader.async_read("glossary/empty_list") == []
assert await reader.async_read("glossary/none_list/0") is None
assert await reader.async_read() == json_after
assert await async_to_obj(reader) == json_after
assert reader == json_after

dict_container = await reader.async_read("glossary/GlossDiv")
Expand Down

0 comments on commit 588f0fe

Please sign in to comment.