Skip to content

Commit c1cd613

Browse files
committed
Polishing.
Add since tags. Reformat code. Switch to Collection-typed properties as Iterable is often used in domain models without being an actual mapped collection. See #3010 Original pull request: #3241
1 parent 11bdb9a commit c1cd613

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

Diff for: src/main/kotlin/org/springframework/data/mapping/KPropertyPath.kt

+9-14
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ private class KPropertyPath<T, U>(
3434
/**
3535
* Abstraction of a property path that consists of parent [KProperty],
3636
* and child property [KProperty], where parent [parent] has an [Iterable]
37-
* of children, so it represents 1-M mapping, not 1-1, like [KPropertyPath]
37+
* of children, so it represents 1-M mapping.
3838
*
3939
* @author Mikhail Polivakha
40+
* @since 3.5
4041
*/
4142
internal class KIterablePropertyPath<T, U>(
4243
val parent: KProperty<Iterable<U?>?>,
@@ -52,8 +53,8 @@ internal fun asString(property: KProperty<*>): String {
5253
return when (property) {
5354
is KPropertyPath<*, *> ->
5455
"${asString(property.parent)}.${property.child.name}"
55-
is KIterablePropertyPath<*, *> ->
56-
"${asString(property.parent)}.${property.child.name}"
56+
is KIterablePropertyPath<*, *> ->
57+
"${asString(property.parent)}.${property.child.name}"
5758
else -> property.name
5859
}
5960
}
@@ -81,21 +82,15 @@ operator fun <T, U> KProperty<T?>.div(other: KProperty1<T, U>): KProperty<U> =
8182
* Note, that this function is different from [div] above in the
8283
* way that it represents a division operator overloading for
8384
* child references, where parent to child reference relation is 1-M, not 1-1.
84-
* It implies that parent has an [Iterable] or any liner [Collection] of children.
85+
* It implies that parent defines a [Collection] of children.
8586
**
86-
* For example, referring to the field "addresses.street":
87-
* ```
88-
* User::addresses / Author::street contains "Austin"
87+
* For example, referring to the field "books.title":
8988
* ```
90-
*
91-
* And the entities may look like this:
92-
* ```
93-
* class User(val addresses: List<Address>)
94-
*
95-
* class Address(val street: String)
89+
* Author::books / Book::title contains "Bartleby"
9690
* ```
9791
* @author Mikhail Polivakha
92+
* @since 3.5
9893
*/
9994
@JvmName("divIterable")
100-
operator fun <T, U> KProperty<Iterable<T?>?>.div(other: KProperty1<T, U>): KProperty<U> =
95+
operator fun <T, U> KProperty<Collection<T?>?>.div(other: KProperty1<T, U>): KProperty<U> =
10196
KIterablePropertyPath(this, other)

Diff for: src/test/kotlin/org/springframework/data/mapping/KPropertyPathTests.kt

+24-19
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,20 @@ class KPropertyPathTests {
4444
assertThat(property).isEqualTo("author.name")
4545
}
4646

47-
@Test // DATACMNS-3010
47+
@Test // GH-3010
4848
fun `Convert from Iterable nested KProperty to field name`() {
4949

50-
val property = (User::addresses / Address::street).toDotPath()
50+
val property = (Author::books / Book::title).toDotPath()
5151

52-
assertThat(property).isEqualTo("addresses.street")
52+
assertThat(property).isEqualTo("books.title")
53+
}
54+
55+
@Test // GH-3010
56+
fun `Convert from Iterable nested Iterable Property to field name`() {
57+
58+
val property = (Author::books / Book::author / Author::name).toDotPath()
59+
60+
assertThat(property).isEqualTo("books.author.name")
5361
}
5462

5563
@Test // DATACMNS-1835
@@ -73,6 +81,18 @@ class KPropertyPathTests {
7381
assertThat(property).isEqualTo("entity.book.author.name")
7482
}
7583

84+
@Test // DATACMNS-1835
85+
fun `Convert triple nested KProperty to property path using toDotPath`() {
86+
87+
class Entity(val book: Book)
88+
class AnotherEntity(val entity: Entity)
89+
90+
val property =
91+
(AnotherEntity::entity / Entity::book / Book::author / Author::name).toDotPath()
92+
93+
assertThat(property).isEqualTo("entity.book.author.name")
94+
}
95+
7696
@Test // DATACMNS-1835
7797
fun `Convert simple KProperty to property path using toDotPath`() {
7898

@@ -91,18 +111,6 @@ class KPropertyPathTests {
91111
assertThat(property).isEqualTo("author.name")
92112
}
93113

94-
@Test // DATACMNS-1835
95-
fun `Convert triple nested KProperty to property path using toDotPath`() {
96-
97-
class Entity(val book: Book)
98-
class AnotherEntity(val entity: Entity)
99-
100-
val property =
101-
(AnotherEntity::entity / Entity::book / Book::author / Author::name).toDotPath()
102-
103-
assertThat(property).isEqualTo("entity.book.author.name")
104-
}
105-
106114
@Test // DATACMNS-1835
107115
fun `Convert nullable KProperty to field name`() {
108116

@@ -114,9 +122,6 @@ class KPropertyPathTests {
114122
}
115123

116124
class Book(val title: String, val author: Author)
117-
class Author(val name: String)
118-
119-
class User(val addresses: List<Address>)
125+
class Author(val name: String, val books: List<Book>)
120126

121-
class Address(val street: String)
122127
}

0 commit comments

Comments
 (0)