-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add seed support, default values, and JSON serialization wrappers #122
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
75c987c
feat: add seed support, default values, and JSON serialization wrappers
github-actions[bot] 253f8b6
Merge branch 'main' into claude/issue-115-20251026-0511
strawgate 8c625e9
refactor: add type overloads to PydanticAdapter.get() and refactor Me…
github-actions[bot] cf94411
feat: add default_ttl to DefaultValueWrapper and refactor MemoryStore
github-actions[bot] 3a3da4e
fix: correct type errors and improve code quality
github-actions[bot] 1bb30e5
refactor: move MemoryStore seeding to setup_collection for lazy initi…
github-actions[bot] ccabd02
Updates for PR Feedback
strawgate 1ac0750
Merge branch 'main' into claude/issue-115-20251026-0511
strawgate 2af53c3
Merge branch 'main' into claude/issue-115-20251026-0511
strawgate d720829
Updates for PR Feedback
strawgate d954e9c
Adjustments to seeding
strawgate a842d93
Update pydantic tests
strawgate eab8935
Adjustments to seeding
strawgate 3c8b9fd
docstring cleanup
strawgate 2767733
remove excessive docstrings
strawgate File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
key-value/key-value-aio/src/key_value/aio/wrappers/default_value/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| """Default value wrapper for returning fallback values when keys are not found.""" | ||
|
|
||
| from key_value.aio.wrappers.default_value.wrapper import DefaultValueWrapper | ||
|
|
||
| __all__ = ["DefaultValueWrapper"] |
68 changes: 68 additions & 0 deletions
68
key-value/key-value-aio/src/key_value/aio/wrappers/default_value/wrapper.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| from collections.abc import Mapping, Sequence | ||
| from typing import Any, SupportsFloat | ||
|
|
||
| from key_value.shared.utils.managed_entry import dump_to_json, load_from_json | ||
| from typing_extensions import override | ||
|
|
||
| from key_value.aio.protocols.key_value import AsyncKeyValue | ||
| from key_value.aio.wrappers.base import BaseWrapper | ||
|
|
||
|
|
||
| class DefaultValueWrapper(BaseWrapper): | ||
| """A wrapper that returns a default value when a key is not found. | ||
|
|
||
| This wrapper provides dict.get(key, default) behavior for the key-value store, | ||
| allowing you to specify a default value to return instead of None when a key doesn't exist. | ||
|
|
||
| It does not store the default value in the underlying key-value store and the TTL returned with the default | ||
| value is hard-coded based on the default_ttl parameter. Picking a default_ttl requires careful consideration | ||
| of how the value will be used and if any other wrappers will be used that may rely on the TTL. | ||
| """ | ||
|
|
||
| key_value: AsyncKeyValue # Alias for BaseWrapper compatibility | ||
| _default_ttl: float | None | ||
| _default_value_json: str | ||
|
|
||
| def __init__( | ||
| self, | ||
| key_value: AsyncKeyValue, | ||
| default_value: Mapping[str, Any], | ||
| default_ttl: SupportsFloat | None = None, | ||
| ) -> None: | ||
| """Initialize the DefaultValueWrapper. | ||
|
|
||
| Args: | ||
| key_value: The underlying key-value store to wrap. | ||
| default_value: The default value to return when a key is not found. | ||
| default_ttl: The TTL to return to the caller for default values. Defaults to None. | ||
| """ | ||
| self.key_value = key_value | ||
| self._default_value_json = dump_to_json(obj=dict(default_value)) | ||
| self._default_ttl = None if default_ttl is None else float(default_ttl) | ||
|
|
||
| def _new_default_value(self) -> dict[str, Any]: | ||
| return load_from_json(json_str=self._default_value_json) | ||
|
|
||
| @override | ||
| async def get(self, key: str, *, collection: str | None = None) -> dict[str, Any] | None: | ||
| result = await self.key_value.get(key=key, collection=collection) | ||
| return result if result is not None else self._new_default_value() | ||
|
|
||
| @override | ||
| async def get_many(self, keys: Sequence[str], *, collection: str | None = None) -> list[dict[str, Any] | None]: | ||
| results = await self.key_value.get_many(keys=keys, collection=collection) | ||
| return [result if result is not None else self._new_default_value() for result in results] | ||
|
|
||
| @override | ||
| async def ttl(self, key: str, *, collection: str | None = None) -> tuple[dict[str, Any] | None, float | None]: | ||
| result, ttl_value = await self.key_value.ttl(key=key, collection=collection) | ||
| if result is None: | ||
| return (self._new_default_value(), self._default_ttl) | ||
| return (result, ttl_value) | ||
|
|
||
| @override | ||
| async def ttl_many(self, keys: Sequence[str], *, collection: str | None = None) -> list[tuple[dict[str, Any] | None, float | None]]: | ||
| results = await self.key_value.ttl_many(keys=keys, collection=collection) | ||
| return [ | ||
| (result, ttl_value) if result is not None else (self._new_default_value(), self._default_ttl) for result, ttl_value in results | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.