Skip to content

Commit 2106092

Browse files
authored
Remove specific encoder for Group and DollarGroup, use property name map (#1540)
1 parent f9400d9 commit 2106092

File tree

276 files changed

+528
-141
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

276 files changed

+528
-141
lines changed

generator/README.md

+19-6
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,31 @@ Updating the generated code can be done only by modifying the code generator, or
1010
To run the generator, you need to have PHP 8.1+ installed and Composer.
1111

1212
1. Move to the `generator` directory: `cd generator`
13-
1. Install dependencies: `composer install`
14-
1. Run the generator: `./generate`
13+
2. Install dependencies: `composer install`
14+
3. Run the generator: `./generate`
1515

1616
## Configuration
1717

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

20+
### Arguments
21+
22+
| Field | Type | Description |
23+
|---------------|---------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
24+
| `name` | `string` | The name of the argument. If it starts with `$`, the dollar is trimmed from the class property name |
25+
| `type` | list of `string` | The list of accepted types |
26+
| `description` | `string` | The description of the argument from MongoDB's documentation |
27+
| `optional` | `boolean` | Whether the argument is optional or not |
28+
| `valueMin` | `number` | The minimum value for a numeric argument |
29+
| `valueMax` | `number` | The maximum value for a numeric argument |
30+
| `variadic` | `string` | If sent, the argument is variadic. Defines the format `array` for a list or `object` for a map |
31+
| `variadicMin` | `integer` | The minimum number of arguments for a variadic parameter |
32+
| `default` | `scalar` or `array` | The default value for the argument |
33+
| `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 |
34+
2035
### Test pipelines
2136

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

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

33-
To add new test cases to operators, you can get inspiration from the official MongoDB documentation and use
34-
the `generator/js2yaml.html` web page to manually convert a pipeline array from JS to Yaml.
47+
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.

generator/config/query/geoIntersects.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ name: $geoIntersects
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/geoIntersects/'
44
type:
55
- fieldQuery
6-
encode: single
6+
encode: object
77
description: |
88
Selects geometries that intersect with a GeoJSON geometry. The 2dsphere index supports $geoIntersects.
99
arguments:
1010
-
1111
name: geometry
12+
mergeObject: true
1213
type:
1314
- geometry
1415
tests:

generator/config/query/geoWithin.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ name: $geoWithin
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/geoWithin/'
44
type:
55
- fieldQuery
6-
encode: single
6+
encode: object
77
description: |
88
Selects geometries within a bounding GeoJSON geometry. The 2dsphere and 2d indexes support $geoWithin.
99
arguments:
1010
-
1111
name: geometry
12+
mergeObject: true
1213
type:
1314
- geometry
1415
tests:

generator/config/query/near.yaml

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@ name: $near
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/near/'
44
type:
55
- fieldQuery
6-
encode: dollar_object
6+
encode: object
77
description: |
88
Returns geospatial objects in proximity to a point. Requires a geospatial index. The 2dsphere and 2d indexes support $near.
99
arguments:
1010
-
1111
name: geometry
12+
mergeObject: true
1213
type:
1314
- geometry
1415
-
15-
name: maxDistance
16+
name: $maxDistance
1617
type:
1718
- number
1819
optional: true
1920
description: |
2021
Distance in meters. Limits the results to those documents that are at most the specified distance from the center point.
2122
-
22-
name: minDistance
23+
name: $minDistance
2324
type:
2425
- number
2526
optional: true

generator/config/query/nearSphere.yaml

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@ name: $nearSphere
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/nearSphere/'
44
type:
55
- fieldQuery
6-
encode: dollar_object
6+
encode: object
77
description: |
88
Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The 2dsphere and 2d indexes support $nearSphere.
99
arguments:
1010
-
1111
name: geometry
12+
mergeObject: true
1213
type:
1314
- geometry
1415
-
15-
name: maxDistance
16+
name: $maxDistance
1617
type:
1718
- number
1819
optional: true
1920
description: |
2021
Distance in meters.
2122
-
22-
name: minDistance
23+
name: $minDistance
2324
type:
2425
- number
2526
optional: true

generator/config/query/text.yaml

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,33 @@ name: $text
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/text/'
44
type:
55
- query
6-
encode: dollar_object
6+
encode: object
77
description: |
88
Performs text search.
99
arguments:
1010
-
11-
name: search
11+
name: $search
1212
type:
1313
- string
1414
description: |
1515
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.
1616
-
17-
name: language
17+
name: $language
1818
type:
1919
- string
2020
optional: true
2121
description: |
2222
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.
2323
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.
2424
-
25-
name: caseSensitive
25+
name: $caseSensitive
2626
type:
2727
- bool
2828
optional: true
2929
description: |
3030
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.
3131
-
32-
name: diacriticSensitive
32+
name: $diacriticSensitive
3333
type:
3434
- bool
3535
optional: true

generator/config/schema.json

+8-5
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@
6161
"array",
6262
"object",
6363
"flat_object",
64-
"dollar_object",
65-
"single",
66-
"group"
64+
"single"
6765
]
6866
},
6967
"description": {
@@ -100,7 +98,7 @@
10098
"properties": {
10199
"name": {
102100
"type": "string",
103-
"pattern": "^(_?[a-z][a-zA-Z0-9]*|N)$"
101+
"pattern": "^([_$]?[a-z][a-zA-Z0-9]*|N)$"
104102
},
105103
"type": {
106104
"type": "array",
@@ -150,7 +148,7 @@
150148
"type": "number"
151149
},
152150
"valueMax": {
153-
"$comment": "The minimum value for a numeric argument.",
151+
"$comment": "The maximum value for a numeric argument.",
154152
"type": "number"
155153
},
156154
"variadic": {
@@ -169,6 +167,11 @@
169167
"default": {
170168
"$comment": "The default value for the argument.",
171169
"type": ["array", "boolean", "number", "string"]
170+
},
171+
"mergeObject": {
172+
"$comment": "Skip the name in object encoding and merge the properties of the value into the operator",
173+
"type": "boolean",
174+
"default": false
172175
}
173176
},
174177
"required": [

generator/config/stage/group.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: $group
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/group/'
44
type:
55
- stage
6-
encode: group
6+
encode: object
77
description: |
88
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.
99
arguments:
@@ -15,6 +15,7 @@ arguments:
1515
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.
1616
-
1717
name: field
18+
mergeObject: true
1819
type:
1920
- accumulator
2021
variadic: object

generator/src/Definition/ArgumentDefinition.php

+5
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
use function get_debug_type;
1010
use function is_array;
1111
use function is_string;
12+
use function ltrim;
1213
use function sprintf;
1314

1415
final class ArgumentDefinition
1516
{
17+
public string $propertyName;
1618
public VariadicType|null $variadic;
1719
public int|null $variadicMin;
1820

@@ -25,6 +27,7 @@ public function __construct(
2527
string|null $variadic = null,
2628
int|null $variadicMin = null,
2729
public mixed $default = null,
30+
public bool $mergeObject = false,
2831
) {
2932
assert($this->optional === false || $this->default === null, 'Optional arguments cannot have a default value');
3033
if (is_array($type)) {
@@ -34,6 +37,8 @@ public function __construct(
3437
}
3538
}
3639

40+
$this->propertyName = ltrim($this->name, '$');
41+
3742
if ($variadic) {
3843
$this->variadic = VariadicType::from($variadic);
3944
if ($variadicMin === null) {

generator/src/Definition/OperatorDefinition.php

-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ public function __construct(
4040
'array' => Encode::Array,
4141
'object' => Encode::Object,
4242
'flat_object' => Encode::FlatObject,
43-
'dollar_object' => Encode::DollarObject,
44-
'group' => Encode::Group,
4543
default => throw new UnexpectedValueException(sprintf('Unexpected "encode" value for operator "%s". Got "%s"', $name, $encode)),
4644
};
4745

0 commit comments

Comments
 (0)