Skip to content

feat: Support select() with KClass<T> arguments#834

Merged
shouwn merged 4 commits intoline:mainfrom
minsujang0:feature/select-kclass
Feb 16, 2025
Merged

feat: Support select() with KClass<T> arguments#834
shouwn merged 4 commits intoline:mainfrom
minsujang0:feature/select-kclass

Conversation

@minsujang0
Copy link
Contributor

@minsujang0 minsujang0 commented Feb 11, 2025

Motivation

  • Original select(..) functions used reified types for class parameters, making it hard to use them for generic classes even in the presence of class variables (T::class)

Modifications

  • Added class variable versions for every function in select(..) family.
    • Tried to match style with existing class type functions
  • Added some tests covering new functions

Result

  • One can write following code without an error:
abstract class JdslRepository<ENTITY : Any> { 
   abstract fun findAll(): SelectQuery<ENTITY>
}

class TRepository<T: Any>(private val entityClass: KClass<T>) : JdslRepository<T> {
  override fun findAll(): SelectQuery<T> {
      return jpql {
          select(entityClass, entity(entityClass)) // <- free of the error came from type erasure
              .from(entity(entityClass))
      }
  }
}

Closes

@CLAassistant
Copy link

CLAassistant commented Feb 11, 2025

CLA assistant check
All committers have signed the CLA.

@minsujang0 minsujang0 force-pushed the feature/select-kclass branch from 0f90cec to 12c3979 Compare February 11, 2025 15:35
@codecov-commenter
Copy link

codecov-commenter commented Feb 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.63%. Comparing base (5f0dfba) to head (64b682b).
Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #834      +/-   ##
==========================================
+ Coverage   90.55%   90.63%   +0.07%     
==========================================
  Files         337      337              
  Lines        3567     3597      +30     
  Branches      222      222              
==========================================
+ Hits         3230     3260      +30     
  Misses        269      269              
  Partials       68       68              

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@shouwn
Copy link
Member

shouwn commented Feb 12, 2025

Thank you for your help. I'll check it out this weekend.

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.

I'm thinking of releasing your work as a hotfix next Tuesday, but if you find it difficult to fix the code, please let me know. I'll make some changes and release it.

Thanks for your help!

/**
* Creates a select clause in a select query.
*/
@SinceJdsl("3.0.0")
Copy link
Member

Choose a reason for hiding this comment

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

Since I'm thinking of releasing it as a hotfix, I'd like to modify it to @SinceJdsl(3.5.5).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay :)

Comment on lines +72 to +96
@Test
fun `select() with a KClass`() {
// when
val select = queryPart {
select(
BigDecimal::class,
expression1,
).from(
entity1,
)
}.toQuery()

val actual: SelectQuery<BigDecimal> = select // for type check

// then
val expected = SelectQueries.selectQuery(
returnType = BigDecimal::class,
distinct = false,
select = listOf(expression1),
from = listOf(entity1),
)

assertThat(actual).isEqualTo(expected)
}

Copy link
Member

Choose a reason for hiding this comment

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

It looks like tests are only written for some of the newly created functions. Please add more tests.
And please order the tests and functions correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I missed these points. Thanks :)

@minsujang0 minsujang0 requested review from cj848 and shouwn February 16, 2025 06:51
@minsujang0
Copy link
Contributor Author

@shouwn I reflected your feedback and revised my code. Please check :)

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.

Thank you so much for your help!

@shouwn shouwn merged commit 17c6cdd into line:main Feb 16, 2025
4 checks passed
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