Skip to content

Commit 634cea7

Browse files
committed
Rename noName to mergeObject
1 parent fa8fbab commit 634cea7

10 files changed

+40
-40
lines changed

generator/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The `generator/config/*.yaml` files contains the list of operators and stages th
2020
### Arguments
2121

2222
| Field | Type | Description |
23-
| `name` | `string` | The name of the argument. It can start with `$`. |
23+
| `name` | `string` | The name of the argument. It can start with `$` when the aggregation operator needs it, but it will be trimmed from the class property name. |
2424
| `type` | list of `string` | The list of accepted types |
2525
| `description` | `string` | The description of the argument from MongoDB's documentation. |
2626
| `optional` | `boolean` | Whether the argument is optional or not. |
@@ -29,7 +29,7 @@ The `generator/config/*.yaml` files contains the list of operators and stages th
2929
| `variadic` | `string` | If sent, the argument is variadic. Defines the format `array` for a list or `object` for a map |
3030
| `variadicMin` | `integer` | The minimum number of arguments for a variadic parameter. |
3131
| `default` | `scalar` or `array` | The default value for the argument. |
32-
| `noName` | `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. |
32+
| `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. |
3333

3434
### Test pipelines
3535

generator/config/query/geoIntersects.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ description: |
99
arguments:
1010
-
1111
name: geometry
12-
noName: true
12+
mergeObject: true
1313
type:
1414
- geometry
1515
tests:

generator/config/query/geoWithin.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ description: |
99
arguments:
1010
-
1111
name: geometry
12-
noName: true
12+
mergeObject: true
1313
type:
1414
- geometry
1515
tests:

generator/config/query/near.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ description: |
99
arguments:
1010
-
1111
name: geometry
12-
noName: true
12+
mergeObject: true
1313
type:
1414
- geometry
1515
-

generator/config/query/nearSphere.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ description: |
99
arguments:
1010
-
1111
name: geometry
12-
noName: true
12+
mergeObject: true
1313
type:
1414
- geometry
1515
-

generator/config/schema.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@
168168
"$comment": "The default value for the argument.",
169169
"type": ["array", "boolean", "number", "string"]
170170
},
171-
"noName": {
172-
"$comment": "Skip the name in object encoding and merge the value object",
171+
"mergeObject": {
172+
"$comment": "Skip the name in object encoding and merge the properties of the value into the operator",
173173
"type": "boolean",
174174
"default": false
175175
}

generator/config/stage/group.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +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-
noName: true
18+
mergeObject: true
1919
type:
2020
- accumulator
2121
variadic: object

generator/src/Definition/ArgumentDefinition.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
final class ArgumentDefinition
1515
{
16+
public string $propertyName;
1617
public VariadicType|null $variadic;
1718
public int|null $variadicMin;
1819

@@ -25,7 +26,7 @@ public function __construct(
2526
string|null $variadic = null,
2627
int|null $variadicMin = null,
2728
public mixed $default = null,
28-
public bool $noName = false,
29+
public bool $mergeObject = false,
2930
) {
3031
assert($this->optional === false || $this->default === null, 'Optional arguments cannot have a default value');
3132
if (is_array($type)) {
@@ -35,6 +36,8 @@ public function __construct(
3536
}
3637
}
3738

39+
$this->propertyName = ltrim($this->name, '$');
40+
3841
if ($variadic) {
3942
$this->variadic = VariadicType::from($variadic);
4043
if ($variadicMin === null) {

generator/src/OperatorClassGenerator.php

+22-24
Original file line numberDiff line numberDiff line change
@@ -65,69 +65,67 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
6565
$encodeNames = [];
6666
$constructor = $class->addMethod('__construct');
6767
foreach ($operator->arguments as $argument) {
68-
// Remove the leading $ from the argument name
69-
$argName = ltrim($argument->name, '$');
70-
$encodeNames[$argName] = $argument->noName ? null : $argument->name;
68+
$encodeNames[$argument->propertyName] = $argument->mergeObject ? null : $argument->name;
7169

7270
$type = $this->getAcceptedTypes($argument);
7371
foreach ($type->use as $use) {
7472
$namespace->addUse($use);
7573
}
7674

77-
$property = $class->addProperty($argName);
75+
$property = $class->addProperty($argument->propertyName);
7876
$property->setReadOnly();
79-
$constructorParam = $constructor->addParameter($argName);
77+
$constructorParam = $constructor->addParameter($argument->propertyName);
8078
$constructorParam->setType($type->native);
8179

8280
if ($argument->variadic) {
8381
$constructor->setVariadic();
84-
$constructor->addComment('@param ' . $type->doc . ' ...$' . $argName . rtrim(' ' . $argument->description));
82+
$constructor->addComment('@param ' . $type->doc . ' ...$' . $argument->propertyName . rtrim(' ' . $argument->description));
8583

8684
if ($argument->variadicMin > 0) {
8785
$namespace->addUse(InvalidArgumentException::class);
8886
$constructor->addBody(<<<PHP
89-
if (\count(\${$argName}) < {$argument->variadicMin}) {
90-
throw new InvalidArgumentException(\sprintf('Expected at least %d values for \${$argName}, got %d.', {$argument->variadicMin}, \count(\${$argName})));
87+
if (\count(\${$argument->propertyName}) < {$argument->variadicMin}) {
88+
throw new InvalidArgumentException(\sprintf('Expected at least %d values for \${$argument->propertyName}, got %d.', {$argument->variadicMin}, \count(\${$argument->propertyName})));
9189
}
9290
9391
PHP);
9492
}
9593

9694
if ($argument->variadic === VariadicType::Array) {
9795
$property->setType('array');
98-
$property->addComment('@var list<' . $type->doc . '> $' . $argName . rtrim(' ' . $argument->description));
96+
$property->addComment('@var list<' . $type->doc . '> $' . $argument->propertyName . rtrim(' ' . $argument->description));
9997
// Warn that named arguments are not supported
10098
// @see https://psalm.dev/docs/running_psalm/issues/NamedArgumentNotAllowed/
10199
$constructor->addComment('@no-named-arguments');
102100
$namespace->addUseFunction('array_is_list');
103101
$namespace->addUse(InvalidArgumentException::class);
104102
$constructor->addBody(<<<PHP
105-
if (! array_is_list(\${$argName})) {
106-
throw new InvalidArgumentException('Expected \${$argName} arguments to be a list (array), named arguments are not supported');
103+
if (! array_is_list(\${$argument->propertyName})) {
104+
throw new InvalidArgumentException('Expected \${$argument->propertyName} arguments to be a list (array), named arguments are not supported');
107105
}
108106
109107
PHP);
110108
} elseif ($argument->variadic === VariadicType::Object) {
111109
$namespace->addUse(stdClass::class);
112110
$property->setType(stdClass::class);
113-
$property->addComment('@var stdClass<' . $type->doc . '> $' . $argName . rtrim(' ' . $argument->description));
111+
$property->addComment('@var stdClass<' . $type->doc . '> $' . $argument->propertyName . rtrim(' ' . $argument->description));
114112
$namespace->addUseFunction('is_string');
115113
$namespace->addUse(InvalidArgumentException::class);
116114
$constructor->addBody(<<<PHP
117-
foreach(\${$argName} as \$key => \$value) {
115+
foreach(\${$argument->propertyName} as \$key => \$value) {
118116
if (! is_string(\$key)) {
119-
throw new InvalidArgumentException('Expected \${$argName} arguments to be a map (object), named arguments (<name>:<value>) or array unpacking ...[\'<name>\' => <value>] must be used');
117+
throw new InvalidArgumentException('Expected \${$argument->propertyName} arguments to be a map (object), named arguments (<name>:<value>) or array unpacking ...[\'<name>\' => <value>] must be used');
120118
}
121119
}
122120
123-
\${$argName} = (object) \${$argName};
121+
\${$argument->propertyName} = (object) \${$argument->propertyName};
124122
PHP);
125123
}
126124
} else {
127125
// Non-variadic arguments
128-
$property->addComment('@var ' . $type->doc . ' $' . $argName . rtrim(' ' . $argument->description));
126+
$property->addComment('@var ' . $type->doc . ' $' . $argument->propertyName . rtrim(' ' . $argument->description));
129127
$property->setType($type->native);
130-
$constructor->addComment('@param ' . $type->doc . ' $' . $argName . rtrim(' ' . $argument->description));
128+
$constructor->addComment('@param ' . $type->doc . ' $' . $argument->propertyName . rtrim(' ' . $argument->description));
131129

132130
if ($argument->optional) {
133131
// We use a special Optional::Undefined type to differentiate between null and undefined
@@ -142,8 +140,8 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
142140
$namespace->addUseFunction('array_is_list');
143141
$namespace->addUse(InvalidArgumentException::class);
144142
$constructor->addBody(<<<PHP
145-
if (is_array(\${$argName}) && ! array_is_list(\${$argName})) {
146-
throw new InvalidArgumentException('Expected \${$argName} argument to be a list, got an associative array.');
143+
if (is_array(\${$argument->propertyName}) && ! array_is_list(\${$argument->propertyName})) {
144+
throw new InvalidArgumentException('Expected \${$argument->propertyName} argument to be a list, got an associative array.');
147145
}
148146
149147
PHP);
@@ -153,8 +151,8 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
153151
$namespace->addUseFunction('is_array');
154152
$namespace->addUse(QueryObject::class);
155153
$constructor->addBody(<<<PHP
156-
if (is_array(\${$argName})) {
157-
\${$argName} = QueryObject::create(\${$argName});
154+
if (is_array(\${$argument->propertyName})) {
155+
\${$argument->propertyName} = QueryObject::create(\${$argument->propertyName});
158156
}
159157
160158
PHP);
@@ -164,16 +162,16 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
164162
$namespace->addUseFunction('is_string');
165163
$namespace->addUse(Javascript::class);
166164
$constructor->addBody(<<<PHP
167-
if (is_string(\${$argName})) {
168-
\${$argName} = new Javascript(\${$argName});
165+
if (is_string(\${$argument->propertyName})) {
166+
\${$argument->propertyName} = new Javascript(\${$argument->propertyName});
169167
}
170168
171169
PHP);
172170
}
173171
}
174172

175173
// Set property from constructor argument
176-
$constructor->addBody('$this->' . $argName . ' = $' . $argName . ';');
174+
$constructor->addBody('$this->' . $argument->propertyName . ' = $' . $argument->propertyName . ';');
177175
}
178176

179177
if ($encodeNames !== []) {

generator/src/OperatorFactoryGenerator.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,12 @@ private function addMethod(GeneratorDefinition $definition, OperatorDefinition $
6060
$method->addComment('@see ' . $operator->link);
6161
$args = [];
6262
foreach ($operator->arguments as $argument) {
63-
$argName = ltrim($argument->name, '$');
6463
$type = $this->getAcceptedTypes($argument);
6564
foreach ($type->use as $use) {
6665
$namespace->addUse($use);
6766
}
6867

69-
$parameter = $method->addParameter($argName);
68+
$parameter = $method->addParameter($argument->propertyName);
7069
$parameter->setType($type->native);
7170
if ($argument->variadic) {
7271
if ($argument->variadic === VariadicType::Array) {
@@ -76,17 +75,17 @@ private function addMethod(GeneratorDefinition $definition, OperatorDefinition $
7675
}
7776

7877
$method->setVariadic();
79-
$method->addComment('@param ' . $type->doc . ' ...$' . $argName . rtrim(' ' . $argument->description));
80-
$args[] = '...$' . $argName;
78+
$method->addComment('@param ' . $type->doc . ' ...$' . $argument->propertyName . rtrim(' ' . $argument->description));
79+
$args[] = '...$' . $argument->propertyName;
8180
} else {
8281
if ($argument->optional) {
8382
$parameter->setDefaultValue(new Literal('Optional::Undefined'));
8483
} elseif ($argument->default !== null) {
8584
$parameter->setDefaultValue($argument->default);
8685
}
8786

88-
$method->addComment('@param ' . $type->doc . ' $' . $argName . rtrim(' ' . $argument->description));
89-
$args[] = '$' . $argName;
87+
$method->addComment('@param ' . $type->doc . ' $' . $argument->propertyName . rtrim(' ' . $argument->description));
88+
$args[] = '$' . $argument->propertyName;
9089
}
9190
}
9291

0 commit comments

Comments
 (0)