Skip to content

Commit 29f4bca

Browse files
committed
DATAMONGO-2138 - Typed criteria extensions where & Criteria.and
1 parent 86aa5aa commit 29f4bca

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

Diff for: spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/CriteriaExtensions.kt

+17
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.mongodb.core.query
1717

18+
import kotlin.reflect.KProperty
19+
1820
/**
1921
* Extension for [Criteria.is] providing an `isEqualTo` alias since `is` is a reserved keyword in Kotlin.
2022
*
@@ -38,3 +40,18 @@ fun <T: Any?> Criteria.inValues(c: Collection<T>) : Criteria = `in`(c)
3840
* @since 2.0
3941
*/
4042
fun Criteria.inValues(vararg o: Any?) : Criteria = `in`(*o)
43+
44+
/**
45+
* Creates a Criteria using a KProperty as key.
46+
* Supports nested field names with [NestedProperty].
47+
* @author Tjeu Kayim
48+
* @since 2.2
49+
*/
50+
fun where(key: KProperty<*>): Criteria = Criteria.where(nestedFieldName(key))
51+
/**
52+
* Add new key to the criteria chain using a KProperty.
53+
* Supports nested field names with [NestedProperty].
54+
* @author Tjeu Kayim
55+
* @since 2.2
56+
*/
57+
infix fun Criteria.and(key: KProperty<*>): Criteria = and(nestedFieldName(key))

Diff for: spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/query/CriteriaExtensionsTests.kt

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.core.query
1717

18+
import org.assertj.core.api.Assertions.assertThat
1819
import org.junit.Test
1920
import org.junit.runner.RunWith
2021
import org.mockito.Answers
@@ -24,6 +25,7 @@ import org.mockito.junit.MockitoJUnitRunner
2425

2526
/**
2627
* @author Sebastien Deleuze
28+
* @author Tjeu Kayim
2729
*/
2830
@RunWith(MockitoJUnitRunner::class)
2931
class CriteriaExtensionsTests {
@@ -87,4 +89,43 @@ class CriteriaExtensionsTests {
8789

8890
Mockito.verify(criteria, Mockito.times(1)).`in`(c)
8991
}
92+
93+
@Test
94+
fun `and(KProperty) extension should call its Java counterpart`() {
95+
96+
criteria.and(Book::title)
97+
98+
Mockito.verify(criteria, Mockito.times(1)).and("title")
99+
}
100+
101+
@Test
102+
fun `and(KProperty) extension should support nested properties`() {
103+
104+
criteria.and(Book::author / Author::name)
105+
106+
Mockito.verify(criteria, Mockito.times(1)).and("author.name")
107+
}
108+
109+
@Test
110+
fun `where(KProperty) should equal Criteria where()`() {
111+
112+
class Book(val title: String)
113+
114+
val typedCriteria = where(Book::title)
115+
val classicCriteria = Criteria.where("title")
116+
117+
assertThat(typedCriteria).isEqualTo(classicCriteria)
118+
}
119+
120+
@Test
121+
fun `where(KProperty) should support nested properties`() {
122+
123+
val typedCriteria = where(Book::author / Author::name)
124+
val classicCriteria = Criteria.where("author.name")
125+
126+
assertThat(typedCriteria).isEqualTo(classicCriteria)
127+
}
128+
129+
class Book(val title: String, val author: Author)
130+
class Author(val name: String)
90131
}

0 commit comments

Comments
 (0)