jdbi icon indicating copy to clipboard operation
jdbi copied to clipboard

How can one map generics in Kotlin?

Open conleym opened this issue 5 years ago • 1 comments

I have the following (simplified) DAO code:

import org.jdbi.v3.core.Jdbi
import org.jdbi.v3.sqlobject.statement.SqlQuery
import java.time.Duration

class StupidDAO(jdbi: Jdbi) : StupidSql by jdbi.onDemand(StupidSql::class.java) {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            val jdbi = DatabaseTestBase.jdbi // Connects to my postgresql test database.
            val stupidDAO = StupidDAO(jdbi)
            println(stupidDAO.doIt())
        }
    }
}
private interface StupidSql {
    @SqlQuery("""
            SELECT 'a' as id,
                   interval '1 hour' as value
        """)
    fun doIt(): List<StupidThing<Duration>>
}
data class StupidThing<T>(
        val id: String,
        val value: T
)

When I run it, I get

Could not find column mapper for type 'T' of parameter 'parameter #1 value of fun <init>(kotlin.String, T): StupidThing<T>' for constructor 'fun <init>(kotlin.String, T): StupidThing<T>'
	at org.jdbi.v3.core.kotlin.KotlinMapper$resolveConstructorParameterMapper$2.get(KotlinMapper.kt:197)
	at org.jdbi.v3.core.kotlin.KotlinMapper$resolveConstructorParameterMapper$2.get(KotlinMapper.kt:51)
	at java.util.Optional.orElseThrow(Optional.java:290)
	at org.jdbi.v3.core.kotlin.KotlinMapper.resolveConstructorParameterMapper(KotlinMapper.kt:196)
	at org.jdbi.v3.core.kotlin.KotlinMapper.specialize0(KotlinMapper.kt:98)
	at org.jdbi.v3.core.kotlin.KotlinMapper.specialize(KotlinMapper.kt:73)
	at org.jdbi.v3.core.result.ResultSetResultIterator.<init>(ResultSetResultIterator.java:38)
	at org.jdbi.v3.core.result.ResultIterable.lambda$of$0(ResultIterable.java:54)
	at org.jdbi.v3.core.result.ResultIterable.stream(ResultIterable.java:228)
	at org.jdbi.v3.core.result.ResultIterable.collect(ResultIterable.java:284)
	at org.jdbi.v3.sqlobject.statement.internal.ResultReturner$CollectedResultReturner.mappedResult(ResultReturner.java:267)
	at org.jdbi.v3.sqlobject.statement.internal.SqlQueryHandler.lambda$configureReturner$0(SqlQueryHandler.java:54)
	at 

I've tried a couple of variations on @MapTo with GenericType to no avail. Are there any examples of mapping generic Kotlin data classes with kotlin-sqlobject?

conleym avatar May 09 '20 18:05 conleym

Note that the type Duration here isn't really important. It'll fail with anything I put in there.

conleym avatar May 09 '20 19:05 conleym