Skip to content

Commit 66fa295

Browse files
committed
DATAMONGO-2138 - TypedCriteria.chain()
1 parent 11c993d commit 66fa295

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/TypedCriteria.kt

+8-4
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ import kotlin.reflect.KProperty1
3535
* @since 2.2
3636
*/
3737
class TypedCriteria(
38-
val operation: Criteria.() -> Criteria,
39-
property: KProperty<*>? = null
38+
private val operation: Criteria.() -> Criteria,
39+
private val property: KProperty<*>? = null
4040
) : CriteriaDefinition {
41-
val name = property?.let(::nestedFieldName)
42-
val criteria by lazy { operation(name?.let(::Criteria) ?: Criteria()) }
41+
val criteria by lazy { chain(Criteria()) }
42+
43+
fun chain(start: Criteria): Criteria {
44+
return (if (property == null) start else start.and(nestedFieldName(property)))
45+
.operation()
46+
}
4347

4448
override fun getCriteriaObject(): Document = criteria.criteriaObject
4549

spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/TypedCriteriaExtensions.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ import org.springframework.data.mongodb.core.find
2828
* @see typedQuery
2929
*/
3030
fun typedCriteria(vararg operations: TypedCriteria): Criteria {
31-
return operations.fold(Criteria()) { chain, head ->
32-
head.operation(if (head.name == null) chain else chain.and(head.name))
33-
}
31+
return operations.fold(Criteria()) { chain, operation -> operation.chain(chain) }
3432
}
3533

3634
/**

0 commit comments

Comments
 (0)