Skip to content

Commit

Permalink
Rename noName to mergeObject
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Nov 28, 2024
1 parent fa8fbab commit 634cea7
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 40 deletions.
4 changes: 2 additions & 2 deletions generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The `generator/config/*.yaml` files contains the list of operators and stages th
### Arguments

| Field | Type | Description |
| `name` | `string` | The name of the argument. It can start with `$`. |
| `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. |
| `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. |
Expand All @@ -29,7 +29,7 @@ The `generator/config/*.yaml` files contains the list of operators and stages th
| `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. |
| `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. |
| `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

Expand Down
2 changes: 1 addition & 1 deletion generator/config/query/geoIntersects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description: |
arguments:
-
name: geometry
noName: true
mergeObject: true
type:
- geometry
tests:
Expand Down
2 changes: 1 addition & 1 deletion generator/config/query/geoWithin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description: |
arguments:
-
name: geometry
noName: true
mergeObject: true
type:
- geometry
tests:
Expand Down
2 changes: 1 addition & 1 deletion generator/config/query/near.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description: |
arguments:
-
name: geometry
noName: true
mergeObject: true
type:
- geometry
-
Expand Down
2 changes: 1 addition & 1 deletion generator/config/query/nearSphere.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description: |
arguments:
-
name: geometry
noName: true
mergeObject: true
type:
- geometry
-
Expand Down
4 changes: 2 additions & 2 deletions generator/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@
"$comment": "The default value for the argument.",
"type": ["array", "boolean", "number", "string"]
},
"noName": {
"$comment": "Skip the name in object encoding and merge the value object",
"mergeObject": {
"$comment": "Skip the name in object encoding and merge the properties of the value into the operator",
"type": "boolean",
"default": false
}
Expand Down
2 changes: 1 addition & 1 deletion generator/config/stage/group.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +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
noName: true
mergeObject: true
type:
- accumulator
variadic: object
Expand Down
5 changes: 4 additions & 1 deletion generator/src/Definition/ArgumentDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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

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

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

Check failure on line 39 in generator/src/Definition/ArgumentDefinition.php

View workflow job for this annotation

GitHub Actions / phpcs

Function ltrim() should not be referenced via a fallback global name, but via a use statement.

if ($variadic) {
$this->variadic = VariadicType::from($variadic);
if ($variadicMin === null) {
Expand Down
46 changes: 22 additions & 24 deletions generator/src/OperatorClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,69 +65,67 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
$encodeNames = [];
$constructor = $class->addMethod('__construct');
foreach ($operator->arguments as $argument) {
// Remove the leading $ from the argument name
$argName = ltrim($argument->name, '$');
$encodeNames[$argName] = $argument->noName ? null : $argument->name;
$encodeNames[$argument->propertyName] = $argument->mergeObject ? null : $argument->name;

$type = $this->getAcceptedTypes($argument);
foreach ($type->use as $use) {
$namespace->addUse($use);
}

$property = $class->addProperty($argName);
$property = $class->addProperty($argument->propertyName);
$property->setReadOnly();
$constructorParam = $constructor->addParameter($argName);
$constructorParam = $constructor->addParameter($argument->propertyName);
$constructorParam->setType($type->native);

if ($argument->variadic) {
$constructor->setVariadic();
$constructor->addComment('@param ' . $type->doc . ' ...$' . $argName . rtrim(' ' . $argument->description));
$constructor->addComment('@param ' . $type->doc . ' ...$' . $argument->propertyName . rtrim(' ' . $argument->description));

if ($argument->variadicMin > 0) {
$namespace->addUse(InvalidArgumentException::class);
$constructor->addBody(<<<PHP
if (\count(\${$argName}) < {$argument->variadicMin}) {
throw new InvalidArgumentException(\sprintf('Expected at least %d values for \${$argName}, got %d.', {$argument->variadicMin}, \count(\${$argName})));
if (\count(\${$argument->propertyName}) < {$argument->variadicMin}) {
throw new InvalidArgumentException(\sprintf('Expected at least %d values for \${$argument->propertyName}, got %d.', {$argument->variadicMin}, \count(\${$argument->propertyName})));
}
PHP);
}

if ($argument->variadic === VariadicType::Array) {
$property->setType('array');
$property->addComment('@var list<' . $type->doc . '> $' . $argName . rtrim(' ' . $argument->description));
$property->addComment('@var list<' . $type->doc . '> $' . $argument->propertyName . rtrim(' ' . $argument->description));
// Warn that named arguments are not supported
// @see https://psalm.dev/docs/running_psalm/issues/NamedArgumentNotAllowed/
$constructor->addComment('@no-named-arguments');
$namespace->addUseFunction('array_is_list');
$namespace->addUse(InvalidArgumentException::class);
$constructor->addBody(<<<PHP
if (! array_is_list(\${$argName})) {
throw new InvalidArgumentException('Expected \${$argName} arguments to be a list (array), named arguments are not supported');
if (! array_is_list(\${$argument->propertyName})) {
throw new InvalidArgumentException('Expected \${$argument->propertyName} arguments to be a list (array), named arguments are not supported');
}
PHP);
} elseif ($argument->variadic === VariadicType::Object) {
$namespace->addUse(stdClass::class);
$property->setType(stdClass::class);
$property->addComment('@var stdClass<' . $type->doc . '> $' . $argName . rtrim(' ' . $argument->description));
$property->addComment('@var stdClass<' . $type->doc . '> $' . $argument->propertyName . rtrim(' ' . $argument->description));
$namespace->addUseFunction('is_string');
$namespace->addUse(InvalidArgumentException::class);
$constructor->addBody(<<<PHP
foreach(\${$argName} as \$key => \$value) {
foreach(\${$argument->propertyName} as \$key => \$value) {
if (! is_string(\$key)) {
throw new InvalidArgumentException('Expected \${$argName} arguments to be a map (object), named arguments (<name>:<value>) or array unpacking ...[\'<name>\' => <value>] must be used');
throw new InvalidArgumentException('Expected \${$argument->propertyName} arguments to be a map (object), named arguments (<name>:<value>) or array unpacking ...[\'<name>\' => <value>] must be used');
}
}
\${$argName} = (object) \${$argName};
\${$argument->propertyName} = (object) \${$argument->propertyName};
PHP);
}
} else {
// Non-variadic arguments
$property->addComment('@var ' . $type->doc . ' $' . $argName . rtrim(' ' . $argument->description));
$property->addComment('@var ' . $type->doc . ' $' . $argument->propertyName . rtrim(' ' . $argument->description));
$property->setType($type->native);
$constructor->addComment('@param ' . $type->doc . ' $' . $argName . rtrim(' ' . $argument->description));
$constructor->addComment('@param ' . $type->doc . ' $' . $argument->propertyName . rtrim(' ' . $argument->description));

if ($argument->optional) {
// We use a special Optional::Undefined type to differentiate between null and undefined
Expand All @@ -142,8 +140,8 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
$namespace->addUseFunction('array_is_list');
$namespace->addUse(InvalidArgumentException::class);
$constructor->addBody(<<<PHP
if (is_array(\${$argName}) && ! array_is_list(\${$argName})) {
throw new InvalidArgumentException('Expected \${$argName} argument to be a list, got an associative array.');
if (is_array(\${$argument->propertyName}) && ! array_is_list(\${$argument->propertyName})) {
throw new InvalidArgumentException('Expected \${$argument->propertyName} argument to be a list, got an associative array.');
}
PHP);
Expand All @@ -153,8 +151,8 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
$namespace->addUseFunction('is_array');
$namespace->addUse(QueryObject::class);
$constructor->addBody(<<<PHP
if (is_array(\${$argName})) {
\${$argName} = QueryObject::create(\${$argName});
if (is_array(\${$argument->propertyName})) {
\${$argument->propertyName} = QueryObject::create(\${$argument->propertyName});
}
PHP);
Expand All @@ -164,16 +162,16 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
$namespace->addUseFunction('is_string');
$namespace->addUse(Javascript::class);
$constructor->addBody(<<<PHP
if (is_string(\${$argName})) {
\${$argName} = new Javascript(\${$argName});
if (is_string(\${$argument->propertyName})) {
\${$argument->propertyName} = new Javascript(\${$argument->propertyName});
}
PHP);
}
}

// Set property from constructor argument
$constructor->addBody('$this->' . $argName . ' = $' . $argName . ';');
$constructor->addBody('$this->' . $argument->propertyName . ' = $' . $argument->propertyName . ';');
}

if ($encodeNames !== []) {
Expand Down
11 changes: 5 additions & 6 deletions generator/src/OperatorFactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,12 @@ private function addMethod(GeneratorDefinition $definition, OperatorDefinition $
$method->addComment('@see ' . $operator->link);
$args = [];
foreach ($operator->arguments as $argument) {
$argName = ltrim($argument->name, '$');
$type = $this->getAcceptedTypes($argument);
foreach ($type->use as $use) {
$namespace->addUse($use);
}

$parameter = $method->addParameter($argName);
$parameter = $method->addParameter($argument->propertyName);
$parameter->setType($type->native);
if ($argument->variadic) {
if ($argument->variadic === VariadicType::Array) {
Expand All @@ -76,17 +75,17 @@ private function addMethod(GeneratorDefinition $definition, OperatorDefinition $
}

$method->setVariadic();
$method->addComment('@param ' . $type->doc . ' ...$' . $argName . rtrim(' ' . $argument->description));
$args[] = '...$' . $argName;
$method->addComment('@param ' . $type->doc . ' ...$' . $argument->propertyName . rtrim(' ' . $argument->description));
$args[] = '...$' . $argument->propertyName;
} else {
if ($argument->optional) {
$parameter->setDefaultValue(new Literal('Optional::Undefined'));
} elseif ($argument->default !== null) {
$parameter->setDefaultValue($argument->default);
}

$method->addComment('@param ' . $type->doc . ' $' . $argName . rtrim(' ' . $argument->description));
$args[] = '$' . $argName;
$method->addComment('@param ' . $type->doc . ' $' . $argument->propertyName . rtrim(' ' . $argument->description));
$args[] = '$' . $argument->propertyName;
}
}

Expand Down

0 comments on commit 634cea7

Please sign in to comment.