diff --git a/build.gradle.kts b/build.gradle.kts index a3b1e81..b5cc5f6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "com.mapk" -version = "0.13" +version = "0.14" java { sourceCompatibility = JavaVersion.VERSION_1_8 @@ -30,7 +30,7 @@ repositories { dependencies { implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation(kotlin("reflect")) - api("com.github.ProjectMapK:Shared:0.15") + api("com.github.ProjectMapK:Shared:0.16") // 使うのはRowMapperのみなため他はexclude、またバージョンそのものは使う相手に合わせるためcompileOnly compileOnly(group = "org.springframework", name = "spring-jdbc", version = "5.2.4.RELEASE") { exclude(module = "spring-beans") @@ -43,7 +43,7 @@ dependencies { exclude(group = "org.junit.vintage", module = "junit-vintage-engine") } // https://mvnrepository.com/artifact/io.mockk/mockk - testImplementation("io.mockk:mockk:1.9.3") + testImplementation("io.mockk:mockk:1.10.0") // テスト時には無いと困るため、別口でimplementation testImplementation(group = "org.springframework", name = "spring-jdbc", version = "5.2.4.RELEASE") diff --git a/src/main/kotlin/com/mapk/deserialization/KColumnDeserialize.kt b/src/main/kotlin/com/mapk/deserialization/KColumnDeserialize.kt index a4d7c72..767850a 100644 --- a/src/main/kotlin/com/mapk/deserialization/KColumnDeserialize.kt +++ b/src/main/kotlin/com/mapk/deserialization/KColumnDeserialize.kt @@ -7,7 +7,7 @@ import kotlin.reflect.KClass @MustBeDocumented annotation class KColumnDeserializeBy(val deserializer: KClass>) -abstract class AbstractKColumnDeserializer(protected val annotation: A) { +abstract class AbstractKColumnDeserializer(protected val annotation: A) { abstract val srcClass: Class abstract fun deserialize(source: S?): D? } diff --git a/src/main/kotlin/com/mapk/krowmapper/KRowMapper.kt b/src/main/kotlin/com/mapk/krowmapper/KRowMapper.kt index 8162444..7b8fa48 100644 --- a/src/main/kotlin/com/mapk/krowmapper/KRowMapper.kt +++ b/src/main/kotlin/com/mapk/krowmapper/KRowMapper.kt @@ -7,18 +7,17 @@ import kotlin.reflect.KClass import kotlin.reflect.KFunction import org.springframework.jdbc.core.RowMapper -class KRowMapper private constructor( - private val function: KFunctionForCall -) : RowMapper { - constructor(function: KFunction, parameterNameConverter: (String) -> String = { it }) : this( +class KRowMapper private constructor(private val function: KFunctionForCall) : RowMapper { + constructor(function: KFunction, parameterNameConverter: ((String) -> String)? = null) : this( KFunctionForCall(function, parameterNameConverter) ) - constructor(clazz: KClass, parameterNameConverter: (String) -> String = { it }) : this( + constructor(clazz: KClass, parameterNameConverter: ((String) -> String)? = null) : this( clazz.toKConstructor(parameterNameConverter) ) - private val parameters: List = function.requiredParameters.map { ParameterForMap.newInstance(it) } + private val parameters: List> = + function.requiredParameters.map { ParameterForMap.newInstance(it) } override fun mapRow(rs: ResultSet, rowNum: Int): T { val adaptor = function.getArgumentAdaptor() diff --git a/src/main/kotlin/com/mapk/krowmapper/ParameterForMap.kt b/src/main/kotlin/com/mapk/krowmapper/ParameterForMap.kt index 42c3623..c21c760 100644 --- a/src/main/kotlin/com/mapk/krowmapper/ParameterForMap.kt +++ b/src/main/kotlin/com/mapk/krowmapper/ParameterForMap.kt @@ -19,40 +19,33 @@ import kotlin.reflect.full.staticFunctions import kotlin.reflect.jvm.isAccessible import kotlin.reflect.jvm.jvmName -internal sealed class ParameterForMap { +internal sealed class ParameterForMap { abstract val name: String - abstract val clazz: Class<*> - abstract fun getObject(rs: ResultSet): Any? + abstract fun getObject(rs: ResultSet): D? - private class Plain( - override val name: String, - override val clazz: Class<*> - ) : ParameterForMap() { - override fun getObject(rs: ResultSet): Any? = rs.getObject(name, clazz) + private class Plain(override val name: String, val requiredClazz: Class) : ParameterForMap() { + override fun getObject(rs: ResultSet): T? = rs.getObject(name, requiredClazz) } - private class Enum( - override val name: String, - override val clazz: Class<*> - ) : ParameterForMap() { - override fun getObject(rs: ResultSet): Any? = EnumMapper.getEnum(clazz, rs.getString(name)) + private class Enum(override val name: String, val enumClazz: Class) : ParameterForMap() { + override fun getObject(rs: ResultSet): D? = EnumMapper.getEnum(enumClazz, rs.getString(name)) } - private class Deserializer( + private class Deserializer( override val name: String, - override val clazz: Class<*>, - private val deserializer: KFunction<*> - ) : ParameterForMap() { + val srcClazz: Class, + private val deserializer: KFunction + ) : ParameterForMap() { constructor( name: String, - deserializer: AbstractKColumnDeserializer<*, *, *> + deserializer: AbstractKColumnDeserializer<*, S, D> ) : this(name, deserializer.srcClass, deserializer::deserialize) - override fun getObject(rs: ResultSet): Any? = deserializer.call(rs.getObject(name, clazz)) + override fun getObject(rs: ResultSet): D? = deserializer.call(rs.getObject(name, srcClazz)) } companion object { - fun newInstance(param: ValueParameter): ParameterForMap { + fun newInstance(param: ValueParameter): ParameterForMap<*, T> { param.getDeserializer()?.let { return Deserializer(param.name, it) } @@ -117,5 +110,5 @@ private fun deserializerFromCompanionObject(clazz: KClass): Collect functions.map { KFunctionWithInstance(it, instance) as KFunction } - } ?: emptySet() + } ?: emptyList() }