From 0f026dfb3da0bf90f20e509d0d3c669838883561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Sun, 9 Aug 2026 09:46:30 +0200 Subject: [PATCH] feat(dsl): Add $dayOfWeek (aggregation) --- .../aggregation/AggregationOperators.kt | 1 + .../operators/DateTimeValueOperators.kt | 100 +++++ .../aggregation/AggregationOperators.kt | 1 + .../operators/DateTimeValueOperators.kt | 395 ++++++++++++++++++ .../operators/DateTimeValueOperatorsTest.kt | 19 + 5 files changed, 516 insertions(+) diff --git a/dsl-template/src/commonMain/kotlin/aggregation/AggregationOperators.kt b/dsl-template/src/commonMain/kotlin/aggregation/AggregationOperators.kt index 56f1fee3..27072c03 100644 --- a/dsl-template/src/commonMain/kotlin/aggregation/AggregationOperators.kt +++ b/dsl-template/src/commonMain/kotlin/aggregation/AggregationOperators.kt @@ -138,6 +138,7 @@ import opensavvy.ktmongo.dsl.query.FilterQuery * * Date and time operators: * - [`$dayOfMonth`][DateTimeValueOperators.dayOfMonth] + * - [`$dayOfWeek`][DateTimeValueOperators.dayOfWeek] * - [`$hour`][DateTimeValueOperators.hour] * - [`$isoDayOfWeek`][DateTimeValueOperators.dayOfWeekIso] * - [`$isoWeek`][DateTimeValueOperators.weekIso] diff --git a/dsl-template/src/commonMain/kotlin/aggregation/operators/DateTimeValueOperators.kt b/dsl-template/src/commonMain/kotlin/aggregation/operators/DateTimeValueOperators.kt index ef284410..30bba561 100644 --- a/dsl-template/src/commonMain/kotlin/aggregation/operators/DateTimeValueOperators.kt +++ b/dsl-template/src/commonMain/kotlin/aggregation/operators/DateTimeValueOperators.kt @@ -1108,6 +1108,106 @@ interface DateTimeValueOperators : ValueOperators { final val Value.dayOfMonth: Value get() = UnaryOperator(context, "dayOfMonth", this) + // endregion + // region $dayOfWeek + + /** + * Returns the day of the week for a date as a number between `1` (Sunday) and `7` (Saturday). + * + * To use ISO 8601 day-of-week numbering (`1` for Monday to `7` for Sunday), see [dayOfWeekIso]. + * + * The accepted date types are [Instant], [ObjectId] and [Timestamp]. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val birthdate: Instant, + * val birthdayOfWeek: Int? = null, + * ) + * + * users.updateManyWithPipeline { + * set { + * User::birthdayOfWeek set User::birthdate.dayOfWeek + * } + * } + * ``` + * + * ### External resources + * + * - [Official documentation](https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/) + */ + @OptIn(LowLevelApi::class) + val Value.dayOfWeek: Value + get() = UnaryOperator(context, "dayOfWeek", this) + + /** + * Returns the day of the week for a date as a number between `1` (Sunday) and `7` (Saturday). + * + * To use ISO 8601 day-of-week numbering (`1` for Monday to `7` for Sunday), see [dayOfWeekIso]. + * + * The accepted date types are [Instant], [ObjectId] and [Timestamp]. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val birthdate: Instant, + * val birthdayOfWeek: Int? = null, + * ) + * + * users.updateManyWithPipeline { + * set { + * User::birthdayOfWeek set User::birthdate.dayOfWeek + * } + * } + * ``` + * + * ### External resources + * + * - [Official documentation](https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/) + */ + @OptIn(LowLevelApi::class) + @Suppress("WRONG_MODIFIER_CONTAINING_DECLARATION") + @get:JvmName("dayOfWeekOfObjectId") + final val Value.dayOfWeek: Value + get() = UnaryOperator(context, "dayOfWeek", this) + + /** + * Returns the day of the week for a date as a number between `1` (Sunday) and `7` (Saturday). + * + * To use ISO 8601 day-of-week numbering (`1` for Monday to `7` for Sunday), see [dayOfWeekIso]. + * + * The accepted date types are [Instant], [ObjectId] and [Timestamp]. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val birthdate: Instant, + * val birthdayOfWeek: Int? = null, + * ) + * + * users.updateManyWithPipeline { + * set { + * User::birthdayOfWeek set User::birthdate.dayOfWeek + * } + * } + * ``` + * + * ### External resources + * + * - [Official documentation](https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/) + */ + @OptIn(LowLevelApi::class) + @Suppress("WRONG_MODIFIER_CONTAINING_DECLARATION") + @get:JvmName("dayOfWeekOfTimestamp") + final val Value.dayOfWeek: Value + get() = UnaryOperator(context, "dayOfWeek", this) + // endregion @LowLevelApi diff --git a/dsl/src/commonMain/kotlin/aggregation/AggregationOperators.kt b/dsl/src/commonMain/kotlin/aggregation/AggregationOperators.kt index d9d337f7..bace745f 100644 --- a/dsl/src/commonMain/kotlin/aggregation/AggregationOperators.kt +++ b/dsl/src/commonMain/kotlin/aggregation/AggregationOperators.kt @@ -141,6 +141,7 @@ import opensavvy.ktmongo.dsl.query.FilterQuery * * Date and time operators: * - [`$dayOfMonth`][DateTimeValueOperators.dayOfMonth] + * - [`$dayOfWeek`][DateTimeValueOperators.dayOfWeek] * - [`$hour`][DateTimeValueOperators.hour] * - [`$isoDayOfWeek`][DateTimeValueOperators.dayOfWeekIso] * - [`$isoWeek`][DateTimeValueOperators.weekIso] diff --git a/dsl/src/commonMain/kotlin/aggregation/operators/DateTimeValueOperators.kt b/dsl/src/commonMain/kotlin/aggregation/operators/DateTimeValueOperators.kt index 97357b27..4a0986a1 100644 --- a/dsl/src/commonMain/kotlin/aggregation/operators/DateTimeValueOperators.kt +++ b/dsl/src/commonMain/kotlin/aggregation/operators/DateTimeValueOperators.kt @@ -4275,6 +4275,401 @@ interface DateTimeValueOperators : ValueOperators { final val Timestamp.dayOfMonth: Value get() = of(this).dayOfMonth + // endregion + // region $dayOfWeek + + /** + * Returns the day of the week for a date as a number between `1` (Sunday) and `7` (Saturday). + * + * To use ISO 8601 day-of-week numbering (`1` for Monday to `7` for Sunday), see [dayOfWeekIso]. + * + * The accepted date types are [Instant], [ObjectId] and [Timestamp]. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val birthdate: Instant, + * val birthdayOfWeek: Int? = null, + * ) + * + * users.updateManyWithPipeline { + * set { + * User::birthdayOfWeek set User::birthdate.dayOfWeek + * } + * } + * ``` + * + * ### External resources + * + * - [Official documentation](https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/) + */ + @OptIn(LowLevelApi::class) + val Value.dayOfWeek: Value + get() = UnaryOperator(context, "dayOfWeek", this) + + /** + * Returns the day of the week for a date as a number between `1` (Sunday) and `7` (Saturday). + * + * To use ISO 8601 day-of-week numbering (`1` for Monday to `7` for Sunday), see [dayOfWeekIso]. + * + * The accepted date types are [Instant], [ObjectId] and [Timestamp]. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val birthdate: Instant, + * val birthdayOfWeek: Int? = null, + * ) + * + * users.updateManyWithPipeline { + * set { + * User::birthdayOfWeek set User::birthdate.dayOfWeek + * } + * } + * ``` + * + * ### External resources + * + * - [Official documentation](https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/) + */ + @OptIn(LowLevelApi::class) + val opensavvy.ktmongo.dsl.path.Field.dayOfWeek: Value + get() = of(this).dayOfWeek + + /** + * Returns the day of the week for a date as a number between `1` (Sunday) and `7` (Saturday). + * + * To use ISO 8601 day-of-week numbering (`1` for Monday to `7` for Sunday), see [dayOfWeekIso]. + * + * The accepted date types are [Instant], [ObjectId] and [Timestamp]. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val birthdate: Instant, + * val birthdayOfWeek: Int? = null, + * ) + * + * users.updateManyWithPipeline { + * set { + * User::birthdayOfWeek set User::birthdate.dayOfWeek + * } + * } + * ``` + * + * ### External resources + * + * - [Official documentation](https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/) + */ + @OptIn(LowLevelApi::class) + val kotlin.reflect.KProperty1.dayOfWeek: Value + get() = of(this).dayOfWeek + + /** + * Returns the day of the week for a date as a number between `1` (Sunday) and `7` (Saturday). + * + * To use ISO 8601 day-of-week numbering (`1` for Monday to `7` for Sunday), see [dayOfWeekIso]. + * + * The accepted date types are [Instant], [ObjectId] and [Timestamp]. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val birthdate: Instant, + * val birthdayOfWeek: Int? = null, + * ) + * + * users.updateManyWithPipeline { + * set { + * User::birthdayOfWeek set User::birthdate.dayOfWeek + * } + * } + * ``` + * + * ### External resources + * + * - [Official documentation](https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/) + */ + @kotlin.internal.LowPriorityInOverloadResolution + @Suppress("INVISIBLE_REFERENCE") + @OptIn(LowLevelApi::class) + val Instant.dayOfWeek: Value + get() = of(this).dayOfWeek + + /** + * Returns the day of the week for a date as a number between `1` (Sunday) and `7` (Saturday). + * + * To use ISO 8601 day-of-week numbering (`1` for Monday to `7` for Sunday), see [dayOfWeekIso]. + * + * The accepted date types are [Instant], [ObjectId] and [Timestamp]. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val birthdate: Instant, + * val birthdayOfWeek: Int? = null, + * ) + * + * users.updateManyWithPipeline { + * set { + * User::birthdayOfWeek set User::birthdate.dayOfWeek + * } + * } + * ``` + * + * ### External resources + * + * - [Official documentation](https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/) + */ + @OptIn(LowLevelApi::class) + @Suppress("WRONG_MODIFIER_CONTAINING_DECLARATION") + @get:JvmName("dayOfWeekOfObjectId") + final val Value.dayOfWeek: Value + get() = UnaryOperator(context, "dayOfWeek", this) + + /** + * Returns the day of the week for a date as a number between `1` (Sunday) and `7` (Saturday). + * + * To use ISO 8601 day-of-week numbering (`1` for Monday to `7` for Sunday), see [dayOfWeekIso]. + * + * The accepted date types are [Instant], [ObjectId] and [Timestamp]. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val birthdate: Instant, + * val birthdayOfWeek: Int? = null, + * ) + * + * users.updateManyWithPipeline { + * set { + * User::birthdayOfWeek set User::birthdate.dayOfWeek + * } + * } + * ``` + * + * ### External resources + * + * - [Official documentation](https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/) + */ + @OptIn(LowLevelApi::class) + @Suppress("WRONG_MODIFIER_CONTAINING_DECLARATION") + @get:JvmName("dayOfWeekOfObjectId") + final val opensavvy.ktmongo.dsl.path.Field.dayOfWeek: Value + get() = of(this).dayOfWeek + + /** + * Returns the day of the week for a date as a number between `1` (Sunday) and `7` (Saturday). + * + * To use ISO 8601 day-of-week numbering (`1` for Monday to `7` for Sunday), see [dayOfWeekIso]. + * + * The accepted date types are [Instant], [ObjectId] and [Timestamp]. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val birthdate: Instant, + * val birthdayOfWeek: Int? = null, + * ) + * + * users.updateManyWithPipeline { + * set { + * User::birthdayOfWeek set User::birthdate.dayOfWeek + * } + * } + * ``` + * + * ### External resources + * + * - [Official documentation](https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/) + */ + @OptIn(LowLevelApi::class) + @Suppress("WRONG_MODIFIER_CONTAINING_DECLARATION") + @get:JvmName("dayOfWeekOfObjectId") + final val kotlin.reflect.KProperty1.dayOfWeek: Value + get() = of(this).dayOfWeek + + /** + * Returns the day of the week for a date as a number between `1` (Sunday) and `7` (Saturday). + * + * To use ISO 8601 day-of-week numbering (`1` for Monday to `7` for Sunday), see [dayOfWeekIso]. + * + * The accepted date types are [Instant], [ObjectId] and [Timestamp]. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val birthdate: Instant, + * val birthdayOfWeek: Int? = null, + * ) + * + * users.updateManyWithPipeline { + * set { + * User::birthdayOfWeek set User::birthdate.dayOfWeek + * } + * } + * ``` + * + * ### External resources + * + * - [Official documentation](https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/) + */ + @kotlin.internal.LowPriorityInOverloadResolution + @OptIn(LowLevelApi::class) + @Suppress("INVISIBLE_REFERENCE", "WRONG_MODIFIER_CONTAINING_DECLARATION") + @get:JvmName("dayOfWeekOfObjectId") + final val ObjectId.dayOfWeek: Value + get() = of(this).dayOfWeek + + /** + * Returns the day of the week for a date as a number between `1` (Sunday) and `7` (Saturday). + * + * To use ISO 8601 day-of-week numbering (`1` for Monday to `7` for Sunday), see [dayOfWeekIso]. + * + * The accepted date types are [Instant], [ObjectId] and [Timestamp]. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val birthdate: Instant, + * val birthdayOfWeek: Int? = null, + * ) + * + * users.updateManyWithPipeline { + * set { + * User::birthdayOfWeek set User::birthdate.dayOfWeek + * } + * } + * ``` + * + * ### External resources + * + * - [Official documentation](https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/) + */ + @OptIn(LowLevelApi::class) + @Suppress("WRONG_MODIFIER_CONTAINING_DECLARATION") + @get:JvmName("dayOfWeekOfTimestamp") + final val Value.dayOfWeek: Value + get() = UnaryOperator(context, "dayOfWeek", this) + + /** + * Returns the day of the week for a date as a number between `1` (Sunday) and `7` (Saturday). + * + * To use ISO 8601 day-of-week numbering (`1` for Monday to `7` for Sunday), see [dayOfWeekIso]. + * + * The accepted date types are [Instant], [ObjectId] and [Timestamp]. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val birthdate: Instant, + * val birthdayOfWeek: Int? = null, + * ) + * + * users.updateManyWithPipeline { + * set { + * User::birthdayOfWeek set User::birthdate.dayOfWeek + * } + * } + * ``` + * + * ### External resources + * + * - [Official documentation](https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/) + */ + @OptIn(LowLevelApi::class) + @Suppress("WRONG_MODIFIER_CONTAINING_DECLARATION") + @get:JvmName("dayOfWeekOfTimestamp") + final val opensavvy.ktmongo.dsl.path.Field.dayOfWeek: Value + get() = of(this).dayOfWeek + + /** + * Returns the day of the week for a date as a number between `1` (Sunday) and `7` (Saturday). + * + * To use ISO 8601 day-of-week numbering (`1` for Monday to `7` for Sunday), see [dayOfWeekIso]. + * + * The accepted date types are [Instant], [ObjectId] and [Timestamp]. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val birthdate: Instant, + * val birthdayOfWeek: Int? = null, + * ) + * + * users.updateManyWithPipeline { + * set { + * User::birthdayOfWeek set User::birthdate.dayOfWeek + * } + * } + * ``` + * + * ### External resources + * + * - [Official documentation](https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/) + */ + @OptIn(LowLevelApi::class) + @Suppress("WRONG_MODIFIER_CONTAINING_DECLARATION") + @get:JvmName("dayOfWeekOfTimestamp") + final val kotlin.reflect.KProperty1.dayOfWeek: Value + get() = of(this).dayOfWeek + + /** + * Returns the day of the week for a date as a number between `1` (Sunday) and `7` (Saturday). + * + * To use ISO 8601 day-of-week numbering (`1` for Monday to `7` for Sunday), see [dayOfWeekIso]. + * + * The accepted date types are [Instant], [ObjectId] and [Timestamp]. + * + * ### Example + * + * ```kotlin + * class User( + * val name: String, + * val birthdate: Instant, + * val birthdayOfWeek: Int? = null, + * ) + * + * users.updateManyWithPipeline { + * set { + * User::birthdayOfWeek set User::birthdate.dayOfWeek + * } + * } + * ``` + * + * ### External resources + * + * - [Official documentation](https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/) + */ + @kotlin.internal.LowPriorityInOverloadResolution + @OptIn(LowLevelApi::class) + @Suppress("INVISIBLE_REFERENCE", "WRONG_MODIFIER_CONTAINING_DECLARATION") + @get:JvmName("dayOfWeekOfTimestamp") + final val Timestamp.dayOfWeek: Value + get() = of(this).dayOfWeek + // endregion @LowLevelApi diff --git a/dsl/src/commonTest/kotlin/aggregation/operators/DateTimeValueOperatorsTest.kt b/dsl/src/commonTest/kotlin/aggregation/operators/DateTimeValueOperatorsTest.kt index a0dfd34c..291fcf30 100644 --- a/dsl/src/commonTest/kotlin/aggregation/operators/DateTimeValueOperatorsTest.kt +++ b/dsl/src/commonTest/kotlin/aggregation/operators/DateTimeValueOperatorsTest.kt @@ -36,6 +36,7 @@ val DateTimeValueOperatorsTest by multiContextSuite { val dayOfWeekIso: Int, val hour: Int, val dayOfMonth: Int, + val dayOfWeek: Int, ) test($$"$year") { @@ -236,4 +237,22 @@ val DateTimeValueOperatorsTest by multiContextSuite { """.trimIndent()) } + test($$"$dayOfWeek") { + TestPipeline() + .set { + Target::dayOfWeek set Target::date.dayOfWeek + } + .shouldBeBson($$""" + [ + { + "$set": { + "dayOfWeek": { + "$dayOfWeek": "$date" + } + } + } + ] + """.trimIndent()) + } + } -- 2.51.2