Skip to content
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

Add path method for KFunction1 #521

Merged
merged 9 commits into from
Nov 12, 2023
Merged

Conversation

jbl428
Copy link
Contributor

@jbl428 jbl428 commented Nov 12, 2023

Motivation

Modifications

  • Add path method for KFunction1
  • Add docs for java entity.

Commit Convention Rule

Commit type Description
feat New Feature
fix Fix bug
docs Documentation only changed
ci Change CI configuration
refactor Not a bug fix or add feature, just refactoring code
test Add Test case or fix wrong test case
style Only change the code style(ex. white-space, formatting)
chore It refers to minor tasks such as library version upgrade, typo correction, etc.
  • If you want to add some more commit type please describe it on the Pull Request

Result

  • user can provide KFunction1 to path method

Closes

@jbl428 jbl428 changed the title Feat/kfunction dsl Add path method for KFunction1 Nov 12, 2023
@codecov-commenter
Copy link

codecov-commenter commented Nov 12, 2023

Codecov Report

Attention: 8 lines in your changes are missing coverage. Please review.

Comparison is base (85e973d) 92.11% compared to head (74ab44d) 91.90%.
Report is 2 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #521      +/-   ##
===========================================
- Coverage    92.11%   91.90%   -0.21%     
===========================================
  Files          269      269              
  Lines         2942     2966      +24     
  Branches       177      177              
===========================================
+ Hits          2710     2726      +16     
- Misses         181      189       +8     
  Partials        51       51              
Files Coverage Δ
...in/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/Jpql.kt 98.32% <100.00%> (+0.02%) ⬆️
.../linecorp/kotlinjdsl/querymodel/jpql/path/Paths.kt 100.00% <100.00%> (ø)
.../com/linecorp/kotlinjdsl/property/PropertyUtils.kt 100.00% <100.00%> (ø)
...javax/autoconfigure/KotlinJdslAutoConfiguration.kt 80.00% <66.66%> (-20.00%) ⬇️
...batch/autoconfigure/KotlinJdslAutoConfiguration.kt 80.00% <66.66%> (-20.00%) ⬇️
...javax/autoconfigure/KotlinJdslAutoConfiguration.kt 84.61% <60.00%> (-15.39%) ⬇️
...a/jpa/autoconfigure/KotlinJdslAutoConfiguration.kt 84.61% <60.00%> (-15.39%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

* Creates a path expression with the entity and property.
*/
@SinceJdsl("3.1.0")
fun <T : Any, V> Entityable<T>.path(getter: KFunction1<T, @Exact V>): Path<V & Any> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to put in in the T generic type? I want to include in in something like KProperty1<in T, @Eact V> because an user might want to create a path with the parent type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works in the test, but it's probably because the receiver's T is cast to a supertype. This causes the type to be broken when chaining occurs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when i first put in keyword. IDE show me the following warning.
It might that KFunction1 has in variance unlike KProperty1.

Projection is redundant: the corresponding type parameter of KFunction1 has the same variance

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, I see. Thank you for the confirmation.

@@ -7,4 +7,6 @@ abstract class Employee(
val phone: String,
val address: EmployeeAddress,
val departments: MutableSet<EmployeeDepartment>,
)
) {
fun getLowerName(): String = name.lowercase()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a test entity, it doesn't matter what methods it has, but how about making it a little more meaningful? Could we write code like fun getNickname() = nickname ?: name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is compile error when we have method named getNickname.
so i rename getDisplayName

Platform declaration clash: The following declarations have the same JVM signature (getNickname()Ljava/lang/String;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

displayName sounds really good.

@@ -17,4 +17,6 @@ class FullTimeEmployee(
phone = phone,
address = address,
departments = departments,
)
) {
fun getUpperName(): String = name.uppercase()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could make a method like fun getMonthlySalary(): EmployeeSalary = EmployeeSalary(annualSalary.value.div(BigDecimal.valueOf(12))).

@@ -33,6 +33,22 @@ class PathsTest : WithAssertions {
assertThat(actual).isEqualTo(expected)
}

@Test
fun pathGetter() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that path can take two parameters, we can rename the path method to path with a property and rename it to path with a getter.

@@ -13,6 +13,75 @@ entity(Book::class, "b").path(Book::isbn).path(Isbn::value)
entity(Book::class, "b")(Book::isbn)(Isbn::value)
```

## Java entity

`path()` and `invoke()` take `KProperty1` or `KFuction1` as an argument.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about replacing take with can take?

path(Book::getTitle)
```

Kotlin JDSL follows the following rules to infer property name from getter name.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to change the sentence to something like this: Kotlin JDSL infers the property name from the getter with the following rules:

path(Book::getAvailable)
```

If you want to use your own rule instead of the above rules, you need to implement `JpqlPropertyIntrospector` and provide it to `RenderContext`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to change you need to implement to you can implement.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to leave a guide and link to the Custom DSL for more details, and to Spring Support if the user is using Spring.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it would be nice to add Spring AutoConfigure, since there is no Spring Support for JpqlIntrospector. This forces the user to modify the JpqlRenderContext by themselves, which I think will be inconvenient for the user.

open class KotlinJdslAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    @SinceJdsl("3.0.0")
    open fun jpqlRenderContext(
        serializers: List<JpqlSerializer<*>>,
        introspectors: List<JpqlIntrospector>,
    ): JpqlRenderContext {
        val userDefinedSerializers = object : JpqlRenderModule {
            override fun setupModule(context: JpqlRenderModule.SetupContext) {
                context.addAllSerializer(serializers.reversed())

                introspectors.reversed().forEach {
                    context.prependIntrospector(it)
                }
            }
        }

        return JpqlRenderContext()
            .registerModules(userDefinedSerializers)
    }
    // ...
}

@@ -13,6 +13,75 @@ entity(Book::class, "b").path(Book::isbn).path(Isbn::value)
entity(Book::class, "b")(Book::isbn)(Isbn::value)
```

## Java entity
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한국어까지 같이 작성해주셔서 정말 감사합니다. ❤️

@CLAassistant
Copy link

CLAassistant commented Nov 12, 2023

CLA assistant check
All committers have signed the CLA.

@jbl428 jbl428 force-pushed the feat/kfunction-dsl branch from 89e79c6 to 74ab44d Compare November 12, 2023 12:45
Copy link
Collaborator

@cj848 cj848 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution!

Copy link
Member

@shouwn shouwn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the help! I'll rename some of the tests.

@shouwn shouwn merged commit 6970af2 into line:develop Nov 12, 2023
3 checks passed
@jbl428 jbl428 deleted the feat/kfunction-dsl branch November 13, 2023 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants