Skip to content
This repository has been archived by the owner on Jan 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #19 from k163377/feature
Browse files Browse the repository at this point in the history
Fixing generics and keeping up with Shared updates.
  • Loading branch information
k163377 authored May 23, 2020
2 parents 7803888 + 0143685 commit 4f5f86f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 31 deletions.
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "com.mapk"
version = "0.13"
version = "0.14"

java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -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")
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlin.reflect.KClass
@MustBeDocumented
annotation class KColumnDeserializeBy(val deserializer: KClass<out AbstractKColumnDeserializer<*, *, *>>)

abstract class AbstractKColumnDeserializer<A : Annotation, S : Any, D : Any>(protected val annotation: A) {
abstract class AbstractKColumnDeserializer<A : Annotation, S : Any, D>(protected val annotation: A) {
abstract val srcClass: Class<S>
abstract fun deserialize(source: S?): D?
}
11 changes: 5 additions & 6 deletions src/main/kotlin/com/mapk/krowmapper/KRowMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import org.springframework.jdbc.core.RowMapper

class KRowMapper<T : Any> private constructor(
private val function: KFunctionForCall<T>
) : RowMapper<T> {
constructor(function: KFunction<T>, parameterNameConverter: (String) -> String = { it }) : this(
class KRowMapper<T : Any> private constructor(private val function: KFunctionForCall<T>) : RowMapper<T> {
constructor(function: KFunction<T>, parameterNameConverter: ((String) -> String)? = null) : this(
KFunctionForCall(function, parameterNameConverter)
)

constructor(clazz: KClass<T>, parameterNameConverter: (String) -> String = { it }) : this(
constructor(clazz: KClass<T>, parameterNameConverter: ((String) -> String)? = null) : this(
clazz.toKConstructor(parameterNameConverter)
)

private val parameters: List<ParameterForMap> = function.requiredParameters.map { ParameterForMap.newInstance(it) }
private val parameters: List<ParameterForMap<*, *>> =
function.requiredParameters.map { ParameterForMap.newInstance(it) }

override fun mapRow(rs: ResultSet, rowNum: Int): T {
val adaptor = function.getArgumentAdaptor()
Expand Down
35 changes: 14 additions & 21 deletions src/main/kotlin/com/mapk/krowmapper/ParameterForMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<S, D> {
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<T>(override val name: String, val requiredClazz: Class<T>) : ParameterForMap<T, T>() {
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<D>(override val name: String, val enumClazz: Class<D>) : ParameterForMap<String, D>() {
override fun getObject(rs: ResultSet): D? = EnumMapper.getEnum(enumClazz, rs.getString(name))
}

private class Deserializer(
private class Deserializer<S : Any, D>(
override val name: String,
override val clazz: Class<*>,
private val deserializer: KFunction<*>
) : ParameterForMap() {
val srcClazz: Class<S>,
private val deserializer: KFunction<D?>
) : ParameterForMap<S, D>() {
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 <T : Any> newInstance(param: ValueParameter<T>): ParameterForMap {
fun <T : Any> newInstance(param: ValueParameter<T>): ParameterForMap<*, T> {
param.getDeserializer()?.let {
return Deserializer(param.name, it)
}
Expand Down Expand Up @@ -117,5 +110,5 @@ private fun <T : Any> deserializerFromCompanionObject(clazz: KClass<T>): Collect
functions.map {
KFunctionWithInstance(it, instance) as KFunction<T>
}
} ?: emptySet()
} ?: emptyList()
}

0 comments on commit 4f5f86f

Please sign in to comment.