Skip to content

Commit 4548d07

Browse files
committed
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.
1 parent b879ec8 commit 4548d07

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import kotlin.reflect.KProperty1
2020

2121
/**
2222
* Abstraction of a property path consisting of [KProperty].
23+
*
2324
* @author Tjeu Kayim
2425
* @author Mark Paluch
2526
* @author Yoann de Martino
@@ -28,11 +29,7 @@ import kotlin.reflect.KProperty1
2829
class KPropertyPath<T, U>(
2930
internal val parent: KProperty<U>,
3031
internal val child: KProperty1<U, T>
31-
) : KProperty<T> by child {
32-
override fun toString(): String {
33-
return asString(this)
34-
}
35-
}
32+
) : KProperty<T> by child
3633

3734
/**
3835
* Recursively construct field name for a nested property.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2020 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+
* https://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+
20+
/**
21+
* Extension for [KProperty] providing an `toPath` function to render a [KProperty] as property path.
22+
*
23+
* @author Mark Paluch
24+
* @since 3.1
25+
*/
26+
fun KProperty<*>.toPath(): String = asString(this)

spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/query/KPropertyPathTests.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ import org.assertj.core.api.Assertions.assertThat
1919
import org.junit.Test
2020

2121
/**
22+
* Unit tests for [KPropertyPath] and its extensions.
23+
*
2224
* @author Tjeu Kayim
2325
* @author Yoann de Martino
26+
* @author Mark Paluch
2427
*/
2528
class KPropertyPathTests {
2629

@@ -62,21 +65,30 @@ class KPropertyPathTests {
6265
}
6366

6467
@Test
65-
fun `Convert nested KProperty to field name using toString()`() {
68+
fun `Convert simple KProperty to property path using toPath`() {
6669

67-
val property = (Book::author / Author::name).toString()
70+
class AnotherEntity(val entity: String)
6871

69-
assertThat(property).isEqualTo("author.name")
72+
val property = (AnotherEntity::entity).toPath()
73+
74+
assertThat(property).isEqualTo("entity")
7075
}
7176

77+
@Test
78+
fun `Convert nested KProperty to field name using toPath()`() {
79+
80+
val property = (Book::author / Author::name).toPath()
81+
82+
assertThat(property).isEqualTo("author.name")
83+
}
7284

7385
@Test
74-
fun `Convert triple nested KProperty to field name using toString()`() {
86+
fun `Convert triple nested KProperty to property path using toPath`() {
7587

7688
class Entity(val book: Book)
7789
class AnotherEntity(val entity: Entity)
7890

79-
val property = (AnotherEntity::entity / Entity::book / Book::author / Author::name).toString()
91+
val property = (AnotherEntity::entity / Entity::book / Book::author / Author::name).toPath()
8092

8193
assertThat(property).isEqualTo("entity.book.author.name")
8294
}

src/main/asciidoc/new-features.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* <<mongo.auditing,Reactive auditing>> enabled through `@EnableReactiveMongoAuditing`. `@EnableMongoAuditing` no longer registers `ReactiveAuditingEntityCallback`.
88
* Reactive SpEL support in `@Query` and `@Aggregation` query methods.
99
* Aggregation hints via `AggregationOptions.builder().hint(bson).build()`.
10+
* Extension Function `KProperty.asPath()` to render property references into a property path representation.
1011

1112
[[new-features.3.0]]
1213
== What's New in Spring Data MongoDB 3.0

0 commit comments

Comments
 (0)