Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

"db_to_json" storage method is poorly named #14489

Open
anoadragon453 opened this issue Nov 18, 2022 · 1 comment
Open

"db_to_json" storage method is poorly named #14489

anoadragon453 opened this issue Nov 18, 2022 · 1 comment
Labels
A-Database DB stuff like queries, migrations, new/remove columns, indexes, unexpected entries in the db T-Task Refactoring, removal, replacement, enabling or disabling functionality, other engineering tasks. Z-Dev-Wishlist Makes developers' lives better, but doesn't have direct user impact

Comments

@anoadragon453
Copy link
Member

anoadragon453 commented Nov 18, 2022

This method converts a JSON-encoded string from a database into a Python dictionary object.

def db_to_json(db_content: Union[memoryview, bytes, bytearray, str]) -> Any:
"""
Take some data from a database row and return a JSON-decoded object.
Args:
db_content: The JSON-encoded contents from the database.
Returns:
The object decoded from JSON.
"""
# psycopg2 on Python 3 returns memoryview objects, which we need to
# cast to bytes to decode
if isinstance(db_content, memoryview):
db_content = db_content.tobytes()
# Decode it to a Unicode string before feeding it to the JSON decoder, since
# it only supports handling strings
if isinstance(db_content, (bytes, bytearray)):
db_content = db_content.decode("utf8")
try:
return json_decoder.decode(db_content)
except Exception:
logging.warning("Tried to decode '%r' as JSON and failed", db_content)
raise

db_to_json feels like the wrong name for it. Perhaps deserialise_json_from_db?

@anoadragon453 anoadragon453 added T-Task Refactoring, removal, replacement, enabling or disabling functionality, other engineering tasks. A-Database DB stuff like queries, migrations, new/remove columns, indexes, unexpected entries in the db Z-Dev-Wishlist Makes developers' lives better, but doesn't have direct user impact labels Nov 18, 2022
@clokep
Copy link
Contributor

clokep commented Nov 18, 2022

This method converts a JSON-encoded string from a database into a Python dictionary.

It isn't always dictionaries, it can be anything that can be JSON encoded.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-Database DB stuff like queries, migrations, new/remove columns, indexes, unexpected entries in the db T-Task Refactoring, removal, replacement, enabling or disabling functionality, other engineering tasks. Z-Dev-Wishlist Makes developers' lives better, but doesn't have direct user impact
Projects
None yet
Development

No branches or pull requests

2 participants