Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for JSON.MERGE command #1304

Merged
merged 3 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ClientContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
* @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 = '$')
Expand Down
1 change: 1 addition & 0 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@
* @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 = '$')
Expand Down
29 changes: 29 additions & 0 deletions src/Command/Redis/Json/JSONMERGE.php
Original file line number Diff line number Diff line change
@@ -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';
}
}
103 changes: 103 additions & 0 deletions tests/Predis/Command/Redis/Json/JSONMERGE_Test.php
Original file line number Diff line number Diff line change
@@ -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]}',
],
];
}
}