Skip to content

Commit

Permalink
Remove specific encoder for Group and DollarGroup, use property n…
Browse files Browse the repository at this point in the history
…ame map (#1540)
  • Loading branch information
GromNaN authored Dec 2, 2024
1 parent f9400d9 commit 2106092
Show file tree
Hide file tree
Showing 276 changed files with 528 additions and 141 deletions.
25 changes: 19 additions & 6 deletions generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,31 @@ Updating the generated code can be done only by modifying the code generator, or
To run the generator, you need to have PHP 8.1+ installed and Composer.

1. Move to the `generator` directory: `cd generator`
1. Install dependencies: `composer install`
1. Run the generator: `./generate`
2. Install dependencies: `composer install`
3. Run the generator: `./generate`

## Configuration

The `generator/config/*.yaml` files contains the list of operators and stages that are supported by the library.

### Arguments

| Field | Type | Description |
|---------------|---------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `name` | `string` | The name of the argument. If it starts with `$`, the dollar is trimmed from the class property name |
| `type` | list of `string` | The list of accepted types |
| `description` | `string` | The description of the argument from MongoDB's documentation |
| `optional` | `boolean` | Whether the argument is optional or not |
| `valueMin` | `number` | The minimum value for a numeric argument |
| `valueMax` | `number` | The maximum value for a numeric argument |
| `variadic` | `string` | If sent, the argument is variadic. Defines the format `array` for a list or `object` for a map |
| `variadicMin` | `integer` | The minimum number of arguments for a variadic parameter |
| `default` | `scalar` or `array` | The default value for the argument |
| `mergeObject` | `bool` | Default `false`. If `true`, the value must be an object and the properties of the value object are merged into the parent operator. `$group` stage uses it for the fields |

### Test pipelines

Each operator can contain a `tests` section with a list if pipelines. To represent specific BSON objects,
it is necessary to use Yaml tags:
Each operator can contain a `tests` section with a list if pipelines. To represent specific BSON objects, it is necessary to use Yaml tags:

| BSON Type | Example |
|-------------|--------------------------------------------------------|
Expand All @@ -30,5 +44,4 @@ it is necessary to use Yaml tags:
| UTCDateTime | `!bson_utcdatetime 0` |
| Binary | `!bson_binary 'IA=='` |

To add new test cases to operators, you can get inspiration from the official MongoDB documentation and use
the `generator/js2yaml.html` web page to manually convert a pipeline array from JS to Yaml.
To add new test cases to operators, you can get inspiration from the official MongoDB documentation and use the `generator/js2yaml.html` web page to manually convert a pipeline array from JS to Yaml.
3 changes: 2 additions & 1 deletion generator/config/query/geoIntersects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ name: $geoIntersects
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/geoIntersects/'
type:
- fieldQuery
encode: single
encode: object
description: |
Selects geometries that intersect with a GeoJSON geometry. The 2dsphere index supports $geoIntersects.
arguments:
-
name: geometry
mergeObject: true
type:
- geometry
tests:
Expand Down
3 changes: 2 additions & 1 deletion generator/config/query/geoWithin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ name: $geoWithin
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/geoWithin/'
type:
- fieldQuery
encode: single
encode: object
description: |
Selects geometries within a bounding GeoJSON geometry. The 2dsphere and 2d indexes support $geoWithin.
arguments:
-
name: geometry
mergeObject: true
type:
- geometry
tests:
Expand Down
7 changes: 4 additions & 3 deletions generator/config/query/near.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ name: $near
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/near/'
type:
- fieldQuery
encode: dollar_object
encode: object
description: |
Returns geospatial objects in proximity to a point. Requires a geospatial index. The 2dsphere and 2d indexes support $near.
arguments:
-
name: geometry
mergeObject: true
type:
- geometry
-
name: maxDistance
name: $maxDistance
type:
- number
optional: true
description: |
Distance in meters. Limits the results to those documents that are at most the specified distance from the center point.
-
name: minDistance
name: $minDistance
type:
- number
optional: true
Expand Down
7 changes: 4 additions & 3 deletions generator/config/query/nearSphere.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ name: $nearSphere
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/nearSphere/'
type:
- fieldQuery
encode: dollar_object
encode: object
description: |
Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The 2dsphere and 2d indexes support $nearSphere.
arguments:
-
name: geometry
mergeObject: true
type:
- geometry
-
name: maxDistance
name: $maxDistance
type:
- number
optional: true
description: |
Distance in meters.
-
name: minDistance
name: $minDistance
type:
- number
optional: true
Expand Down
10 changes: 5 additions & 5 deletions generator/config/query/text.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@ name: $text
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/text/'
type:
- query
encode: dollar_object
encode: object
description: |
Performs text search.
arguments:
-
name: search
name: $search
type:
- string
description: |
A string of terms that MongoDB parses and uses to query the text index. MongoDB performs a logical OR search of the terms unless specified as a phrase.
-
name: language
name: $language
type:
- string
optional: true
description: |
The language that determines the list of stop words for the search and the rules for the stemmer and tokenizer. If not specified, the search uses the default language of the index.
If you specify a default_language value of none, then the text index parses through each word in the field, including stop words, and ignores suffix stemming.
-
name: caseSensitive
name: $caseSensitive
type:
- bool
optional: true
description: |
A boolean flag to enable or disable case sensitive search. Defaults to false; i.e. the search defers to the case insensitivity of the text index.
-
name: diacriticSensitive
name: $diacriticSensitive
type:
- bool
optional: true
Expand Down
13 changes: 8 additions & 5 deletions generator/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@
"array",
"object",
"flat_object",
"dollar_object",
"single",
"group"
"single"
]
},
"description": {
Expand Down Expand Up @@ -100,7 +98,7 @@
"properties": {
"name": {
"type": "string",
"pattern": "^(_?[a-z][a-zA-Z0-9]*|N)$"
"pattern": "^([_$]?[a-z][a-zA-Z0-9]*|N)$"
},
"type": {
"type": "array",
Expand Down Expand Up @@ -150,7 +148,7 @@
"type": "number"
},
"valueMax": {
"$comment": "The minimum value for a numeric argument.",
"$comment": "The maximum value for a numeric argument.",
"type": "number"
},
"variadic": {
Expand All @@ -169,6 +167,11 @@
"default": {
"$comment": "The default value for the argument.",
"type": ["array", "boolean", "number", "string"]
},
"mergeObject": {
"$comment": "Skip the name in object encoding and merge the properties of the value into the operator",
"type": "boolean",
"default": false
}
},
"required": [
Expand Down
3 changes: 2 additions & 1 deletion generator/config/stage/group.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: $group
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/group/'
type:
- stage
encode: group
encode: object
description: |
Groups input documents by a specified identifier expression and applies the accumulator expression(s), if specified, to each group. Consumes all input documents and outputs one document per each distinct group. The output documents only contain the identifier field and, if specified, accumulated fields.
arguments:
Expand All @@ -15,6 +15,7 @@ arguments:
The _id expression specifies the group key. If you specify an _id value of null, or any other constant value, the $group stage returns a single document that aggregates values across all of the input documents.
-
name: field
mergeObject: true
type:
- accumulator
variadic: object
Expand Down
5 changes: 5 additions & 0 deletions generator/src/Definition/ArgumentDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
use function get_debug_type;
use function is_array;
use function is_string;
use function ltrim;
use function sprintf;

final class ArgumentDefinition
{
public string $propertyName;
public VariadicType|null $variadic;
public int|null $variadicMin;

Expand All @@ -25,6 +27,7 @@ public function __construct(
string|null $variadic = null,
int|null $variadicMin = null,
public mixed $default = null,
public bool $mergeObject = false,
) {
assert($this->optional === false || $this->default === null, 'Optional arguments cannot have a default value');
if (is_array($type)) {
Expand All @@ -34,6 +37,8 @@ public function __construct(
}
}

$this->propertyName = ltrim($this->name, '$');

if ($variadic) {
$this->variadic = VariadicType::from($variadic);
if ($variadicMin === null) {
Expand Down
2 changes: 0 additions & 2 deletions generator/src/Definition/OperatorDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public function __construct(
'array' => Encode::Array,
'object' => Encode::Object,
'flat_object' => Encode::FlatObject,
'dollar_object' => Encode::DollarObject,
'group' => Encode::Group,
default => throw new UnexpectedValueException(sprintf('Unexpected "encode" value for operator "%s". Got "%s"', $name, $encode)),
};

Expand Down
Loading

0 comments on commit 2106092

Please sign in to comment.