-
Notifications
You must be signed in to change notification settings - Fork 2
Refactor ManagedEntry serialization with adapter pattern #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
796e8ec
0ab6766
49cf605
714b0cb
17743fe
0dbf7d6
8c4b6d3
59cbaee
2cb6d1a
3ffd540
e519537
194bc94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| from key_value.shared.utils.compound import compound_key | ||||||||||||||||||||||||
| from key_value.shared.utils.managed_entry import ManagedEntry | ||||||||||||||||||||||||
| from key_value.shared.utils.serialization import BasicSerializationAdapter | ||||||||||||||||||||||||
| from typing_extensions import override | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| from key_value.aio.stores.base import BaseContextManagerStore, BaseDestroyStore, BaseStore | ||||||||||||||||||||||||
|
|
@@ -44,9 +45,11 @@ def __init__( | |||||||||||||||||||||||
| port: Memcached port. Defaults to 11211. | ||||||||||||||||||||||||
| default_collection: The default collection to use if no collection is provided. | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| super().__init__(default_collection=default_collection) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| self._client = client or Client(host=host, port=port) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| super().__init__(default_collection=default_collection) | ||||||||||||||||||||||||
| self._serialization_adapter = BasicSerializationAdapter(value_format="dict") | ||||||||||||||||||||||||
|
Comment on lines
+48
to
+52
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical initialization order issue. The code calls Apply this diff to initialize the adapter before calling the parent constructor: - super().__init__(default_collection=default_collection)
-
self._client = client or Client(host=host, port=port)
self._serialization_adapter = BasicSerializationAdapter(value_format="dict")
+
+ super().__init__(default_collection=default_collection)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def sanitize_key(self, key: str) -> str: | ||||||||||||||||||||||||
| if len(key) > MAX_KEY_LENGTH: | ||||||||||||||||||||||||
|
|
@@ -65,7 +68,7 @@ async def _get_managed_entry(self, *, key: str, collection: str) -> ManagedEntry | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| json_str: str = raw_value.decode(encoding="utf-8") | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return ManagedEntry.from_json(json_str=json_str) | ||||||||||||||||||||||||
| return self._serialization_adapter.load_json(json_str=json_str) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||
| async def _get_managed_entries(self, *, collection: str, keys: Sequence[str]) -> list[ManagedEntry | None]: | ||||||||||||||||||||||||
|
|
@@ -82,7 +85,7 @@ async def _get_managed_entries(self, *, collection: str, keys: Sequence[str]) -> | |||||||||||||||||||||||
| for raw_value in raw_values: | ||||||||||||||||||||||||
| if isinstance(raw_value, (bytes, bytearray)): | ||||||||||||||||||||||||
| json_str: str = raw_value.decode(encoding="utf-8") | ||||||||||||||||||||||||
| entries.append(ManagedEntry.from_json(json_str=json_str)) | ||||||||||||||||||||||||
| entries.append(self._serialization_adapter.load_json(json_str=json_str)) | ||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||
| entries.append(None) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -106,7 +109,7 @@ async def _put_managed_entry( | |||||||||||||||||||||||
| else: | ||||||||||||||||||||||||
| exptime = max(int(managed_entry.ttl), 1) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| json_value: str = managed_entry.to_json() | ||||||||||||||||||||||||
| json_value: str = self._serialization_adapter.dump_json(entry=managed_entry) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| _ = await self._client.set( | ||||||||||||||||||||||||
| key=combo_key.encode(encoding="utf-8"), | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.