Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ repositories {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation(kotlin("reflect"))
implementation("com.github.ProjectMapK:Shared:0.3")
implementation("com.github.ProjectMapK:Shared:0.4")

// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter
testImplementation(group = "org.junit.jupiter", name = "junit-jupiter", version = "5.6.0") {
Expand Down
30 changes: 3 additions & 27 deletions src/main/kotlin/com/mapk/kmapper/KMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ class KMapper<T : Any> private constructor(
ParameterForMap.newInstance(it)
}

private fun throwExceptionOnNotInitialized(argumentBucket: ArgumentBucket): Nothing {
val notInitializedIndexes = argumentBucket.notInitializedParameterIndexes
function.parameters
.filter { it.index in notInitializedIndexes }
.map { it.name }
.joinToString(", ")
.let {
throw IllegalArgumentException("Not passed arguments: $it")
}
}

private fun bindArguments(argumentBucket: ArgumentBucket, src: Any) {
src::class.memberProperties.forEach outer@{ property ->
// propertyが公開されていない場合は処理を行わない
Expand All @@ -68,7 +57,7 @@ class KMapper<T : Any> private constructor(
parameterMap[alias ?: property.name]?.let {
// javaGetterを呼び出す方が高速
javaGetter.isAccessible = true
argumentBucket.setArgument(javaGetter.invoke(src)?.let { value -> mapObject(it, value) }, it.index)
argumentBucket.setArgument(it.param, javaGetter.invoke(src)?.let { value -> mapObject(it, value) })
// 終了判定
if (argumentBucket.isInitialized) return
}
Expand All @@ -79,7 +68,7 @@ class KMapper<T : Any> private constructor(
src.forEach { (key, value) ->
parameterMap[key]?.let { param ->
// 取得した内容がnullでなければ適切にmapする
argumentBucket.setArgument(value?.let { mapObject(param, it) }, param.index)
argumentBucket.setArgument(param.param, value?.let { mapObject(param, it) })
// 終了判定
if (argumentBucket.isInitialized) return
}
Expand All @@ -88,39 +77,28 @@ class KMapper<T : Any> private constructor(

private fun bindArguments(argumentBucket: ArgumentBucket, srcPair: Pair<*, *>) {
parameterMap[srcPair.first.toString()]?.let {
argumentBucket.setArgument(srcPair.second?.let { value ->
mapObject(
it,
value
)
}, it.index)
argumentBucket.setArgument(it.param, srcPair.second?.let { value -> mapObject(it, value) })
}
}

fun map(srcMap: Map<String, Any?>): T {
val bucket: ArgumentBucket = function.getArgumentBucket()
bindArguments(bucket, srcMap)

if (!bucket.isInitialized) throwExceptionOnNotInitialized(bucket)

return function.call(bucket)
}

fun map(srcPair: Pair<String, Any?>): T {
val bucket: ArgumentBucket = function.getArgumentBucket()
bindArguments(bucket, srcPair)

if (!bucket.isInitialized) throwExceptionOnNotInitialized(bucket)

return function.call(bucket)
}

fun map(src: Any): T {
val bucket: ArgumentBucket = function.getArgumentBucket()
bindArguments(bucket, src)

if (!bucket.isInitialized) throwExceptionOnNotInitialized(bucket)

return function.call(bucket)
}

Expand All @@ -135,8 +113,6 @@ class KMapper<T : Any> private constructor(
}
}

if (!bucket.isInitialized) throwExceptionOnNotInitialized(bucket)

return function.call(bucket)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/mapk/kmapper/ParameterForMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.full.staticFunctions
import kotlin.reflect.jvm.isAccessible

internal class ParameterForMap<T : Any> private constructor(val index: Int, val clazz: KClass<T>) {
internal class ParameterForMap<T : Any> private constructor(val param: KParameter, val clazz: KClass<T>) {
val javaClazz: Class<T> by lazy {
clazz.java
}
Expand All @@ -28,7 +28,7 @@ internal class ParameterForMap<T : Any> private constructor(val index: Int, val

companion object {
fun newInstance(param: KParameter): ParameterForMap<*> {
return ParameterForMap(param.index, param.type.classifier as KClass<*>)
return ParameterForMap(param, param.type.classifier as KClass<*>)
}
}
}
Expand Down