Skip to content

Commit

Permalink
Make FieldPath implement ResolveToAny
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Oct 11, 2023
1 parent e51b16f commit c0790e0
Show file tree
Hide file tree
Showing 22 changed files with 86 additions and 38 deletions.
24 changes: 11 additions & 13 deletions generator/config/expressions.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
'decimal' => ['int', BSON\Int64::class, 'float', BSON\Decimal128::class],
];

// "any" accepts all the BSON types. No generic "object".
// "any" accepts all the BSON types. No generic "object" or "mixed"
$bsonTypes['any'] = array_unique(array_merge(...array_values($bsonTypes)));

// "number" accepts all the numeric types
Expand All @@ -57,15 +57,18 @@
'acceptedTypes' => $acceptedTypes,
];

if ($name !== 'any') {
$expressions[$name . 'FieldPath'] = [
'generate' => Generate::PhpClass,
'extends' => FieldPath::class,
'implements' => [$resolvesToInterface],
'acceptedTypes' => ['string'],
];
$fieldPathName = $name . 'FieldPath';
if ($name === 'any') {
$fieldPathName = 'fieldPath';
} else {
$resolvesToInterfaces[] = $resolvesToInterface;
}

$expressions[$fieldPathName] = [
'generate' => Generate::PhpClass,
'implements' => [Type\FieldPathInterface::class, $resolvesToInterface],
'acceptedTypes' => ['string'],
];
}

$expressions['resolvesToLong']['implements'] = [ResolvesToInt::class];
Expand Down Expand Up @@ -106,11 +109,6 @@
'pipeline' => [
'acceptedTypes' => [Pipeline::class, ...$bsonTypes['array']],
],
'fieldPath' => [
'generate' => Generate::PhpClass,
'implements' => [Type\ExpressionInterface::class],
'acceptedTypes' => ['string'],
],
'variable' => [
'generate' => Generate::PhpClass,
'implements' => [ResolvesToAny::class],
Expand Down
8 changes: 7 additions & 1 deletion generator/src/ExpressionClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use MongoDB\CodeGenerator\Definition\Generate;
use Nette\PhpGenerator\PhpNamespace;
use Nette\PhpGenerator\Type;
use RuntimeException;
use Throwable;

use function array_map;
use function var_export;
Expand All @@ -23,7 +25,11 @@ public function generate(ExpressionDefinition $definition): void
return;
}

$this->writeFile($this->createClassOrInterface($definition));
try {
$this->writeFile($this->createClassOrInterface($definition));
} catch (Throwable $e) {
throw new RuntimeException('Failed to generate expression class for ' . $definition->name, 0, $e);
}
}

public function createClassOrInterface(ExpressionDefinition $definition): PhpNamespace
Expand Down
4 changes: 2 additions & 2 deletions src/Builder/BuilderEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace MongoDB\Builder;

use LogicException;
use MongoDB\Builder\Expression\FieldPath;
use MongoDB\Builder\Expression\Variable;
use MongoDB\Builder\Stage\GroupStage;
use MongoDB\Builder\Type\AccumulatorInterface;
use MongoDB\Builder\Type\CombinedFieldQuery;
use MongoDB\Builder\Type\Encode;
use MongoDB\Builder\Type\ExpressionInterface;
use MongoDB\Builder\Type\FieldPathInterface;
use MongoDB\Builder\Type\FieldQueryInterface;
use MongoDB\Builder\Type\Optional;
use MongoDB\Builder\Type\OutputWindow;
Expand Down Expand Up @@ -73,7 +73,7 @@ public function encode($value): stdClass|array|string
}

// This specific encoding code if temporary until we have a generic way to encode stages and operators
if ($value instanceof FieldPath) {
if ($value instanceof FieldPathInterface) {
return '$' . $value->expression;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Builder/Expression/ArrayFieldPath.php

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

4 changes: 3 additions & 1 deletion src/Builder/Expression/BinDataFieldPath.php

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

4 changes: 3 additions & 1 deletion src/Builder/Expression/BoolFieldPath.php

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

4 changes: 3 additions & 1 deletion src/Builder/Expression/DateFieldPath.php

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

4 changes: 3 additions & 1 deletion src/Builder/Expression/DecimalFieldPath.php

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

4 changes: 3 additions & 1 deletion src/Builder/Expression/DoubleFieldPath.php

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

4 changes: 2 additions & 2 deletions src/Builder/Expression/FieldPath.php

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

4 changes: 3 additions & 1 deletion src/Builder/Expression/IntFieldPath.php

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

4 changes: 3 additions & 1 deletion src/Builder/Expression/JavascriptFieldPath.php

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

4 changes: 3 additions & 1 deletion src/Builder/Expression/LongFieldPath.php

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

4 changes: 3 additions & 1 deletion src/Builder/Expression/NullFieldPath.php

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

4 changes: 3 additions & 1 deletion src/Builder/Expression/NumberFieldPath.php

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

4 changes: 3 additions & 1 deletion src/Builder/Expression/ObjectFieldPath.php

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

4 changes: 3 additions & 1 deletion src/Builder/Expression/ObjectIdFieldPath.php

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

4 changes: 3 additions & 1 deletion src/Builder/Expression/RegexFieldPath.php

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

4 changes: 3 additions & 1 deletion src/Builder/Expression/StringFieldPath.php

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

4 changes: 3 additions & 1 deletion src/Builder/Expression/TimestampFieldPath.php

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

12 changes: 12 additions & 0 deletions src/Builder/Type/FieldPathInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace MongoDB\Builder\Type;

/**
* Aggregation expressions use field path to access fields in the input documents.
*
* @see https://www.mongodb.com/docs/manual/meta/aggregation-quick-reference/#field-paths
*/
interface FieldPathInterface extends ExpressionInterface
{
}
8 changes: 4 additions & 4 deletions src/Builder/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ Create a FieldPathInterface for encoding needs.
## Query & Field Query

The `query` are used in a `$match`, `$geoNear` or `$graphLookup` stages and `$elemMatch` operator.
The `fieldQuery` are used compose query. A query is a map of field name to filter and/or a list of other queries.
The `fieldQuery` are used compose query. A query is a map of field name to fieldQuery and/or a list of other queries.

Queries can be created with `$and`, `$or`, `$nor`, `$jsonSchema`, `$text`, `$comment` operators or with the `QueryObject`
class when composed with filter.
The factory function `Query::query()` accepts variadic arguments which can be named (for `filter` with field
class when composed with `fieldQuery`.
The factory function `Query::query()` accepts variadic arguments which can be named (for `fieldQuery` with field
path) or sequential (for `query` without field path). This function is used by stages that accept a `query` as parameter
to create the `QueryObject`. The encoder handle this object specifically to merge all the queries in a single object.

Expand Down Expand Up @@ -138,7 +138,7 @@ Stage::match(
bar: Query::no Query🚬::gt(...)),
// Use array unpacking for complex field path
...['foo.$.baz' => Query::eq(...)],
// Multiple filters on the same field
// Multiple fieldQueries on the same field
baz: Query::lt(...), Query::gt(...)],
)
```
Expand Down

0 comments on commit c0790e0

Please sign in to comment.