-
Notifications
You must be signed in to change notification settings - Fork 682
DATACMNS-1835 - Provide Type-safe Kotlin query extension #478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/kotlin/org/springframework/data/mapping/KPropertyPath.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2018-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.mapping | ||
|
||
import kotlin.reflect.KProperty | ||
import kotlin.reflect.KProperty1 | ||
|
||
/** | ||
* Abstraction of a property path consisting of [KProperty]. | ||
* | ||
* @author Tjeu Kayim | ||
* @author Mark Paluch | ||
* @author Yoann de Martino | ||
* @since 2.5 | ||
*/ | ||
private class KPropertyPath<T, U>( | ||
val parent: KProperty<U?>, | ||
val child: KProperty1<U, T> | ||
) : KProperty<T> by child | ||
|
||
/** | ||
* Recursively construct field name for a nested property. | ||
* @author Tjeu Kayim | ||
*/ | ||
internal fun asString(property: KProperty<*>): String { | ||
return when (property) { | ||
is KPropertyPath<*, *> -> | ||
"${asString(property.parent)}.${property.child.name}" | ||
else -> property.name | ||
} | ||
} | ||
|
||
/** | ||
* Builds [KPropertyPath] from Property References. | ||
* Refer to a nested property in an embeddable or association. | ||
* | ||
* For example, referring to the field "author.name": | ||
* ``` | ||
* Book::author / Author::name isEqualTo "Herman Melville" | ||
* ``` | ||
* @author Tjeu Kayim | ||
* @author Yoann de Martino | ||
* @since 2.5 | ||
*/ | ||
operator fun <T, U> KProperty<T?>.div(other: KProperty1<T, U>): KProperty<U> = | ||
KPropertyPath(this, other) |
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/org/springframework/data/mapping/KPropertyPathExtensions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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.mapping | ||
|
||
import kotlin.reflect.KProperty | ||
|
||
/** | ||
* Extension for [KProperty] providing an `toPath` function to render a [KProperty] in dot notation. | ||
* | ||
* @author Mark Paluch | ||
* @since 2.5 | ||
* @see PropertyPath.toDotPath | ||
*/ | ||
fun KProperty<*>.toDotPath(): String = asString(this) |
109 changes: 109 additions & 0 deletions
109
src/test/kotlin/org/springframework/data/mapping/KPropertyPathTests.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright 2018-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.mapping | ||
|
||
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 { | ||
|
||
@Test // DATACMNS-1835 | ||
fun `Convert normal KProperty to field name`() { | ||
|
||
val property = Book::title.toDotPath() | ||
|
||
assertThat(property).isEqualTo("title") | ||
} | ||
|
||
@Test // DATACMNS-1835 | ||
fun `Convert nested KProperty to field name`() { | ||
|
||
val property = (Book::author / Author::name).toDotPath() | ||
|
||
assertThat(property).isEqualTo("author.name") | ||
} | ||
|
||
@Test // DATACMNS-1835 | ||
fun `Convert double nested KProperty to field name`() { | ||
|
||
class Entity(val book: Book) | ||
|
||
val property = (Entity::book / Book::author / Author::name).toDotPath() | ||
|
||
assertThat(property).isEqualTo("book.author.name") | ||
} | ||
|
||
@Test // DATACMNS-1835 | ||
fun `Convert triple nested KProperty to field name`() { | ||
|
||
class Entity(val book: Book) | ||
class AnotherEntity(val entity: Entity) | ||
|
||
val property = asString(AnotherEntity::entity / Entity::book / Book::author / Author::name) | ||
|
||
assertThat(property).isEqualTo("entity.book.author.name") | ||
} | ||
|
||
@Test // DATACMNS-1835 | ||
fun `Convert simple KProperty to property path using toDotPath`() { | ||
|
||
class AnotherEntity(val entity: String) | ||
|
||
val property = AnotherEntity::entity.toDotPath() | ||
|
||
assertThat(property).isEqualTo("entity") | ||
} | ||
|
||
@Test // DATACMNS-1835 | ||
fun `Convert nested KProperty to field name using toDotPath()`() { | ||
|
||
val property = (Book::author / Author::name).toDotPath() | ||
|
||
assertThat(property).isEqualTo("author.name") | ||
} | ||
|
||
@Test // DATACMNS-1835 | ||
fun `Convert triple nested KProperty to property path using toDotPath`() { | ||
|
||
class Entity(val book: Book) | ||
class AnotherEntity(val entity: Entity) | ||
|
||
val property = | ||
(AnotherEntity::entity / Entity::book / Book::author / Author::name).toDotPath() | ||
|
||
assertThat(property).isEqualTo("entity.book.author.name") | ||
} | ||
|
||
@Test // DATACMNS-1835 | ||
fun `Convert nullable KProperty to field name`() { | ||
|
||
class Cat(val name: String?) | ||
class Owner(val cat: Cat?) | ||
|
||
val property = asString(Owner::cat / Cat::name) | ||
assertThat(property).isEqualTo("cat.name") | ||
} | ||
|
||
class Book(val title: String, val author: Author) | ||
class Author(val name: String) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toDotPath
function?