Skip to content

Commit

Permalink
feat(query-model): add path methods for getter
Browse files Browse the repository at this point in the history
  • Loading branch information
jbl428 committed Nov 11, 2023
1 parent 85e973d commit 341ddfc
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -26,6 +27,16 @@ object Paths {
return JpqlEntityProperty(Entities.entity(owner), property)
}

/**
* Creates a path with the property.
*/
@SinceJdsl("3.1.0")
fun <T : Any, V> path(getter: KFunction1<T, @Exact V>): Path<V & Any> {
val owner = PropertyUtils.getOwner(getter)

return JpqlEntityProperty(Entities.entity(owner), getter)
}

/**
* Creates a path with the entity and property.
*/
Expand All @@ -34,6 +45,14 @@ object Paths {
return JpqlEntityProperty(entity, property)
}

/**
* Creates a path with the entity and property.
*/
@SinceJdsl("3.1.0")
fun <T : Any, V> path(entity: Entity<T>, getter: KFunction1<T, @Exact V>): Path<V & Any> {
return JpqlEntityProperty(entity, getter)
}

/**
* Creates a path with the path and property.
*/
Expand All @@ -42,6 +61,14 @@ object Paths {
return JpqlPathProperty(path, property)
}

/**
* Creates a path with the path and property.
*/
@SinceJdsl("3.1.0")
fun <T : Any, V> path(path: Path<T>, getter: KFunction1<T, @Exact V>): Path<V & Any> {
return JpqlPathProperty(path, getter)
}

/**
* Creates a path with downcasting.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ abstract class Employee(
val phone: String,
val address: EmployeeAddress,
val departments: MutableSet<EmployeeDepartment>,
)
) {
fun getUpperName(): String = name.uppercase()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package com.linecorp.kotlinjdsl.property

import kotlin.jvm.internal.CallableReference
import kotlin.reflect.KClass
import kotlin.reflect.KFunction1
import kotlin.reflect.KProperty1

object PropertyUtils {
fun <T : Any> getOwner(property: KProperty1<T, *>): KClass<T> {
@Suppress("UNCHECKED_CAST")
return (property as CallableReference).owner as KClass<T>
}

fun <T : Any> getOwner(property: KFunction1<T, *>): KClass<T> {
@Suppress("UNCHECKED_CAST")
return (property as CallableReference).owner as KClass<T>
}
}

0 comments on commit 341ddfc

Please sign in to comment.