Skip to content

Commit

Permalink
Improved Mongo typed filters
Browse files Browse the repository at this point in the history
Signed-off-by: Uberto Barbini <[email protected]>
  • Loading branch information
uberto committed Aug 25, 2023
1 parent 8cec7be commit 2290ba7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ import com.mongodb.client.model.Filters
import com.ubertob.kondor.json.JsonProperty
import org.bson.conversions.Bson

infix fun <T> JsonProperty<T>.eq(value: T): Bson =
infix fun <T : CharSequence> JsonProperty<T>.eq(value: T): Bson =
Filters.eq(propName, value)

infix fun <T : Any> JsonProperty<T>.lt(value: T): Bson =
infix fun <T : Number> JsonProperty<T>.eq(value: T): Bson =
Filters.eq(propName, value)

infix fun JsonProperty<Boolean>.eq(value: Boolean): Bson =
Filters.eq(propName, value)

infix fun <T : Comparable<T>> JsonProperty<T>.lt(value: T): Bson =
Filters.lt(propName, value)

infix fun <T : Any> JsonProperty<T>.lte(value: T): Bson =
infix fun <T : Comparable<T>> JsonProperty<T>.lte(value: T): Bson =
Filters.lte(propName, value)

infix fun <T : Any> JsonProperty<T>.gt(value: T): Bson =
infix fun <T : Comparable<T>> JsonProperty<T>.gt(value: T): Bson =
Filters.gt(propName, value)

infix fun <T : Any> JsonProperty<T>.gte(value: T): Bson =
infix fun <T : Comparable<T>> JsonProperty<T>.gte(value: T): Bson =
Filters.gte(propName, value)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}


}

0 comments on commit 2290ba7

Please sign in to comment.