From 341ddfc2ec71db2714919d02a78fe80f242a07fa Mon Sep 17 00:00:00 2001 From: Jake Son Date: Fri, 10 Nov 2023 09:00:02 +0900 Subject: [PATCH] feat(query-model): add path methods for getter --- .../kotlinjdsl/querymodel/jpql/path/Paths.kt | 27 ++++++ .../querymodel/jpql/path/PathsTest.kt | 84 +++++++++++++++++++ .../jpql/entity/employee/Employee.kt | 4 +- .../kotlinjdsl/property/PropertyUtils.kt | 6 ++ 4 files changed, 120 insertions(+), 1 deletion(-) diff --git a/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/path/Paths.kt b/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/path/Paths.kt index b97a13840..90d5d6b81 100644 --- a/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/path/Paths.kt +++ b/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/path/Paths.kt @@ -9,6 +9,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.path.impl.JpqlPathProperty import com.linecorp.kotlinjdsl.querymodel.jpql.path.impl.JpqlPathTreat import kotlin.internal.Exact import kotlin.reflect.KClass +import kotlin.reflect.KFunction1 import kotlin.reflect.KProperty1 /** @@ -26,6 +27,16 @@ object Paths { return JpqlEntityProperty(Entities.entity(owner), property) } + /** + * Creates a path with the property. + */ + @SinceJdsl("3.1.0") + fun path(getter: KFunction1): Path { + val owner = PropertyUtils.getOwner(getter) + + return JpqlEntityProperty(Entities.entity(owner), getter) + } + /** * Creates a path with the entity and property. */ @@ -34,6 +45,14 @@ object Paths { return JpqlEntityProperty(entity, property) } + /** + * Creates a path with the entity and property. + */ + @SinceJdsl("3.1.0") + fun path(entity: Entity, getter: KFunction1): Path { + return JpqlEntityProperty(entity, getter) + } + /** * Creates a path with the path and property. */ @@ -42,6 +61,14 @@ object Paths { return JpqlPathProperty(path, property) } + /** + * Creates a path with the path and property. + */ + @SinceJdsl("3.1.0") + fun path(path: Path, getter: KFunction1): Path { + return JpqlPathProperty(path, getter) + } + /** * Creates a path with downcasting. */ diff --git a/query-model/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/path/PathsTest.kt b/query-model/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/path/PathsTest.kt index d68e002bc..d0cd8bffa 100644 --- a/query-model/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/path/PathsTest.kt +++ b/query-model/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/path/PathsTest.kt @@ -33,6 +33,22 @@ class PathsTest : WithAssertions { assertThat(actual).isEqualTo(expected) } + @Test + fun pathGetter() { + // when + val actual = Paths.path( + FullTimeEmployee::getUpperName, + ) + + // then + val expected = JpqlEntityProperty( + entity = Entities.entity(FullTimeEmployee::class), + property = FullTimeEmployee::getUpperName, + ) + + assertThat(actual).isEqualTo(expected) + } + @Test fun `path() with an entity`() { // when @@ -50,6 +66,23 @@ class PathsTest : WithAssertions { assertThat(actual).isEqualTo(expected) } + @Test + fun `path() with an entity and a getter`() { + // when + val actual = Paths.path( + entity1, + Employee::getUpperName, + ) + + // then + val expected = JpqlEntityProperty( + entity = entity1, + property = Employee::getUpperName, + ) + + assertThat(actual).isEqualTo(expected) + } + @Test fun `path() with a path`() { // when @@ -67,6 +100,23 @@ class PathsTest : WithAssertions { assertThat(actual).isEqualTo(expected) } + @Test + fun `path() with a path and a getter`() { + // when + val actual = Paths.path( + path1, + getter = Employee::getUpperName, + ) + + // then + val expected = JpqlPathProperty( + path = path1, + property = Employee::getUpperName, + ) + + assertThat(actual).isEqualTo(expected) + } + @Test fun `path() with an entity and a property of super class`() { // when @@ -84,6 +134,23 @@ class PathsTest : WithAssertions { assertThat(actual).isEqualTo(expected) } + @Test + fun `path() with an entity and a getter of super class`() { + // when + val actual = Paths.path( + entity2, + Employee::getUpperName, + ) + + // then + val expected = JpqlEntityProperty( + entity = entity2, + property = Employee::getUpperName, + ) + + assertThat(actual).isEqualTo(expected) + } + @Test fun `path() with a path and a property of super class`() { // when @@ -101,6 +168,23 @@ class PathsTest : WithAssertions { assertThat(actual).isEqualTo(expected) } + @Test + fun `path() with a path and a getter of super class`() { + // when + val actual = Paths.path( + path2, + getter = Employee::getUpperName, + ) + + // then + val expected = JpqlPathProperty( + path = path2, + property = Employee::getUpperName, + ) + + assertThat(actual).isEqualTo(expected) + } + @Test fun treat() { // when diff --git a/query-model/jpql/src/testFixtures/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/entity/employee/Employee.kt b/query-model/jpql/src/testFixtures/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/entity/employee/Employee.kt index 534c41239..12100b5c7 100644 --- a/query-model/jpql/src/testFixtures/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/entity/employee/Employee.kt +++ b/query-model/jpql/src/testFixtures/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/entity/employee/Employee.kt @@ -9,4 +9,6 @@ abstract class Employee( val phone: String, val address: EmployeeAddress, val departments: MutableSet, -) +) { + fun getUpperName(): String = name.uppercase() +} diff --git a/src/main/kotlin/com/linecorp/kotlinjdsl/property/PropertyUtils.kt b/src/main/kotlin/com/linecorp/kotlinjdsl/property/PropertyUtils.kt index ccbe84052..461adee71 100644 --- a/src/main/kotlin/com/linecorp/kotlinjdsl/property/PropertyUtils.kt +++ b/src/main/kotlin/com/linecorp/kotlinjdsl/property/PropertyUtils.kt @@ -2,6 +2,7 @@ package com.linecorp.kotlinjdsl.property import kotlin.jvm.internal.CallableReference import kotlin.reflect.KClass +import kotlin.reflect.KFunction1 import kotlin.reflect.KProperty1 object PropertyUtils { @@ -9,4 +10,9 @@ object PropertyUtils { @Suppress("UNCHECKED_CAST") return (property as CallableReference).owner as KClass } + + fun getOwner(property: KFunction1): KClass { + @Suppress("UNCHECKED_CAST") + return (property as CallableReference).owner as KClass + } }