DATAMONGO-2596 - Polishing.
Refactor KPropertyPath.toString() into KProperty.asPath() extension function to allow rendering simple properties and property paths into a String property path. Original pull request: #880.
This commit is contained in:
@@ -20,6 +20,7 @@ import kotlin.reflect.KProperty1
|
||||
|
||||
/**
|
||||
* Abstraction of a property path consisting of [KProperty].
|
||||
*
|
||||
* @author Tjeu Kayim
|
||||
* @author Mark Paluch
|
||||
* @author Yoann de Martino
|
||||
@@ -28,11 +29,7 @@ import kotlin.reflect.KProperty1
|
||||
class KPropertyPath<T, U>(
|
||||
internal val parent: KProperty<U>,
|
||||
internal val child: KProperty1<U, T>
|
||||
) : KProperty<T> by child {
|
||||
override fun toString(): String {
|
||||
return asString(this)
|
||||
}
|
||||
}
|
||||
) : KProperty<T> by child
|
||||
|
||||
/**
|
||||
* Recursively construct field name for a nested property.
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2020 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
|
||||
*
|
||||
* https://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
|
||||
|
||||
/**
|
||||
* Extension for [KProperty] providing an `toPath` function to render a [KProperty] as property path.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @since 3.1
|
||||
*/
|
||||
fun KProperty<*>.toPath(): String = asString(this)
|
||||
@@ -19,8 +19,11 @@ import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.Test
|
||||
|
||||
/**
|
||||
* Unit tests for [KPropertyPath] and its extensions.
|
||||
*
|
||||
* @author Tjeu Kayim
|
||||
* @author Yoann de Martino
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class KPropertyPathTests {
|
||||
|
||||
@@ -62,21 +65,30 @@ class KPropertyPathTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Convert nested KProperty to field name using toString()`() {
|
||||
fun `Convert simple KProperty to property path using toPath`() {
|
||||
|
||||
val property = (Book::author / Author::name).toString()
|
||||
class AnotherEntity(val entity: String)
|
||||
|
||||
val property = (AnotherEntity::entity).toPath()
|
||||
|
||||
assertThat(property).isEqualTo("entity")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Convert nested KProperty to field name using toPath()`() {
|
||||
|
||||
val property = (Book::author / Author::name).toPath()
|
||||
|
||||
assertThat(property).isEqualTo("author.name")
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun `Convert triple nested KProperty to field name using toString()`() {
|
||||
fun `Convert triple nested KProperty to property path using toPath`() {
|
||||
|
||||
class Entity(val book: Book)
|
||||
class AnotherEntity(val entity: Entity)
|
||||
|
||||
val property = (AnotherEntity::entity / Entity::book / Book::author / Author::name).toString()
|
||||
val property = (AnotherEntity::entity / Entity::book / Book::author / Author::name).toPath()
|
||||
|
||||
assertThat(property).isEqualTo("entity.book.author.name")
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
* <<mongo.auditing,Reactive auditing>> enabled through `@EnableReactiveMongoAuditing`. `@EnableMongoAuditing` no longer registers `ReactiveAuditingEntityCallback`.
|
||||
* Reactive SpEL support in `@Query` and `@Aggregation` query methods.
|
||||
* Aggregation hints via `AggregationOptions.builder().hint(bson).build()`.
|
||||
* Extension Function `KProperty.asPath()` to render property references into a property path representation.
|
||||
|
||||
[[new-features.3.0]]
|
||||
== What's New in Spring Data MongoDB 3.0
|
||||
|
||||
Reference in New Issue
Block a user