From 8acf18f6ca99bacaec9351d184c7e06dfac9f3fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Fri, 11 Sep 2026 18:56:59 +0200 Subject: [PATCH] feat(dsl): Add an aggregation shorthand for of(kotlinValue).unsafeCast() --- .../aggregation/operators/ValueOperators.kt | 29 +++++++++++++++++++ .../aggregation/operators/ValueOperators.kt | 29 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/dsl-template/src/commonMain/kotlin/aggregation/operators/ValueOperators.kt b/dsl-template/src/commonMain/kotlin/aggregation/operators/ValueOperators.kt index a526d0b5..5b8f20b5 100644 --- a/dsl-template/src/commonMain/kotlin/aggregation/operators/ValueOperators.kt +++ b/dsl-template/src/commonMain/kotlin/aggregation/operators/ValueOperators.kt @@ -194,6 +194,35 @@ interface ValueOperators : FieldDsl { fun of(value: BsonType): Value = BsonTypeValue(value, context) + /** + * Overwrites the represented type of this value. + * + * This method can be useful to bypass type checks. + * + * ### Example + * + * If a field has changed type over time, the DTO will use the newer type. + * However, you may still want to write a query using the old type: + * + * ```kotlin + * class User( + * val _id: ObjectId, + * val name: String, + * val age: Int, + * ) + * + * users.filter { User::age hasType BsonType.Double } + * .updateManyWithPipeline { + * set { + * User::age set User::age.unsafeCast() // Reflect the old type + * .toInt() // Convert to the new type + * } + * } + * ``` + */ + fun Any?.unsafeCast(): Value = + of(this).unsafeCast() + /** * Refers to [field] as a nested field of the current value. * diff --git a/dsl/src/commonMain/kotlin/aggregation/operators/ValueOperators.kt b/dsl/src/commonMain/kotlin/aggregation/operators/ValueOperators.kt index f6dc4705..3e961c56 100644 --- a/dsl/src/commonMain/kotlin/aggregation/operators/ValueOperators.kt +++ b/dsl/src/commonMain/kotlin/aggregation/operators/ValueOperators.kt @@ -197,6 +197,35 @@ interface ValueOperators : FieldDsl { fun of(value: BsonType): Value = BsonTypeValue(value, context) + /** + * Overwrites the represented type of this value. + * + * This method can be useful to bypass type checks. + * + * ### Example + * + * If a field has changed type over time, the DTO will use the newer type. + * However, you may still want to write a query using the old type: + * + * ```kotlin + * class User( + * val _id: ObjectId, + * val name: String, + * val age: Int, + * ) + * + * users.filter { User::age hasType BsonType.Double } + * .updateManyWithPipeline { + * set { + * User::age set User::age.unsafeCast() // Reflect the old type + * .toInt() // Convert to the new type + * } + * } + * ``` + */ + fun Any?.unsafeCast(): Value = + of(this).unsafeCast() + /** * Refers to [field] as a nested field of the current value. * -- 2.51.2