From b9f235bae1b4303aa4cb249d64c17f92ab5606b7 Mon Sep 17 00:00:00 2001 From: Jens Suhr Date: Wed, 29 Jan 2025 15:52:16 +0100 Subject: [PATCH 1/2] JAVA-5774 Allow all property types for Filter.regex() --- .../mongodb/kotlin/client/model/Filters.kt | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/driver-kotlin-extensions/src/main/kotlin/com/mongodb/kotlin/client/model/Filters.kt b/driver-kotlin-extensions/src/main/kotlin/com/mongodb/kotlin/client/model/Filters.kt index 3faef6a8458..3643cb1cd91 100644 --- a/driver-kotlin-extensions/src/main/kotlin/com/mongodb/kotlin/client/model/Filters.kt +++ b/driver-kotlin-extensions/src/main/kotlin/com/mongodb/kotlin/client/model/Filters.kt @@ -434,7 +434,7 @@ public object Filters { */ @JvmSynthetic @JvmName("regexExt") - public infix fun KProperty.regex(pattern: String): Bson = Filters.regex(path(), pattern) + public infix fun KProperty.regex(pattern: String): Bson = Filters.regex(path(), pattern) /** * Creates a filter that matches all documents where the value of the property matches the given regular expression @@ -444,7 +444,7 @@ public object Filters { * @param pattern the pattern * @return the filter */ - public fun regex(property: KProperty, pattern: String): Bson = property.regex(pattern) + public fun regex(property: KProperty, pattern: String): Bson = property.regex(pattern) /** * Creates a filter that matches all documents where the value of the property matches the given regular expression @@ -455,7 +455,7 @@ public object Filters { */ @JvmSynthetic @JvmName("regexExt") - public infix fun KProperty.regex(pattern: Pattern): Bson = Filters.regex(path(), pattern) + public infix fun KProperty.regex(pattern: Pattern): Bson = Filters.regex(path(), pattern) /** * Creates a filter that matches all documents where the value of the property matches the given regular expression @@ -465,7 +465,7 @@ public object Filters { * @param pattern the pattern * @return the filter */ - public fun regex(property: KProperty, pattern: Pattern): Bson = property.regex(pattern) + public fun regex(property: KProperty, pattern: Pattern): Bson = property.regex(pattern) /** * Creates a filter that matches all documents where the value of the option matches the given regular expression @@ -477,7 +477,7 @@ public object Filters { */ @JvmSynthetic @JvmName("regexExt") - public fun KProperty.regex(pattern: String, options: String): Bson = + public fun KProperty.regex(pattern: String, options: String): Bson = Filters.regex(path(), pattern, options) /** @@ -489,7 +489,7 @@ public object Filters { * @param options the options * @return the filter */ - public fun regex(property: KProperty, pattern: String, options: String): Bson = + public fun regex(property: KProperty, pattern: String, options: String): Bson = property.regex(pattern, options) /** @@ -501,7 +501,7 @@ public object Filters { */ @JvmSynthetic @JvmName("regexExt") - public infix fun KProperty.regex(regex: Regex): Bson = Filters.regex(path(), regex.toPattern()) + public infix fun KProperty.regex(regex: Regex): Bson = Filters.regex(path(), regex.toPattern()) /** * Creates a filter that matches all documents where the value of the property matches the given regular expression @@ -511,7 +511,7 @@ public object Filters { * @param regex the regex * @return the filter */ - public fun regex(property: KProperty, regex: Regex): Bson = property.regex(regex.toPattern()) + public fun regex(property: KProperty, regex: Regex): Bson = property.regex(regex.toPattern()) /** * Creates a filter that matches all documents where the value of the property matches the given regular expression @@ -522,7 +522,7 @@ public object Filters { */ @JvmSynthetic @JvmName("regexIterableExt") - public infix fun KProperty>.regex(pattern: String): Bson = Filters.regex(path(), pattern) + public infix fun KProperty>.regex(pattern: String): Bson = Filters.regex(path(), pattern) /** * Creates a filter that matches all documents where the value of the property matches the given regular expression @@ -534,7 +534,7 @@ public object Filters { */ @JvmSynthetic @JvmName("regexIterable") - public fun regex(property: KProperty>, pattern: String): Bson = property.regex(pattern) + public fun regex(property: KProperty>, pattern: String): Bson = property.regex(pattern) /** * Creates a filter that matches all documents where the value of the property matches the given regular expression @@ -545,7 +545,7 @@ public object Filters { */ @JvmSynthetic @JvmName("regexIterableExt") - public infix fun KProperty>.regex(pattern: Pattern): Bson = Filters.regex(path(), pattern) + public infix fun KProperty>.regex(pattern: Pattern): Bson = Filters.regex(path(), pattern) /** * Creates a filter that matches all documents where the value of the property matches the given regular expression @@ -557,7 +557,7 @@ public object Filters { */ @JvmSynthetic @JvmName("regexIterable") - public fun regex(property: KProperty>, pattern: Pattern): Bson = property.regex(pattern) + public fun regex(property: KProperty>, pattern: Pattern): Bson = property.regex(pattern) /** * Creates a filter that matches all documents where the value of the option matches the given regular expression @@ -569,7 +569,7 @@ public object Filters { */ @JvmSynthetic @JvmName("regexIterableExt") - public fun KProperty>.regex(regex: String, options: String): Bson = + public fun KProperty>.regex(regex: String, options: String): Bson = Filters.regex(path(), regex, options) /** @@ -583,7 +583,7 @@ public object Filters { */ @JvmSynthetic @JvmName("regexIterable") - public fun regex(property: KProperty>, regex: String, options: String): Bson = + public fun regex(property: KProperty>, regex: String, options: String): Bson = property.regex(regex, options) /** @@ -595,7 +595,7 @@ public object Filters { */ @JvmSynthetic @JvmName("regexIterableExt") - public infix fun KProperty>.regex(regex: Regex): Bson = Filters.regex(path(), regex.toPattern()) + public infix fun KProperty>.regex(regex: Regex): Bson = Filters.regex(path(), regex.toPattern()) /** * Creates a filter that matches all documents where the value of the property matches the given regular expression @@ -607,7 +607,7 @@ public object Filters { */ @JvmSynthetic @JvmName("regexIterable") - public fun regex(property: KProperty>, regex: Regex): Bson = property.regex(regex.toPattern()) + public fun regex(property: KProperty>, regex: Regex): Bson = property.regex(regex.toPattern()) /** * Creates a filter that matches all documents matching the given the search term with the given text search From ebe9601085f1a871bf12ee13d191209c9ab8f9f4 Mon Sep 17 00:00:00 2001 From: Jens Suhr Date: Tue, 4 Feb 2025 11:12:20 +0100 Subject: [PATCH 2/2] JAVA-5774 Add a test do describe the use case for using the regex filter on a non-String (value class) property --- .../com/mongodb/kotlin/client/model/FiltersTest.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/driver-kotlin-extensions/src/test/kotlin/com/mongodb/kotlin/client/model/FiltersTest.kt b/driver-kotlin-extensions/src/test/kotlin/com/mongodb/kotlin/client/model/FiltersTest.kt index da15d6ee5af..06b348fee46 100644 --- a/driver-kotlin-extensions/src/test/kotlin/com/mongodb/kotlin/client/model/FiltersTest.kt +++ b/driver-kotlin-extensions/src/test/kotlin/com/mongodb/kotlin/client/model/FiltersTest.kt @@ -69,6 +69,10 @@ class FiltersTest { data class Person(val name: String, val age: Int, val address: List, val results: List) val person = Person("Ada", 20, listOf("St James Square", "London", "W1"), listOf(1, 2, 3)) + @JvmInline + value class Name(val value: String) + data class PersonWithValueClass(val name: Name, val age: Int, val address: List, val results: List) + @Test fun testEqSupport() { val expected = BsonDocument.parse("""{"name": "Ada"}""") @@ -427,6 +431,15 @@ class FiltersTest { kmongoDsl = Person::address.regex(pattern.toRegex(RegexOption.IGNORE_CASE).toPattern()) assertEquals(expected, kmongoDsl.document) + + // Filter on a value class + expected = BsonDocument.parse("""{"name": {"${'$'}regex": "$pattern", ${'$'}options : ""}}}""") + + bson = regex(PersonWithValueClass::name, pattern) + assertEquals(expected, bson.document) + + kmongoDsl = PersonWithValueClass::name.regex(pattern) + assertEquals(expected, kmongoDsl.document) } @Test