summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlaixintao <laixintaoo@gmail.com>2022-06-26 22:49:47 +0800
committerlaixintao <laixintaoo@gmail.com>2022-06-26 22:49:47 +0800
commitc8fc012920e4c0dede3bf737853e155066dc32c6 (patch)
treefe76056d3357b4042102f9d7735fe8a52579abd2
parentcd95f184a5818915987034a66c9770a9aaeb2ba1 (diff)
support HRANDFIELD.
-rw-r--r--CHANGELOG.md4
-rw-r--r--iredis/data/command_syntax.csv1
-rw-r--r--iredis/redis_grammar.py6
-rw-r--r--tests/unittests/command_parse/test_hash_parse.py19
4 files changed, 30 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1420608..7738a9b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+## UPCOMING
+
+- Feature: support new command: `HRANDFIELD`.
+
## 1.12
- Feature: `CLIENT KILL` now support `LADDR` argument.
diff --git a/iredis/data/command_syntax.csv b/iredis/data/command_syntax.csv
index 477c15c..528a6d4 100644
--- a/iredis/data/command_syntax.csv
+++ b/iredis/data/command_syntax.csv
@@ -87,6 +87,7 @@ hash,HKEYS,command_key,command_hkeys
hash,HLEN,command_key,render_int
hash,HMGET,command_key_fields,render_list
hash,HMSET,command_key_fieldvalues,render_bulk_string
+hash,HRANDFIELD,command_key_count_withvalues,render_list_or_string
hash,HSCAN,command_key_cursor_match_pattern_count,command_hscan
hash,HSET,command_key_field_value,render_int
hash,HSETNX,command_key_field_value,render_int
diff --git a/iredis/redis_grammar.py b/iredis/redis_grammar.py
index 4e38540..b9e2ea8 100644
--- a/iredis/redis_grammar.py
+++ b/iredis/redis_grammar.py
@@ -16,6 +16,7 @@ logger = logging.getLogger(__name__)
CONST = {
"failoverchoice": "TAKEOVER FORCE",
"withscores": "WITHSCORES",
+ "withvalues_const": "WITHVALUES",
"limit": "LIMIT",
"expiration": "EX PX",
"exat_const": "EXAT",
@@ -362,6 +363,7 @@ TIMEOUT_CONST = rf"(?P<timeout_const>{c('timeout_const')})"
ABORT_CONST = rf"(?P<abort_const>{c('abort_const')})"
PXAT_CONST = rf"(?P<pxat_const>{c('pxat_const')})"
EXAT_CONST = rf"(?P<exat_const>{c('exat_const')})"
+WITHVALUES_CONST = rf"(?P<withvalues_const>{c('withvalues_const')})"
command_grammar = compile(COMMAND)
@@ -660,6 +662,10 @@ GRAMMAR = {
(\s+ {EXAT_CONST} \s+ {TIMESTAMP})
)?
\s*""",
+ "command_key_count_withvalues": rf"""
+ \s+ {KEY}
+ (\s+ {COUNT} (\s+ {WITHVALUES_CONST})?)?
+ \s*""",
}
pipeline = r"(?P<shellcommand>\|.*)?"
diff --git a/tests/unittests/command_parse/test_hash_parse.py b/tests/unittests/command_parse/test_hash_parse.py
index 0c3aa04..bfe8768 100644
--- a/tests/unittests/command_parse/test_hash_parse.py
+++ b/tests/unittests/command_parse/test_hash_parse.py
@@ -43,3 +43,22 @@ def test_hset(judge_command):
"HSET foo bar hello",
{"command": "HSET", "key": "foo", "field": "bar", "value": "hello"},
)
+
+
+def test_hrandfield(judge_command):
+ judge_command(
+ "HRANDFIELD coin",
+ {"command": "HRANDFIELD", "key": "coin"},
+ )
+ judge_command(
+ "HRANDFIELD coin -5 WITHVALUES",
+ {"command": "HRANDFIELD", "key": "coin", "count": "-5", "withvalues_const": "WITHVALUES"},
+ )
+ judge_command(
+ "HRANDFIELD coin -5",
+ {"command": "HRANDFIELD", "key": "coin", "count": "-5"},
+ )
+ judge_command(
+ "HRANDFIELD coin WITHVALUES",
+ None
+ )