Skip to content

Commit

Permalink
Add expression types
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Sep 27, 2023
1 parent 4a1ceea commit 8b34f89
Show file tree
Hide file tree
Showing 57 changed files with 990 additions and 552 deletions.
10 changes: 10 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ phpunit.evergreen.xml export-ignore
phpunit.xml.dist export-ignore
psalm.xml.dist export-ignore
psalm-baseline.xml export-ignore

# Keep generated files from displaying in diffs by default
# https://docs.github.com/en/repositories/working-with-files/managing-files/customizing-how-changed-files-appear-on-github
/src/Builder/Aggregation.php linguist-generated=true
/src/Builder/Aggregation/*.php linguist-generated=true
/src/Builder/Expression/*.php linguist-generated=true
/src/Builder/Query.php linguist-generated=true
/src/Builder/Query/*.php linguist-generated=true
/src/Builder/Stage.php linguist-generated=true
/src/Builder/Stage/*.php linguist-generated=true
2 changes: 1 addition & 1 deletion generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The `generator` directory is not included in `mongodb/mongodb` package and is no

## Contributing

Updating the generated code can be done only by modifying the generator code, or it's configuration.
Updating the generated code can be done only by modifying the generator code, or its configuration.

To run the generator, you need to have PHP 8.2+ installed and Composer.

Expand Down
2 changes: 1 addition & 1 deletion generator/bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ if (!file_exists(__DIR__ . '/../vendor/autoload.php')) {
require __DIR__ . '/../vendor/autoload.php';

$application = new Application();
$application->add(new GenerateCommand(__DIR__ . '/../config/config.php'));
$application->add(new GenerateCommand(__DIR__ . '/../../',__DIR__ . '/../config'));
$application->run();
79 changes: 79 additions & 0 deletions generator/config/expressions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

// Target namespace for the generated files, allows to use ::class notation without use statements

namespace MongoDB\Builder\Expression;

use MongoDB\BSON;
use MongoDB\Model\BSONArray;
use stdClass;

return [
Expression::class => [
'types' => ['mixed'],
],
FieldPath::class => [
'class' => true,
'implements' => [Expression::class],
'types' => ['string'],
],
Variable::class => [
'class' => true,
'implements' => [Expression::class],
'types' => ['string'],
],
Literal::class => [
'class' => true,
'implements' => [Expression::class],
'types' => ['mixed'],
],
ExpressionObject::class => [
'class' => true,
'implements' => [Expression::class],
'types' => ['array', stdClass::class, BSON\Document::class, BSON\Serializable::class],
],
Operator::class => [
'implements' => [Expression::class],
'types' => ['array', stdClass::class, BSON\Document::class, BSON\Serializable::class],
],
ResolvesToArray::class => [
'implements' => [Expression::class],
'types' => ['list', BSONArray::class, BSON\PackedArray::class],
],
ResolvesToBool::class => [
'implements' => [Expression::class],
'types' => ['bool'],
],
ResolvesToDate::class => [
'implements' => [Expression::class],
'types' => ['DateTimeInterface', 'UTCDateTime'],
],
ResolvesToObject::class => [
'implements' => [Expression::class],
'types' => ['array', 'object', BSON\Document::class, BSON\Serializable::class],
],
ResolvesToNull::class => [
'implements' => [Expression::class],
'types' => ['null'],
],
ResolvesToNumber::class => [
'implements' => [Expression::class],
'types' => ['int', 'float', BSON\Int64::class, BSON\Decimal128::class],
],
ResolvesToDecimal::class => [
'implements' => [ResolvesToNumber::class],
'types' => ['int', 'float', BSON\Int64::class, BSON\Decimal128::class],
],
ResolvesToFloat::class => [
'implements' => [ResolvesToNumber::class],
'types' => ['int', 'float', BSON\Int64::class],
],
ResolvesToInt::class => [
'implements' => [ResolvesToNumber::class],
'types' => ['int', BSON\Int64::class],
],
ResolvesToString::class => [
'implements' => [Expression::class],
'types' => ['string'],
],
];
17 changes: 9 additions & 8 deletions generator/config/config.php → generator/config/operators.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace MongoDB\CodeGenerator\Config;

use MongoDB\CodeGenerator\FactoryClassGenerator;
use MongoDB\CodeGenerator\ValueClassGenerator;
use MongoDB\CodeGenerator\ExpressionClassGenerator;
use MongoDB\CodeGenerator\OperatorFactoryGenerator;
use MongoDB\CodeGenerator\OperatorClassGenerator;

$src = __DIR__ . '/../../src';
$tests = __DIR__ . '/../../tests';
Expand All @@ -12,14 +13,14 @@
// Aggregation Pipeline Stages
[
'configFile' => __DIR__ . '/stages.yaml',
'generatorClass' => ValueClassGenerator::class,
'generatorClass' => OperatorClassGenerator::class,
'namespace' => 'MongoDB\\Builder\\Stage',
'filePath' => $src . '/Builder/Stage',
'classNameSuffix' => 'Stage',
],
[
'configFile' => __DIR__ . '/stages.yaml',
'generatorClass' => FactoryClassGenerator::class,
'generatorClass' => OperatorFactoryGenerator::class,
'namespace' => 'MongoDB\\Builder\\Stage',
'filePath' => $src . '/Builder/Stage',
'classNameSuffix' => 'Stage',
Expand All @@ -28,14 +29,14 @@
// Aggregation Pipeline Operators
[
'configFile' => __DIR__ . '/pipeline-operators.yaml',
'generatorClass' => ValueClassGenerator::class,
'generatorClass' => OperatorClassGenerator::class,
'namespace' => 'MongoDB\\Builder\\Aggregation',
'filePath' => $src . '/Builder/Aggregation',
'classNameSuffix' => 'Aggregation',
],
[
'configFile' => __DIR__ . '/pipeline-operators.yaml',
'generatorClass' => FactoryClassGenerator::class,
'generatorClass' => OperatorFactoryGenerator::class,
'namespace' => 'MongoDB\\Builder\\Aggregation',
'filePath' => $src . '/Builder/Aggregation',
'classNameSuffix' => 'Aggregation',
Expand All @@ -44,14 +45,14 @@
// Query Operators
[
'configFile' => __DIR__ . '/query-operators.yaml',
'generatorClass' => ValueClassGenerator::class,
'generatorClass' => OperatorClassGenerator::class,
'namespace' => 'MongoDB\\Builder\\Query',
'filePath' => $src . '/Builder/Query',
'classNameSuffix' => 'Query',
],
[
'configFile' => __DIR__ . '/query-operators.yaml',
'generatorClass' => FactoryClassGenerator::class,
'generatorClass' => OperatorFactoryGenerator::class,
'namespace' => 'MongoDB\\Builder\\Query',
'filePath' => $src . '/Builder/Query',
'classNameSuffix' => 'Query',
Expand Down
38 changes: 19 additions & 19 deletions generator/config/pipeline-operators.yaml
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
- name: and
type: resolvesToBoolExpression
type: resolvesToBool
args:
- name: expressions
type: resolvesToExpression
type: expression
isVariadic: true
variadicMin: 1
- name: eq
type: resolvesToBoolExpression
type: resolvesToBool
args:
- name: expression1
type: resolvesToExpression
type: expression
- name: expression2
type: resolvesToExpression
type: expression
- name: gt
type: resolvesToBoolExpression
type: resolvesToBool
args:
- name: expression1
type: resolvesToExpression
type: expression
- name: expression2
type: resolvesToExpression
type: expression
- name: lt
type: resolvesToBoolExpression
type: resolvesToBool
args:
- name: expression1
type: resolvesToExpression
type: expression
- name: expression2
type: resolvesToExpression
type: expression
- name: ne
type: resolvesToBoolExpression
type: resolvesToBool
args:
- name: expression1
type: resolvesToExpression
type: expression
- name: expression2
type: resolvesToExpression
type: expression
- name: filter
type: resolvesToArrayExpression
type: resolvesToArray
usesNamedArgs: true
args:
- name: input
type: resolvesToArrayExpression
type: resolvesToArray
- name: cond
type: resolvesToBoolExpression
type: resolvesToBool
- name: as
type: string
type: resolvesToString
isOptional: true
- name: limit
type: resolvesToNumberExpression
type: resolvesToInt
isOptional: true
8 changes: 4 additions & 4 deletions generator/config/query-operators.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
- name: and
type: resolvesToBoolExpression
type: resolvesToBool
args:
- name: query
type: resolvesToQueryOperator
type: resolvesToBool
isVariadic: true
- name: expr
type: resolvesToExpression
type: expression
args:
- name: expression
type: resolvesToExpression
type: expression
6 changes: 3 additions & 3 deletions generator/config/stages.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
- name: match
args:
- name: matchExpr
type: resolvesToMatchExpression
type: expression
isVariadic: true
variadicMin: 1
- name: sort
args:
- name: sortSpecification
type: resolvesToSortSpecification
type: resolvesToObject
- name: limit
args:
- name: limit
type: int
type: resolvesToInt
Loading

0 comments on commit 8b34f89

Please sign in to comment.