Skip to content

Commit

Permalink
Add Optional::Undefined for optional values
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Oct 2, 2023
1 parent cae32c0 commit d3e48cc
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 20 deletions.
3 changes: 2 additions & 1 deletion generator/src/OperatorClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use MongoDB\Builder\Stage\StageInterface;
use MongoDB\CodeGenerator\Definition\GeneratorDefinition;
use MongoDB\CodeGenerator\Definition\OperatorDefinition;
use Nette\PhpGenerator\Literal;
use Nette\PhpGenerator\PhpNamespace;

use function assert;
Expand Down Expand Up @@ -75,7 +76,7 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
PHP);
}
} elseif ($argument->isOptional) {
$constuctorParam->setDefaultValue(null);
$constuctorParam->setDefaultValue(new Literal('Optional::Undefined'));
}

$constuctor->addComment('@param ' . $type->doc . ' $' . $argument->name);
Expand Down
3 changes: 2 additions & 1 deletion generator/src/OperatorFactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use MongoDB\CodeGenerator\Definition\GeneratorDefinition;
use MongoDB\CodeGenerator\Definition\OperatorDefinition;
use Nette\PhpGenerator\Literal;
use Nette\PhpGenerator\PhpNamespace;

use function implode;
Expand Down Expand Up @@ -52,7 +53,7 @@ private function createFactoryClass(GeneratorDefinition $definition): PhpNamespa
$args[] = '...$' . $argument->name;
} else {
if ($argument->isOptional) {
$parameter->setDefaultValue(null);
$parameter->setDefaultValue(new Literal('Optional::Undefined'));
}

$method->addComment('@param ' . $type->doc . ' $' . $argument->name);
Expand Down
6 changes: 4 additions & 2 deletions generator/src/OperatorGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace MongoDB\CodeGenerator;

use MongoDB\Builder\Expression\ExpressionInterface;
use MongoDB\Builder\Optional;
use MongoDB\CodeGenerator\Definition\ArgumentDefinition;
use MongoDB\CodeGenerator\Definition\ExpressionDefinition;
use MongoDB\CodeGenerator\Definition\GeneratorDefinition;
Expand Down Expand Up @@ -112,8 +113,9 @@ final protected function generateExpressionTypes(ArgumentDefinition $arg): objec
}

if ($arg->isOptional) {
$nativeTypes[] = 'null';
$docTypes[] = 'null';
$use[] = '\\' . Optional::class;
$docTypes[] = 'Undefined';
$nativeTypes[] = Optional::class;
}

// mixed can only be used as a standalone type
Expand Down
8 changes: 4 additions & 4 deletions src/Builder/Aggregation.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions src/Builder/Aggregation/FilterAggregation.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/Builder/BuilderEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ private function encodeAsObject(ExpressionInterface|StageInterface $value): stdC
foreach (get_object_vars($value) as $key => $val) {
/** @var mixed $val */
$val = $this->encodeIfSupported($val);
// @todo check for undefined value vs null
if ($val !== null) {
if ($val !== Optional::Undefined) {
$result->{$key} = $val;
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/Builder/Optional.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace MongoDB\Builder;

/**
* Mark an argument as undefined.
*
* @internal
*/
enum Optional
{
case Undefined;
}
8 changes: 4 additions & 4 deletions tests/Builder/BuilderCodecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ public function testPerformCount(): void
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/#examples
* @dataProvider provideAggregationFilterLimit
*/
public function testAggregationFilter(?int $limit, array $expectedLimit): void
public function testAggregationFilter(array $limit, array $expectedLimit): void
{
$pipeline = new Pipeline(
Stage::project(
items: Aggregation::filter(
Expression::arrayFieldPath('items'),
Aggregation::gte(Expression::variable('item.price'), 100),
'item',
$limit,
...$limit,
),
),
);
Expand All @@ -110,12 +110,12 @@ public function testAggregationFilter(?int $limit, array $expectedLimit): void
public static function provideAggregationFilterLimit(): Generator
{
yield 'unspecified limit' => [
null,
[],
[],
];

yield 'int limit' => [
1,
[1],
['limit' => 1],
];
}
Expand Down

0 comments on commit d3e48cc

Please sign in to comment.