Skip to content

Commit 63333c5

Browse files
committed
Rename noName to mergeObject
1 parent fa8fbab commit 63333c5

10 files changed

+41
-41
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

+5-1
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,7 +27,7 @@ public function __construct(
2527
string|null $variadic = null,
2628
int|null $variadicMin = null,
2729
public mixed $default = null,
28-
public bool $noName = false,
30+
public bool $mergeObject = false,
2931
) {
3032
assert($this->optional === false || $this->default === null, 'Optional arguments cannot have a default value');
3133
if (is_array($type)) {
@@ -35,6 +37,8 @@ public function __construct(
3537
}
3638
}
3739

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

generator/src/OperatorClassGenerator.php

+22-25
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
use function assert;
2222
use function interface_exists;
23-
use function ltrim;
2423
use function rtrim;
2524
use function sprintf;
2625
use function var_export;
@@ -65,69 +64,67 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
6564
$encodeNames = [];
6665
$constructor = $class->addMethod('__construct');
6766
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;
67+
$encodeNames[$argument->propertyName] = $argument->mergeObject ? null : $argument->name;
7168

7269
$type = $this->getAcceptedTypes($argument);
7370
foreach ($type->use as $use) {
7471
$namespace->addUse($use);
7572
}
7673

77-
$property = $class->addProperty($argName);
74+
$property = $class->addProperty($argument->propertyName);
7875
$property->setReadOnly();
79-
$constructorParam = $constructor->addParameter($argName);
76+
$constructorParam = $constructor->addParameter($argument->propertyName);
8077
$constructorParam->setType($type->native);
8178

8279
if ($argument->variadic) {
8380
$constructor->setVariadic();
84-
$constructor->addComment('@param ' . $type->doc . ' ...$' . $argName . rtrim(' ' . $argument->description));
81+
$constructor->addComment('@param ' . $type->doc . ' ...$' . $argument->propertyName . rtrim(' ' . $argument->description));
8582

8683
if ($argument->variadicMin > 0) {
8784
$namespace->addUse(InvalidArgumentException::class);
8885
$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})));
86+
if (\count(\${$argument->propertyName}) < {$argument->variadicMin}) {
87+
throw new InvalidArgumentException(\sprintf('Expected at least %d values for \${$argument->propertyName}, got %d.', {$argument->variadicMin}, \count(\${$argument->propertyName})));
9188
}
9289
9390
PHP);
9491
}
9592

9693
if ($argument->variadic === VariadicType::Array) {
9794
$property->setType('array');
98-
$property->addComment('@var list<' . $type->doc . '> $' . $argName . rtrim(' ' . $argument->description));
95+
$property->addComment('@var list<' . $type->doc . '> $' . $argument->propertyName . rtrim(' ' . $argument->description));
9996
// Warn that named arguments are not supported
10097
// @see https://psalm.dev/docs/running_psalm/issues/NamedArgumentNotAllowed/
10198
$constructor->addComment('@no-named-arguments');
10299
$namespace->addUseFunction('array_is_list');
103100
$namespace->addUse(InvalidArgumentException::class);
104101
$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');
102+
if (! array_is_list(\${$argument->propertyName})) {
103+
throw new InvalidArgumentException('Expected \${$argument->propertyName} arguments to be a list (array), named arguments are not supported');
107104
}
108105
109106
PHP);
110107
} elseif ($argument->variadic === VariadicType::Object) {
111108
$namespace->addUse(stdClass::class);
112109
$property->setType(stdClass::class);
113-
$property->addComment('@var stdClass<' . $type->doc . '> $' . $argName . rtrim(' ' . $argument->description));
110+
$property->addComment('@var stdClass<' . $type->doc . '> $' . $argument->propertyName . rtrim(' ' . $argument->description));
114111
$namespace->addUseFunction('is_string');
115112
$namespace->addUse(InvalidArgumentException::class);
116113
$constructor->addBody(<<<PHP
117-
foreach(\${$argName} as \$key => \$value) {
114+
foreach(\${$argument->propertyName} as \$key => \$value) {
118115
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');
116+
throw new InvalidArgumentException('Expected \${$argument->propertyName} arguments to be a map (object), named arguments (<name>:<value>) or array unpacking ...[\'<name>\' => <value>] must be used');
120117
}
121118
}
122119
123-
\${$argName} = (object) \${$argName};
120+
\${$argument->propertyName} = (object) \${$argument->propertyName};
124121
PHP);
125122
}
126123
} else {
127124
// Non-variadic arguments
128-
$property->addComment('@var ' . $type->doc . ' $' . $argName . rtrim(' ' . $argument->description));
125+
$property->addComment('@var ' . $type->doc . ' $' . $argument->propertyName . rtrim(' ' . $argument->description));
129126
$property->setType($type->native);
130-
$constructor->addComment('@param ' . $type->doc . ' $' . $argName . rtrim(' ' . $argument->description));
127+
$constructor->addComment('@param ' . $type->doc . ' $' . $argument->propertyName . rtrim(' ' . $argument->description));
131128

132129
if ($argument->optional) {
133130
// We use a special Optional::Undefined type to differentiate between null and undefined
@@ -142,8 +139,8 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
142139
$namespace->addUseFunction('array_is_list');
143140
$namespace->addUse(InvalidArgumentException::class);
144141
$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.');
142+
if (is_array(\${$argument->propertyName}) && ! array_is_list(\${$argument->propertyName})) {
143+
throw new InvalidArgumentException('Expected \${$argument->propertyName} argument to be a list, got an associative array.');
147144
}
148145
149146
PHP);
@@ -153,8 +150,8 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
153150
$namespace->addUseFunction('is_array');
154151
$namespace->addUse(QueryObject::class);
155152
$constructor->addBody(<<<PHP
156-
if (is_array(\${$argName})) {
157-
\${$argName} = QueryObject::create(\${$argName});
153+
if (is_array(\${$argument->propertyName})) {
154+
\${$argument->propertyName} = QueryObject::create(\${$argument->propertyName});
158155
}
159156
160157
PHP);
@@ -164,16 +161,16 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
164161
$namespace->addUseFunction('is_string');
165162
$namespace->addUse(Javascript::class);
166163
$constructor->addBody(<<<PHP
167-
if (is_string(\${$argName})) {
168-
\${$argName} = new Javascript(\${$argName});
164+
if (is_string(\${$argument->propertyName})) {
165+
\${$argument->propertyName} = new Javascript(\${$argument->propertyName});
169166
}
170167
171168
PHP);
172169
}
173170
}
174171

175172
// Set property from constructor argument
176-
$constructor->addBody('$this->' . $argName . ' = $' . $argName . ';');
173+
$constructor->addBody('$this->' . $argument->propertyName . ' = $' . $argument->propertyName . ';');
177174
}
178175

179176
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)