Extended RedisJson support by implementing JSON.OBJLEN command (#913)

* 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.OBJLEN 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:34:59 +02:00
committed by GitHub
parent 729598e37b
commit e486f8a7e1
4 changed files with 139 additions and 0 deletions
+1
View File
@@ -104,6 +104,7 @@ use Predis\Command\CommandInterface;
* @method $this jsonmget(array $keys, string $path)
* @method $this jsonset(string $key, string $path, string $value, ?string $subcommand = null)
* @method $this jsonobjkeys(string $key, string $path = '$')
* @method $this jsonobjlen(string $key, string $path = '$')
* @method $this blmove(string $source, string $destination, string $where, string $to, int $timeout)
* @method $this blpop(array|string $keys, $timeout)
* @method $this brpop(array|string $keys, $timeout)
+1
View File
@@ -113,6 +113,7 @@ use Predis\Response\Status;
* @method array jsonmget(array $keys, string $path)
* @method string jsonset(string $key, string $path, string $value, ?string $subcommand = null)
* @method array jsonobjkeys(string $key, string $path = '$')
* @method array jsonobjlen(string $key, string $path = '$')
* @method string blmove(string $source, string $destination, string $where, string $to, int $timeout)
* @method array|null blpop(array|string $keys, int|float $timeout)
* @method array|null brpop(array|string $keys, int|float $timeout)
+28
View File
@@ -0,0 +1,28 @@
<?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;
/**
* @see https://redis.io/commands/json.objlen/
*
* Report the number of keys in the JSON object at path in key
*/
class JSONOBJLEN extends RedisCommand
{
public function getId()
{
return 'JSON.OBJLEN';
}
}
@@ -0,0 +1,109 @@
<?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 JSONOBJLEN_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return JSONOBJLEN::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'JSONOBJLEN';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$arguments = ['key', '$..'];
$expected = ['key', '$..'];
$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 $jsonArguments
* @param string $key
* @param string $path
* @param array $expectedObjectLength
* @return void
* @requiresRedisJsonVersion >= 1.0.0
*/
public function testReturnsLengthOfGivenJsonObject(
array $jsonArguments,
string $key,
string $path,
array $expectedObjectLength
): void {
$redis = $this->getClient();
$redis->jsonset(...$jsonArguments);
$this->assertSame($expectedObjectLength, $redis->jsonobjlen($key, $path));
}
public function jsonProvider(): array
{
return [
'on root level' => [
['key', '$', '{"key1":"value1","key2":"value2"}'],
'key',
'$',
[2],
],
'on nested level' => [
['key', '$', '{"key1":"value1","key2":{"key1":"value1","key2":"value2"}}'],
'key',
'$.key2',
[2],
],
'with same key on both levels' => [
['key', '$', '{"key1":{"key3":"value3","key4":"value4"},"key2":{"key1":{"key2":"value2"}}}'],
'key',
'$..key1',
[2, 1],
],
'with one of the keys not a JSON object' => [
['key', '$', '{"key1":"value1","key2":{"key1":{"key2":"value2"}}}'],
'key',
'$..key1',
[null, 1],
],
];
}
}