From 2290ba7b387815665291adb40c63bc1edde002c8 Mon Sep 17 00:00:00 2001 From: Uberto Barbini Date: Fri, 25 Aug 2023 17:22:28 +0900 Subject: [PATCH] Improved Mongo typed filters Signed-off-by: Uberto Barbini --- .../ubertob/kondor/mongo/core/MongoFilters.kt | 16 +++++++++++----- .../ubertob/kondor/mongo/core/MongoTableTest.kt | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/kondor-mongo/src/main/kotlin/com/ubertob/kondor/mongo/core/MongoFilters.kt b/kondor-mongo/src/main/kotlin/com/ubertob/kondor/mongo/core/MongoFilters.kt index 4f8343b..25966c1 100644 --- a/kondor-mongo/src/main/kotlin/com/ubertob/kondor/mongo/core/MongoFilters.kt +++ b/kondor-mongo/src/main/kotlin/com/ubertob/kondor/mongo/core/MongoFilters.kt @@ -4,19 +4,25 @@ import com.mongodb.client.model.Filters import com.ubertob.kondor.json.JsonProperty import org.bson.conversions.Bson -infix fun JsonProperty.eq(value: T): Bson = +infix fun JsonProperty.eq(value: T): Bson = Filters.eq(propName, value) -infix fun JsonProperty.lt(value: T): Bson = +infix fun JsonProperty.eq(value: T): Bson = + Filters.eq(propName, value) + +infix fun JsonProperty.eq(value: Boolean): Bson = + Filters.eq(propName, value) + +infix fun > JsonProperty.lt(value: T): Bson = Filters.lt(propName, value) -infix fun JsonProperty.lte(value: T): Bson = +infix fun > JsonProperty.lte(value: T): Bson = Filters.lte(propName, value) -infix fun JsonProperty.gt(value: T): Bson = +infix fun > JsonProperty.gt(value: T): Bson = Filters.gt(propName, value) -infix fun JsonProperty.gte(value: T): Bson = +infix fun > JsonProperty.gte(value: T): Bson = Filters.gte(propName, value) diff --git a/kondor-mongo/src/test/kotlin/com/ubertob/kondor/mongo/core/MongoTableTest.kt b/kondor-mongo/src/test/kotlin/com/ubertob/kondor/mongo/core/MongoTableTest.kt index a1e7a8b..2f196f5 100644 --- a/kondor-mongo/src/test/kotlin/com/ubertob/kondor/mongo/core/MongoTableTest.kt +++ b/kondor-mongo/src/test/kotlin/com/ubertob/kondor/mongo/core/MongoTableTest.kt @@ -242,5 +242,22 @@ class MongoTableTest { } + @Test + fun `use typed filters`() { + + val query = mongoOperation { + val a = complexDocTable.find(JSmallClass.double eq 12.0).firstOrNull() + val b = complexDocTable.find(JSmallClass.string eq "SmallClass3").firstOrNull() + val c = complexDocTable.find(JSmallClass.int eq 9).firstOrNull() + val d = complexDocTable.find(JSmallClass.boolean eq true).firstOrNull() + + "a=${a}, b=${b}, c=${c}, d=${d}" + } + val res = write100Doc + query exec localMongo + + expectThat(res.expectSuccess()).isEqualTo("a=SmallClass(string=SmallClass12, int=12, double=12.0, boolean=true), b=SmallClass(string=SmallClass3, int=3, double=3.0, boolean=false), c=SmallClass(string=SmallClass9, int=9, double=9.0, boolean=false), d=SmallClass(string=SmallClass6, int=6, double=6.0, boolean=true)") + } + + }