Skip to content

Commit

Permalink
Python: Add FUNCTION LIST command. (valkey-io#1738)
Browse files Browse the repository at this point in the history
* FUNCTION LIST tracer code

* update defaults

* fix type hints

* add tests

* add client tests

* update client tests with FUNCTION LIST

* fix formatting

* fix typing

* update typing

* typing updates

* fix formatting

* Apply suggestions from code review

Co-authored-by: Aaron <[email protected]>

* update response type to use bytes instead of str

* formatting

* add routing to function list tests

* fix routing tests

* update routing tests

* fix tests

* formatting

* rename args, add docstrings, update tests

* formatting

* updated docstrings and tests

* changelog

* fix tests

* update type definitions

* Apply suggestions from code review

Co-authored-by: Yury-Fridlyand <[email protected]>

* update docstrings

* Apply suggestions from code review

Co-authored-by: Aaron <[email protected]>

* minor cleanup

* black fixes

* fix typing

* update to use TEncodable

* introduce TFunctionListResponse to abstract away complex return type

---------

Co-authored-by: Aaron <[email protected]>
Co-authored-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
3 people authored and cyip10 committed Jul 16, 2024
1 parent 8e2693b commit 66d6b9a
Show file tree
Hide file tree
Showing 8 changed files with 510 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
* Python: Added RANDOMKEY command ([#1701](https://github.com/aws/glide-for-redis/pull/1701))
* Python: Added FUNCTION FLUSH command ([#1700](https://github.com/aws/glide-for-redis/pull/1700))
* Python: Added FUNCTION DELETE command ([#1714](https://github.com/aws/glide-for-redis/pull/1714))
* Python: Added FUNCTION LIST command ([#1738](https://github.com/aws/glide-for-redis/pull/1738))
* Python: Added SSCAN command ([#1709](https://github.com/aws/glide-for-redis/pull/1709))
* Python: Added LCS command ([#1716](https://github.com/aws/glide-for-redis/pull/1716))
* Python: Added WAIT command ([#1710](https://github.com/aws/glide-for-redis/pull/1710))
Expand Down
61 changes: 59 additions & 2 deletions python/python/glide/async_commands/cluster_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import Dict, List, Mapping, Optional, Union, cast
from typing import Any, Dict, List, Mapping, Optional, Set, Union, cast

from glide.async_commands.command_args import Limit, OrderBy
from glide.async_commands.core import (
Expand All @@ -12,7 +12,14 @@
_build_sort_args,
)
from glide.async_commands.transaction import BaseTransaction, ClusterTransaction
from glide.constants import TOK, TClusterResponse, TEncodable, TResult, TSingleNodeRoute
from glide.constants import (
TOK,
TClusterResponse,
TEncodable,
TFunctionListResponse,
TResult,
TSingleNodeRoute,
)
from glide.protobuf.redis_request_pb2 import RequestType
from glide.routes import Route

Expand Down Expand Up @@ -361,6 +368,56 @@ async def function_load(
),
)

async def function_list(
self,
library_name_pattern: Optional[TEncodable] = None,
with_code: bool = False,
route: Optional[Route] = None,
) -> TClusterResponse[TFunctionListResponse]:
"""
Returns information about the functions and libraries.
See https://valkey.io/commands/function-list/ for more details.
Args:
library_name_pattern (Optional[TEncodable]): A wildcard pattern for matching library names.
with_code (bool): Specifies whether to request the library code from the server or not.
route (Optional[Route]): The command will be routed to a random node, unless `route` is provided,
in which case the client will route the command to the nodes defined by `route`.
Returns:
TClusterResponse[TFunctionListResponse]: Info
about all or selected libraries and their functions.
Examples:
>>> response = await client.function_list("myLib?_backup", True)
[{
b"library_name": b"myLib5_backup",
b"engine": b"LUA",
b"functions": [{
b"name": b"myfunc",
b"description": None,
b"flags": {b"no-writes"},
}],
b"library_code": b"#!lua name=mylib \n redis.register_function('myfunc', function(keys, args) return args[1] end)"
}]
Since: Redis 7.0.0.
"""
args = []
if library_name_pattern is not None:
args.extend(["LIBRARYNAME", library_name_pattern])
if with_code:
args.append("WITHCODE")
return cast(
TClusterResponse[TFunctionListResponse],
await self._execute_command(
RequestType.FunctionList,
args,
route,
),
)

async def function_flush(
self, mode: Optional[FlushMode] = None, route: Optional[Route] = None
) -> TOK:
Expand Down
48 changes: 46 additions & 2 deletions python/python/glide/async_commands/standalone_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import Dict, List, Mapping, Optional, cast
from typing import Any, Dict, List, Mapping, Optional, Set, Union, cast

from glide.async_commands.command_args import Limit, OrderBy
from glide.async_commands.core import (
Expand All @@ -12,7 +12,7 @@
_build_sort_args,
)
from glide.async_commands.transaction import BaseTransaction, Transaction
from glide.constants import OK, TOK, TEncodable, TResult
from glide.constants import OK, TOK, TEncodable, TFunctionListResponse, TResult
from glide.protobuf.redis_request_pb2 import RequestType


Expand Down Expand Up @@ -265,6 +265,50 @@ async def function_load(
),
)

async def function_list(
self, library_name_pattern: Optional[TEncodable] = None, with_code: bool = False
) -> TFunctionListResponse:
"""
Returns information about the functions and libraries.
See https://valkey.io/commands/function-list/ for more details.
Args:
library_name_pattern (Optional[TEncodable]): A wildcard pattern for matching library names.
with_code (bool): Specifies whether to request the library code from the server or not.
Returns:
TFunctionListResponse: Info about all or
selected libraries and their functions.
Examples:
>>> response = await client.function_list("myLib?_backup", True)
[{
b"library_name": b"myLib5_backup",
b"engine": b"LUA",
b"functions": [{
b"name": b"myfunc",
b"description": None,
b"flags": {b"no-writes"},
}],
b"library_code": b"#!lua name=mylib \n redis.register_function('myfunc', function(keys, args) return args[1] end)"
}]
Since: Redis 7.0.0.
"""
args = []
if library_name_pattern is not None:
args.extend(["LIBRARYNAME", library_name_pattern])
if with_code:
args.append("WITHCODE")
return cast(
TFunctionListResponse,
await self._execute_command(
RequestType.FunctionList,
args,
),
)

async def function_flush(self, mode: Optional[FlushMode] = None) -> TOK:
"""
Deletes all function libraries.
Expand Down
30 changes: 30 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,36 @@ def function_load(
["REPLACE", library_code] if replace else [library_code],
)

def function_list(
self: TTransaction,
library_name_pattern: Optional[TEncodable] = None,
with_code: bool = False,
) -> TTransaction:
"""
Returns information about the functions and libraries.
See https://valkey.io/commands/function-list/ for more details.
Args:
library_name_pattern (Optional[TEncodable]): A wildcard pattern for matching library names.
with_code (bool): Specifies whether to request the library code from the server or not.
Commands response:
TFunctionListResponse: Info about all or
selected libraries and their functions.
Since: Redis 7.0.0.
"""
args = []
if library_name_pattern is not None:
args.extend(["LIBRARYNAME", library_name_pattern])
if with_code:
args.append("WITHCODE")
return self.append_command(
RequestType.FunctionList,
args,
)

def function_flush(
self: TTransaction, mode: Optional[FlushMode] = None
) -> TTransaction:
Expand Down
6 changes: 6 additions & 0 deletions python/python/glide/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@
# For more information, see: https://redis.io/docs/data-types/json/path/ .
TJsonResponse = Union[T, List[Optional[T]]]
TEncodable = Union[str, bytes]
TFunctionListResponse = List[
Mapping[
bytes,
Union[bytes, List[Mapping[bytes, Union[bytes, Set[bytes]]]]],
]
]
Loading

0 comments on commit 66d6b9a

Please sign in to comment.