Skip to content

DATAMONGO-2138 - Type-safe Kotlin query extension #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.data.mongodb.core.query

import kotlin.reflect.KProperty

/**
* Extension for [Criteria.is] providing an `isEqualTo` alias since `is` is a reserved keyword in Kotlin.
*
Expand All @@ -38,3 +40,18 @@ fun <T: Any?> Criteria.inValues(c: Collection<T>) : Criteria = `in`(c)
* @since 2.0
*/
fun Criteria.inValues(vararg o: Any?) : Criteria = `in`(*o)

/**
* Creates a Criteria using a KProperty as key.
* Supports nested field names with [NestedProperty].
* @author Tjeu Kayim
* @since 2.2
*/
fun where(key: KProperty<*>): Criteria = Criteria.where(nestedFieldName(key))
/**
* Add new key to the criteria chain using a KProperty.
* Supports nested field names with [NestedProperty].
* @author Tjeu Kayim
* @since 2.2
*/
infix fun Criteria.and(key: KProperty<*>): Criteria = and(nestedFieldName(key))
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.query

import kotlin.reflect.KProperty
import kotlin.reflect.KProperty1

/**
* Refer to a field in an embedded/nested document.
* @author Tjeu Kayim
* @since 2.2
*/
class NestedProperty<T, U>(
internal val parent: KProperty<U>,
internal val child: KProperty1<U, T>
) : KProperty<T> by child

/**
* Recursively construct field name for a nested property.
* @author Tjeu Kayim
*/
internal fun nestedFieldName(property: KProperty<*>): String {
return when (property) {
is NestedProperty<*, *> ->
"${nestedFieldName(property.parent)}.${property.child.name}"
else -> property.name
}
}

/**
* Builds [NestedProperty] from Property References.
* Refer to a field in an embedded/nested document.
*
* For example, referring to the field "book.author":
* ```
* Book::author / Author::name isEqualTo "Herman Melville"
* ```
* @author Tjeu Kayim
* @since 2.2
*/
operator fun <T, U> KProperty<T>.div(other: KProperty1<T, U>) =
NestedProperty(this, other)
Loading