json retrive support empty path

This commit is contained in:
Yuri Kuznetsov
2023-10-23 20:59:59 +03:00
parent 6463eaa6f6
commit 33e7f4e3ef
2 changed files with 32 additions and 12 deletions

View File

@@ -29,26 +29,31 @@
namespace Espo\Core\Formula\Functions\JsonGroup;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
use Espo\Core\Formula\ArgumentList;
use Espo\Core\Formula\Exceptions\Error;
use Espo\Core\Formula\Exceptions\ExecutionException;
use Espo\Core\Formula\Exceptions\TooFewArguments;
use Espo\Core\Formula\Functions\BaseFunction;
class RetrieveType extends BaseFunction
{
/**
* @return mixed
* @throws \Espo\Core\Formula\Exceptions\TooFewArguments
* @throws \Espo\Core\Formula\Exceptions\Error
* @throws TooFewArguments
* @throws Error
* @throws ExecutionException
*/
public function process(ArgumentList $args)
{
if (count($args) < 2) {
if (count($args) < 1) {
$this->throwTooFewArguments();
}
$jsonString = $this->evaluate($args[0]);
$path = $this->evaluate($args[1]);
$path = count($args) > 1 ?
$this->evaluate($args[1]) :
'';
if (!is_string($jsonString)) {
$this->throwBadArgumentType(1, 'string');
@@ -58,10 +63,6 @@ class RetrieveType extends BaseFunction
$this->throwBadArgumentType(2, 'string');
}
if ($path === '') {
$this->throwBadArgumentValue(2);
}
$item = json_decode($jsonString);
$pathArray = $this->splitPath($path);
@@ -75,6 +76,10 @@ class RetrieveType extends BaseFunction
*/
private function splitPath(string $path): array
{
if ($path === '') {
return [];
}
/** @var string[] $pathArray */
$pathArray = preg_split('/(?<!\\\)\./', $path);

View File

@@ -585,6 +585,21 @@ class EvaluatorTest extends \PHPUnit\Framework\TestCase
$this->assertEquals('test', $result);
}
public function testJsonRetrieve8()
{
$value = (object) [
0 => 'test',
];
$expression = "json\\retrieve(\$value)";
$result = $this->evaluator->process($expression, null, (object) [
'value' => json_encode($value),
]);
$this->assertEquals($value, $result);
}
public function testNegate1()
{
$expression = "!string\contains('hello', 'test')";