Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

PHPLIB-1348 Add tests on Custom Aggregation Expression Operators #35

Merged
merged 1 commit into from
Jan 18, 2024

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Jan 18, 2024

Fix PHPLIB-1348

I updated the operators accepting javascript type, to automatically convert strings into a MongoDB\BSON\Javascript object.
The $function operator has default values for args: [] and lang: 'js', because this parameters are required. The lang is the only one supported.

https://www.mongodb.com/docs/manual/reference/operator/aggregation/#custom-aggregation-expression-operators

  • $accumulator example updated to use automatic creation of Javascript objects from string
  • $function
  • $where fixed examples to be Javascript objects

$this->init = $init;
if (is_string($accumulate)) {
$accumulate = new Javascript($accumulate);
Copy link
Member Author

@GromNaN GromNaN Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MongoDB\BSON\Javascript constructor accepts a 2nd parameter $scope. I don't think we need to set a value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scope has been deprecated since 4.4 (SERVER-45460), so no need to support it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that's what I just concluded by reading the list of types.

Automatically convert string into Javascript BSON
Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I don't understand the impact of the OperatorFactoryGenerator.php change.

@@ -80,6 +80,8 @@ private function addMethod(GeneratorDefinition $definition, OperatorDefinition $
} else {
if ($argument->optional) {
$parameter->setDefaultValue(new Literal('Optional::Undefined'));
} elseif ($argument->default !== null) {
$parameter->setDefaultValue($argument->default);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the first time this logic is required? The default value in the schema was previously "string", "number", "boolean" so I would have expected some handling for this already.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a bug. The default value was set on the operator class contructor, but not on the factory method. There is duplicated code between both generators.

@@ -64,14 +64,29 @@ tests:
avgCopies:
$accumulator:
init:
$code: 'function () { return { count: 0, sum: 0 } }'
$code: |-
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted that this has no impact on tests/Builder/Accumulator/Pipelines.php apart from whitespace changes. Likewise for query/where.yaml.

@GromNaN GromNaN merged commit 471e7f7 into mongodb:0.1 Jan 18, 2024
@GromNaN GromNaN deleted the PHPLIB-1348 branch January 18, 2024 19:51
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants