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

PHPLIB-1350 PHPLIB-1350 Add tests on Date Expression Operators and add Yaml tags for BSON types #34

Merged
merged 5 commits into from
Jan 19, 2024

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Jan 18, 2024

Fix PHPLIB-1350

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

  • $dateAdd
  • $dateDiff
  • $dateFromParts
  • $dateFromString
  • $dateSubtract
  • $dateToParts
  • $dateToString
  • $dateTrunc
  • $dayOfMonth using verbose date field
  • $dayOfWeek using verbose date field
  • $dayOfYear using verbose date field
  • $hour using verbose date field
  • $isoDayOfWeek using verbose date field
  • $isoWeek using verbose date field
  • $isoWeekYear using verbose date field
  • $millisecond using verbose date field
  • $minute using verbose date field
  • $month using verbose date field
  • $second using verbose date field
  • $toDate
  • $week using verbose date field
  • $year using verbose date field

dateString: '$date'
timezone: '$timezone'
# onNull: new Date(0)
onNull: 1970-01-01T00:00:00+00:00
Copy link
Member Author

Choose a reason for hiding this comment

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

I tried to represent the date using the Extended JSON format: { $date: 0 }, but this get converted to { $date: { $numberInt: 0 } } and the date is actually represented with a long { $date: { $numberLong: 0 } }.

So I ended using Yaml date, that why I configured the parser to parse dates, and added a transformation pass using array_walk_recursive to convert DateTimeInterface into MongoDB\BSON\UTCDateTime.

Copy link
Member

Choose a reason for hiding this comment

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

Just adding a note that we can leverage a custom tag for this if we run into issues with the automatic parsing of dates when we don't expect it: https://symfony.com/doc/current/components/yaml.html#parsing-and-dumping-custom-tags

Copy link
Member Author

Choose a reason for hiding this comment

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

I switched all specific BSON types to Yaml tags. That's more explicit.

@GromNaN GromNaN requested a review from alcaeus January 18, 2024 14:12
day:
# $dayOfMonth: '$date'
$dayOfMonth:
date: '$date'
Copy link
Member Author

Choose a reason for hiding this comment

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

The example is the same for all this date part operators. So I picked only the tested operator for each.

Stage::project(
expectedDeliveryDate: Expression::dateAdd(
startDate: Expression::dateFieldPath('purchaseDate'),
unit: 'day',
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.

It would be very useful to have an enum for this units. PHPLIB-1370

unit: 'day'
amount: 3
-
# $merge: 'shipping'
Copy link
Member

Choose a reason for hiding this comment

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

Noted you left a comment denoting the original since our $merge operator always uses the into argument.

Copy link
Member Author

Choose a reason for hiding this comment

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

I did this in few other place. So that if we find there is a bug, it's easier to see the original example.

Comment on lines 93 to 98
$pipeline = $test->pipeline;
array_walk_recursive($pipeline, function (mixed &$value): void {
if ($value instanceof DateTimeInterface) {
$value = new UTCDateTime($value);
}
});
Copy link
Member

Choose a reason for hiding this comment

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

This looks like it's left over from before extracting convertTypeRecursively.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed, thanks.

dateString: '$date'
timezone: '$timezone'
# onNull: new Date(0)
onNull: 1970-01-01T00:00:00+00:00
Copy link
Member

Choose a reason for hiding this comment

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

Just adding a note that we can leverage a custom tag for this if we run into issues with the automatic parsing of dates when we don't expect it: https://symfony.com/doc/current/components/yaml.html#parsing-and-dumping-custom-tags

@GromNaN GromNaN force-pushed the PHPLIB-1350 branch 2 times, most recently from 76fb890 to 2cb9464 Compare January 19, 2024 10:27
@GromNaN
Copy link
Member Author

GromNaN commented Jan 19, 2024

Added support for !date 0 Yaml tag, and !binary 'base64' for #37 (comment)

@GromNaN GromNaN requested a review from alcaeus January 19, 2024 11:17
Copy link
Member

@alcaeus alcaeus left a comment

Choose a reason for hiding this comment

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

Yaml tags look a lot better, great work!

@GromNaN GromNaN changed the title PHPLIB-1350 Add tests on Date Expression Operators PHPLIB-1350 PHPLIB-1350 Add tests on Date Expression Operators and add Yaml tags for BSON types Jan 19, 2024
@GromNaN GromNaN merged commit 149dca6 into mongodb:0.1 Jan 19, 2024
@GromNaN GromNaN deleted the PHPLIB-1350 branch January 19, 2024 13:47
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