From 9bedabebd5a6f108de89e7d9c839966acd99c71c Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Thu, 26 Jan 2023 15:09:48 +0200 Subject: [PATCH] Extended Redis JSON support by implementing JSON.MGET (#892) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added CommandResolver, moved resolve command logic there, added ClientConfiguration object * Fixed test, added dependecies * Added resolved command to commands array * Used aggregation approach for modules * Added decorator to check Redis JSON module version * Added support for JSON.SET and JSON.GET commands * Added separate workflow for redis-stack tests * Changed docker imange name to correct one * Fixed indentation * Added test coverage for JSON.GET command * Changed module version resolving using annotations mapping * Re-written CommandResolver test * Update ClientInterface.php * Changes to CI, readme, removed unused modules from configuration * Fixed build badge URL * Refactored annotation check to be generic for each module * Fixed bug with incorrect tests skip * Added CommandResolver, moved resolve command logic there, added ClientConfiguration object * Fixed test, added dependecies * Added resolved command to commands array * Used aggregation approach for modules * Added decorator to check Redis JSON module version * Added support for JSON.SET and JSON.GET commands * Added separate workflow for redis-stack tests * Changed docker imange name to correct one * Fixed indentation * Added test coverage for JSON.GET command * Changed module version resolving using annotations mapping * Re-written CommandResolver test * Update ClientInterface.php * Changes to CI, readme, removed unused modules from configuration * Fixed build badge URL * Refactored annotation check to be generic for each module * Fixed bug with incorrect tests skip * Fixed naming issue with nxXx argument * Removed redundant trait * Fixed NxXxArgument test * Added support for JSON.MGET command Co-authored-by: Vladyslav Vildanov Co-authored-by: Till Krüss --- src/ClientContextInterface.php | 1 + src/ClientInterface.php | 1 + src/Command/Redis/Json/JSONMGET.php | 36 ++++++ .../Command/Redis/Json/JSONMGET_Test.php | 116 ++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 src/Command/Redis/Json/JSONMGET.php create mode 100644 tests/Predis/Command/Redis/Json/JSONMGET_Test.php diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 337db2d3..0fde921a 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -100,6 +100,7 @@ use Predis\Command\CommandInterface; * @method $this hvals($key) * @method $this hstrlen($key, $field) * @method $this jsonget(string $key, string $indent = '', string $newline = '', string $space = '', string ...$paths) + * @method $this jsonmget(array $keys, string $path) * @method $this jsonset(string $key, string $path, string $value, ?string $subcommand = null) * @method $this blmove(string $source, string $destination, string $where, string $to, int $timeout) * @method $this blpop(array|string $keys, $timeout) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 63fcf41c..13cc956e 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -109,6 +109,7 @@ use Predis\Response\Status; * @method array hvals(string $key) * @method int hstrlen(string $key, string $field) * @method string jsonget(string $key, string $indent = '', string $newline = '', string $space = '', string ...$paths) + * @method array jsonmget(array $keys, string $path) * @method string jsonset(string $key, string $path, string $value, ?string $subcommand = null) * @method string blmove(string $source, string $destination, string $where, string $to, int $timeout) * @method array|null blpop(array|string $keys, int|float $timeout) diff --git a/src/Command/Redis/Json/JSONMGET.php b/src/Command/Redis/Json/JSONMGET.php new file mode 100644 index 00000000..1a5163fb --- /dev/null +++ b/src/Command/Redis/Json/JSONMGET.php @@ -0,0 +1,36 @@ +getCommand(); + $command->setArguments($arguments); + + $this->assertSame($expected, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $this->assertSame(1, $this->getCommand()->parseResponse(1)); + } + + /** + * @group connected + * @dataProvider jsonProvider + * @param array $firstJson + * @param array $secondJson + * @param array $keys + * @param string $path + * @param array $expectedResponse + * @return void + * @requiresRedisJsonVersion >= 1.0.0 + */ + public function testMGetReturnsMultipleKeysArguments( + array $firstJson, + array $secondJson, + array $keys, + string $path, + array $expectedResponse + ): void { + $redis = $this->getClient(); + + $redis->jsonset(...$firstJson); + $redis->jsonset(...$secondJson); + + $this->assertSame($expectedResponse, $redis->jsonmget($keys, $path)); + } + + public function jsonProvider(): array + { + return [ + 'with both existing paths' => [ + ['key1', '$', '{"key1":"value1","key2":"value2"}'], + ['key2', '$', '{"key1":"value3","key2":"value2"}'], + ['key1', 'key2'], + '$.key1', + ['["value1"]', '["value3"]'], + ], + 'with non-existing path' => [ + ['key1', '$', '{"key1":"value1","key2":"value2"}'], + ['key2', '$', '{"key1":"value3","key3":"value3"}'], + ['key1', 'key2'], + '$.key3', + ['[]', '["value3"]'], + ], + 'with nested paths - different keys' => [ + ['key1', '$', '{"key1":"value1","key2":{"key3":"value3"}}'], + ['key2', '$', '{"key1":"value3","key2":{"key3":"value4"}}'], + ['key1', 'key2'], + '$..key3', + ['["value3"]', '["value4"]'], + ], + 'with nested paths - similar keys' => [ + ['key1', '$', '{"key1":"value1","key2":{"key1":"value3"}}'], + ['key2', '$', '{"key1":"value3","key2":{"key1":"value4"}}'], + ['key1', 'key2'], + '$..key1', + ['["value1","value3"]', '["value3","value4"]'], + ], + ]; + } +}