DATAMONGO-2596 - Introduce extension to render KProperty/KPropertyPath as property path.

Original pull request: #880.
This commit is contained in:
Yoann de Martino
2020-08-05 09:00:58 +02:00
committed by Mark Paluch
parent c0581c4943
commit b879ec8c0f
2 changed files with 28 additions and 2 deletions

View File

@@ -22,12 +22,17 @@ import kotlin.reflect.KProperty1
* Abstraction of a property path consisting of [KProperty].
* @author Tjeu Kayim
* @author Mark Paluch
* @author Yoann de Martino
* @since 2.2
*/
class KPropertyPath<T, U>(
internal val parent: KProperty<U>,
internal val child: KProperty1<U, T>
) : KProperty<T> by child
) : KProperty<T> by child {
override fun toString(): String {
return asString(this)
}
}
/**
* Recursively construct field name for a nested property.
@@ -45,7 +50,7 @@ internal fun asString(property: KProperty<*>): String {
* Builds [KPropertyPath] from Property References.
* Refer to a field in an embedded/nested document.
*
* For example, referring to the field "book.author":
* For example, referring to the field "author.name":
* ```
* Book::author / Author::name isEqualTo "Herman Melville"
* ```

View File

@@ -20,6 +20,7 @@ import org.junit.Test
/**
* @author Tjeu Kayim
* @author Yoann de Martino
*/
class KPropertyPathTests {
@@ -60,6 +61,26 @@ class KPropertyPathTests {
assertThat(property).isEqualTo("entity.book.author.name")
}
@Test
fun `Convert nested KProperty to field name using toString()`() {
val property = (Book::author / Author::name).toString()
assertThat(property).isEqualTo("author.name")
}
@Test
fun `Convert triple nested KProperty to field name using toString()`() {
class Entity(val book: Book)
class AnotherEntity(val entity: Entity)
val property = (AnotherEntity::entity / Entity::book / Book::author / Author::name).toString()
assertThat(property).isEqualTo("entity.book.author.name")
}
class Book(val title: String, val author: Author)
class Author(val name: String)
}