Skip to content

Commit 447ccb2

Browse files
committed
DATAMONGO-2138 - Typed Criteria extensions
1 parent a834f29 commit 447ccb2

File tree

6 files changed

+947
-1
lines changed

6 files changed

+947
-1
lines changed

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))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.query
17+
18+
import kotlin.reflect.KProperty
19+
import kotlin.reflect.KProperty1
20+
21+
/**
22+
* Refer to a field in an embedded/nested document.
23+
* @author Tjeu Kayim
24+
* @since 2.2
25+
*/
26+
class NestedProperty<T, U>(
27+
internal val parent: KProperty<U>,
28+
internal val child: KProperty1<U, T>
29+
) : KProperty<T> by child
30+
31+
/**
32+
* Recursively construct field name for a nested property.
33+
* @author Tjeu Kayim
34+
*/
35+
internal fun nestedFieldName(property: KProperty<*>): String {
36+
return when (property) {
37+
is NestedProperty<*, *> ->
38+
"${nestedFieldName(property.parent)}.${property.child.name}"
39+
else -> property.name
40+
}
41+
}
42+
43+
/**
44+
* Builds [NestedProperty] from Property References.
45+
* Refer to a field in an embedded/nested document.
46+
*
47+
* For example, referring to the field "book.author":
48+
* ```
49+
* Book::author / Author::name isEqualTo "Herman Melville"
50+
* ```
51+
* @author Tjeu Kayim
52+
* @since 2.2
53+
*/
54+
operator fun <T, U> KProperty<T>.div(other: KProperty1<T, U>) =
55+
NestedProperty(this, other)

0 commit comments

Comments
 (0)