Extended Redis JSON support by implementing JSON.MGET (#892)

* 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 <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
Co-authored-by: Till Krüss <tillkruss@users.noreply.github.com>
This commit is contained in:
Vladyslav Vildanov
2023-01-26 15:09:48 +02:00
committed by GitHub
parent 5707784900
commit 9bedabebd5
4 changed files with 154 additions and 0 deletions
+1
View File
@@ -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)
+1
View File
@@ -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)
+36
View File
@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Redis\Json;
use Predis\Command\Command as RedisCommand;
class JSONMGET extends RedisCommand
{
public function getId()
{
return 'JSON.MGET';
}
public function setArguments(array $arguments)
{
$unpackedArguments = [];
foreach ($arguments[0] as $key) {
$unpackedArguments[] = $key;
}
$unpackedArguments[] = $arguments[1];
parent::setArguments($unpackedArguments);
}
}
@@ -0,0 +1,116 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Redis\Json;
use Predis\Command\Redis\PredisCommandTestCase;
class JSONMGET_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return JSONMGET::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'JSONMGET';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$arguments = [['key1', 'key2', 'key3'], 'path'];
$expected = ['key1', 'key2', 'key3', 'path'];
$command = $this->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"]'],
],
];
}
}