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

Support select() with KClass<T> arguments #825

Closed
minsujang0 opened this issue Jan 31, 2025 · 5 comments
Closed

Support select() with KClass<T> arguments #825

minsujang0 opened this issue Jan 31, 2025 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@minsujang0
Copy link
Contributor

While creating some generic repositories using Kotlin JDSL, I found that there is a case where

  • Interfaces are from other code bases, so it's hard to change function signatures; i.e. cannot add <reified T> or something to transfer type classes.
  • Under this circumstances, the best option to implement such behaviors is by passing T::class directly as an argument of type KClass<T>.
  • However, select() in JDSL has a limited option of only using the reified version.

An example code is like this:

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(entity(entityClass)) // !Cannot use 'T' as reified type parameter. Use a class instead.
              .from(entity(entityClass))
      }
  }
}

To fix this, my current workaround is like this:

class TRepository<T: Any>(private val entityClass: KClass<T>) : JdslRepository<T> {
  val entity = object : Entity<T> {
      override val alias: String
          get() = entityClass.simpleName!!
  }
  
  override fun findAll(): SelectQuery<T> {
      return SelectQueries.selectQuery(
          entityClass,
          false,
          listOf(entity),
          listOf(entity),
      )
  }
}

If this is an intended work flow, I'm still happy with this.

I know that this use case is quite rare and somewhat undesirable for simplicity of the library, but supporting this might add some flexibilities for users.

@shouwn shouwn self-assigned this Feb 3, 2025
@shouwn shouwn added the enhancement New feature or request label Feb 3, 2025
@shouwn
Copy link
Member

shouwn commented Feb 3, 2025

I agree with your suggestion.

I don't think it's necessary to force reified interfaces in the Kotlin JDSL. I will try to create an interface that accepts a KClass for the select function.

But I can't do this this week and next week due to other work. If you can help me with this, I would appreciate it.

@minsujang0
Copy link
Contributor Author

Thank you for kind reply :)
I'll try to work on this if it's okay.

@shouwn
Copy link
Member

shouwn commented Feb 4, 2025

@minsujang0 Thank you for your help!

minsujang0 added a commit to minsujang0/kotlin-jdsl that referenced this issue Feb 10, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
minsujang0 added a commit to minsujang0/kotlin-jdsl that referenced this issue Feb 10, 2025
minsujang0 added a commit to minsujang0/kotlin-jdsl that referenced this issue Feb 11, 2025
minsujang0 added a commit to minsujang0/kotlin-jdsl that referenced this issue Feb 11, 2025
@cj848 cj848 closed this as completed Feb 18, 2025
@shouwn
Copy link
Member

shouwn commented Feb 19, 2025

@minsujang0 The public key I use for deployment appears to have expired, so deployment will be delayed.

@shouwn
Copy link
Member

shouwn commented Mar 1, 2025

@minsujang0 I apologize for the delay in deployment.
The 3.5.5 deployment is complete.

It's been a while since I've set up a sign, so I had to add the secret key to the settings instead of the public key, but I wrote it down as the public key and got stuck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants