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

Commit 2a92908

Browse files
committed
処理の共通化
1 parent 930877d commit 2a92908

File tree

1 file changed

+8
-12
lines changed
  • src/main/kotlin/com/wrongwrong/mapk/core

1 file changed

+8
-12
lines changed

src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import java.lang.reflect.Method
88
import kotlin.reflect.KClass
99
import kotlin.reflect.KFunction
1010
import kotlin.reflect.KParameter
11-
import kotlin.reflect.KProperty1
1211
import kotlin.reflect.KVisibility
1312
import kotlin.reflect.full.companionObjectInstance
1413
import kotlin.reflect.full.findAnnotation
@@ -55,16 +54,18 @@ class KMapper<T : Any> private constructor(
5554
}
5655
}
5756

58-
fun map(srcMap: Map<String, Any?>): T {
59-
val array: Array<Any?> = function.argumentArray
60-
61-
srcMap.forEach { (key, value) ->
57+
private fun Map<*, *>.bindParameters(targetArray: Array<Any?>) {
58+
forEach { (key, value) ->
6259
parameterMap[key]?.let { param ->
6360
// 取得した内容がnullでなければ適切にmapする
64-
array[param.index] = value?.let { mapObject(param, it) }
61+
targetArray[param.index] = value?.let { mapObject(param, it) }
6562
}
6663
}
64+
}
6765

66+
fun map(srcMap: Map<String, Any?>): T {
67+
val array: Array<Any?> = function.argumentArray
68+
srcMap.bindParameters(array)
6869
return function.call(array)
6970
}
7071

@@ -85,12 +86,7 @@ class KMapper<T : Any> private constructor(
8586

8687
listOf(*args).forEach { arg ->
8788
when (arg) {
88-
is Map<*, *> -> arg.forEach { (key, value) ->
89-
parameterMap[key]?.let { param ->
90-
// 取得した内容がnullでなければ適切にmapする
91-
array[param.index] = value?.let { mapObject(param, it) }
92-
}
93-
}
89+
is Map<*, *> -> arg.bindParameters(array)
9490
is Pair<*, *> -> parameterMap.getValue(arg.first as String).let {
9591
array[it.index] = arg.second?.let { value -> mapObject(it, value) }
9692
}

0 commit comments

Comments
 (0)