Added support for JSON.MERGE command (#1304)

This commit is contained in:
Vladyslav Vildanov
2023-06-08 12:16:14 +03:00
committed by GitHub
parent a711ef96e2
commit df83c94bb3
4 changed files with 134 additions and 0 deletions
+1
View File
@@ -178,6 +178,7 @@ use Predis\Command\Redis\Container\Search\FTCURSOR;
* @method $this jsonforget(string $key, string $path = '$')
* @method $this jsonget(string $key, string $indent = '', string $newline = '', string $space = '', string ...$paths)
* @method $this jsonnumincrby(string $key, string $path, int $value)
* @method $this jsonmerge(string $key, string $path, string $value)
* @method $this jsonmget(array $keys, string $path)
* @method $this jsonobjkeys(string $key, string $path = '$')
* @method $this jsonobjlen(string $key, string $path = '$')
+1
View File
@@ -187,6 +187,7 @@ use Predis\Response\Status;
* @method int jsonforget(string $key, string $path = '$')
* @method string jsonget(string $key, string $indent = '', string $newline = '', string $space = '', string ...$paths)
* @method string jsonnumincrby(string $key, string $path, int $value)
* @method Status jsonmerge(string $key, string $path, string $value)
* @method array jsonmget(array $keys, string $path)
* @method array jsonobjkeys(string $key, string $path = '$')
* @method array jsonobjlen(string $key, string $path = '$')
+29
View File
@@ -0,0 +1,29 @@
<?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.merge/
*
* Merge a given JSON value into matching paths.
* Consequently, JSON values at matching paths are updated, deleted, or expanded with new children.
*/
class JSONMERGE extends RedisCommand
{
public function getId()
{
return 'JSON.MERGE';
}
}
@@ -0,0 +1,103 @@
<?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 JSONMERGE_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return JSONMERGE::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'JSONMERGE';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$arguments = ['key', '$..', '{"a":2}'];
$expected = ['key', '$..', '{"a":2}'];
$command = $this->getCommand();
$command->setArguments($arguments);
$this->assertSame($expected, $command->getArguments());
}
/**
* @group disconnected
*/
public function testParseResponse(): void
{
$this->assertSame(1, $this->getCommand()->parseResponse(1));
}
/**
* @dataProvider jsonProvider
* @group connected
* @param array $setArguments
* @param array $mergeArguments
* @param string $expectedResponse
* @return void
* @requiresRedisJsonVersion >= 2.6.0
*/
public function testMergeCorrectlyMergeJsonValues(
array $setArguments,
array $mergeArguments,
string $expectedResponse
): void {
$redis = $this->getClient();
$this->assertEquals('OK', $redis->jsonset(...$setArguments));
$this->assertEquals('OK', $redis->jsonmerge(...$mergeArguments));
$this->assertEquals($expectedResponse, $redis->jsonget('key'));
}
public function jsonProvider(): array
{
return [
'create non-existing value' => [
['key', '$', '{"a":2}'],
['key', '$.b', '8'],
'{"a":2,"b":8}',
],
'replace existing value' => [
['key', '$', '{"a":2}'],
['key', '$.a', '3'],
'{"a":3}',
],
'replace an array' => [
['key', '$', '{"a":[2,4,6,8]}'],
['key', '$.a', '[10,12]'],
'{"a":[10,12]}',
],
'merge in multiple-paths' => [
['key', '$', '{"f1": {"a":1}, "f2":{"a":2}}'],
['key', '$', '{"f2":{"a":3, "b":4}, "f3":[2,4,6]}'],
'{"f1":{"a":1},"f2":{"a":3,"b":4},"f3":[2,4,6]}',
],
];
}
}