mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Merge 07059e32f9 into 25c76b7d74
This commit is contained in:
commit
752ad9f0c3
4 changed files with 23 additions and 0 deletions
3
CHANGES/1431.feature.rst
Normal file
3
CHANGES/1431.feature.rst
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
Added method :code:`get_value` for class :code:`FSMContext`
|
||||||
|
|
||||||
|
- Added a new function :code:`get_value`, which allows retrieving the value by its name from the :code:`FSMContext`.
|
||||||
|
|
@ -20,6 +20,9 @@ class FSMContext:
|
||||||
async def get_data(self) -> Dict[str, Any]:
|
async def get_data(self) -> Dict[str, Any]:
|
||||||
return await self.storage.get_data(key=self.key)
|
return await self.storage.get_data(key=self.key)
|
||||||
|
|
||||||
|
async def get_value(self, key: str, default: Any = None) -> Any:
|
||||||
|
return await self.storage.get_value(key=self.key, data_key=key, default=default)
|
||||||
|
|
||||||
async def update_data(
|
async def update_data(
|
||||||
self, data: Optional[Dict[str, Any]] = None, **kwargs: Any
|
self, data: Optional[Dict[str, Any]] = None, **kwargs: Any
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,17 @@ class BaseStorage(ABC):
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
async def get_value(self, key: StorageKey, data_key: str, default: Any = None) -> Any:
|
||||||
|
"""
|
||||||
|
Get selected value by key in current data
|
||||||
|
|
||||||
|
:param key: storage key
|
||||||
|
:param data_key: key of selected data
|
||||||
|
:return: value of current data by key
|
||||||
|
"""
|
||||||
|
current_data = await self.get_data(key=key)
|
||||||
|
return current_data.get(data_key, default)
|
||||||
|
|
||||||
async def update_data(self, key: StorageKey, data: Dict[str, Any]) -> Dict[str, Any]:
|
async def update_data(self, key: StorageKey, data: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Update date in the storage for key (like dict.update)
|
Update date in the storage for key (like dict.update)
|
||||||
|
|
|
||||||
|
|
@ -43,3 +43,9 @@ class TestStorages:
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
"baz": "spam",
|
"baz": "spam",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async def test_get_value(self, bot: MockedBot, storage: BaseStorage, storage_key: StorageKey):
|
||||||
|
await storage.set_data(key=storage_key, data={"hello": "world"})
|
||||||
|
assert await storage.get_value(key=storage_key, data_key="hello") == "world"
|
||||||
|
assert await storage.get_value(key=storage_key, data_key="12345") is None
|
||||||
|
assert await storage.get_value(key=storage_key, data_key="qwerty", default=42) == 42
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue