Skip to content

Commit 86aa5aa

Browse files
committed
DATAMONGO-2138 - Typed nested properties
1 parent a834f29 commit 86aa5aa

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2018 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+
* http://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+
import kotlin.reflect.KProperty1
20+
21+
/**
22+
* Refer to a field in an embedded/nested document.
23+
* @author Tjeu Kayim
24+
* @since 2.2
25+
*/
26+
class NestedProperty<T, U>(
27+
internal val parent: KProperty<U>,
28+
internal val child: KProperty1<U, T>
29+
) : KProperty<T> by child
30+
31+
/**
32+
* Recursively construct field name for a nested property.
33+
* @author Tjeu Kayim
34+
*/
35+
internal fun nestedFieldName(property: KProperty<*>): String {
36+
return when (property) {
37+
is NestedProperty<*, *> ->
38+
"${nestedFieldName(property.parent)}.${property.child.name}"
39+
else -> property.name
40+
}
41+
}
42+
43+
/**
44+
* Build nested properties with Property References.
45+
* Refer to a field in an embedded/nested document.
46+
*
47+
* For example, referring to the field "book.author":
48+
* ```
49+
* Book::author / Author::name isEqualTo "Herman Melville"
50+
* ```
51+
* @author Tjeu Kayim
52+
* @since 2.2
53+
*/
54+
operator fun <T, U> KProperty<T>.div(other: KProperty1<T, U>) =
55+
NestedProperty(this, other)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2018 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+
* http://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 org.assertj.core.api.Assertions.assertThat
19+
import org.junit.Test
20+
21+
/**
22+
* @author Tjeu Kayim
23+
*/
24+
class NestedPropertyTests {
25+
26+
@Test
27+
fun `Convert normal KProperty to field name`() {
28+
29+
val property = nestedFieldName(Book::title)
30+
31+
assertThat(property).isEqualTo("title")
32+
}
33+
34+
@Test
35+
fun `Convert nested KProperty to field name`() {
36+
37+
val property = nestedFieldName(Book::author / Author::name)
38+
39+
assertThat(property).isEqualTo("author.name")
40+
}
41+
42+
@Test
43+
fun `Convert double nested KProperty to field name`() {
44+
45+
class Entity(val book: Book)
46+
47+
val property = nestedFieldName(Entity::book / Book::author / Author::name)
48+
49+
assertThat(property).isEqualTo("book.author.name")
50+
}
51+
52+
@Test
53+
fun `Convert triple nested KProperty to field name`() {
54+
55+
class Entity(val book: Book)
56+
class AnotherEntity(val entity: Entity)
57+
58+
val property = nestedFieldName(AnotherEntity::entity / Entity::book / Book::author / Author::name)
59+
60+
assertThat(property).isEqualTo("entity.book.author.name")
61+
}
62+
63+
class Book(val title: String, val author: Author)
64+
class Author(val name: String)
65+
}

0 commit comments

Comments
 (0)