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

Commit 164cbf6

Browse files
authored
Merge pull request #9 from k163377/feature
Modify annotation.
2 parents f7aacd8 + 1109c25 commit 164cbf6

File tree

6 files changed

+60
-26
lines changed

6 files changed

+60
-26
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ repositories {
3030
dependencies {
3131
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
3232
implementation(kotlin("reflect"))
33-
implementation("com.github.ProjectMapK:Shared:0.2")
33+
implementation("com.github.ProjectMapK:Shared:0.3")
3434

3535
// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter
3636
testImplementation(group = "org.junit.jupiter", name = "junit-jupiter", version = "5.6.0") {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.mapk.annotations
22

3-
@Target(AnnotationTarget.PROPERTY)
3+
@Target(AnnotationTarget.PROPERTY_GETTER)
44
@Retention(AnnotationRetention.RUNTIME)
55
annotation class KGetterAlias(val value: String)
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.mapk.annotations
22

3-
@Target(AnnotationTarget.PROPERTY)
3+
@Target(AnnotationTarget.PROPERTY_GETTER)
44
@Retention(AnnotationRetention.RUNTIME)
55
@MustBeDocumented
6-
annotation class KPropertyIgnore
6+
annotation class KGetterIgnore

src/main/kotlin/com/mapk/kmapper/KMapper.kt

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package com.mapk.kmapper
22

33
import com.mapk.annotations.KConstructor
44
import com.mapk.annotations.KGetterAlias
5-
import com.mapk.annotations.KPropertyAlias
6-
import com.mapk.annotations.KPropertyIgnore
5+
import com.mapk.annotations.KGetterIgnore
6+
import com.mapk.annotations.KParameterAlias
77
import com.mapk.core.ArgumentBucket
88
import com.mapk.core.EnumMapper
99
import com.mapk.core.KFunctionForCall
@@ -35,7 +35,7 @@ class KMapper<T : Any> private constructor(
3535
private val parameterMap: Map<String, ParameterForMap<*>> = function.parameters
3636
.filter { it.kind != KParameter.Kind.INSTANCE }
3737
.associate {
38-
(it.findAnnotation<KPropertyAlias>()?.value ?: propertyNameConverter(it.name!!)) to
38+
(it.findAnnotation<KParameterAlias>()?.value ?: propertyNameConverter(it.name!!)) to
3939
ParameterForMap.newInstance(it)
4040
}
4141

@@ -51,21 +51,26 @@ class KMapper<T : Any> private constructor(
5151
}
5252

5353
private fun bindArguments(argumentBucket: ArgumentBucket, src: Any) {
54-
src::class.memberProperties.forEach { property ->
55-
val javaGetter: Method? = property.javaGetter
56-
if (javaGetter != null && property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) {
57-
parameterMap[property.findAnnotation<KGetterAlias>()?.value ?: property.name]?.let {
58-
// javaGetterを呼び出す方が高速
59-
javaGetter.isAccessible = true
60-
argumentBucket.setArgument(javaGetter.invoke(src)?.let { value ->
61-
mapObject(
62-
it,
63-
value
64-
)
65-
}, it.index)
66-
// 終了判定
67-
if (argumentBucket.isInitialized) return
68-
}
54+
src::class.memberProperties.forEach outer@{ property ->
55+
// propertyが公開されていない場合は処理を行わない
56+
if (property.visibility != KVisibility.PUBLIC) return@outer
57+
58+
// ゲッターが取れない場合は処理を行わない
59+
val javaGetter: Method = property.javaGetter ?: return@outer
60+
61+
var alias: String? = null
62+
// NOTE: IgnoreとAliasが同時に指定されるようなパターンを考慮してaliasが取れてもbreakしていない
63+
javaGetter.annotations.forEach {
64+
if (it is KGetterIgnore) return@outer // ignoreされている場合は処理を行わない
65+
if (it is KGetterAlias) alias = it.value
66+
}
67+
68+
parameterMap[alias ?: property.name]?.let {
69+
// javaGetterを呼び出す方が高速
70+
javaGetter.isAccessible = true
71+
argumentBucket.setArgument(javaGetter.invoke(src)?.let { value -> mapObject(it, value) }, it.index)
72+
// 終了判定
73+
if (argumentBucket.isInitialized) return
6974
}
7075
}
7176
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.mapk.kmapper
2+
3+
import com.mapk.annotations.KGetterIgnore
4+
import org.junit.jupiter.api.Assertions.assertEquals
5+
import org.junit.jupiter.api.Assertions.assertTrue
6+
import org.junit.jupiter.api.DisplayName
7+
import org.junit.jupiter.api.Test
8+
9+
class KGetterIgnoreTest {
10+
data class Src1(val arg1: Int, val arg2: String, @get:KGetterIgnore val arg3: Short)
11+
data class Src2(@get:KGetterIgnore val arg2: String, val arg3: Int, val arg4: String)
12+
13+
data class Dst(val arg1: Int, val arg2: String, val arg3: Int, val arg4: String)
14+
15+
@Test
16+
@DisplayName("フィールドを無視するテスト")
17+
fun test() {
18+
val src1 = Src1(1, "2-1", 31)
19+
val src2 = Src2("2-2", 32, "4")
20+
21+
val mapper = KMapper(::Dst)
22+
23+
val dst1 = mapper.map(src1, src2)
24+
val dst2 = mapper.map(src2, src1)
25+
26+
assertTrue(dst1 == dst2)
27+
assertEquals(Dst(1, "2-1", 32, "4"), dst1)
28+
}
29+
}

src/test/kotlin/com/mapk/kmapper/ParamAliasTest.kt renamed to src/test/kotlin/com/mapk/kmapper/PropertyAliasTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
package com.mapk.kmapper
22

33
import com.mapk.annotations.KGetterAlias
4-
import com.mapk.annotations.KPropertyAlias
4+
import com.mapk.annotations.KParameterAlias
55
import org.junit.jupiter.api.Assertions.assertEquals
66
import org.junit.jupiter.api.DisplayName
77
import org.junit.jupiter.api.Test
88

99
private data class AliasedDst(
1010
val arg1: Double,
11-
@param:KPropertyAlias("arg3") val arg2: Int
11+
@param:KParameterAlias("arg3") val arg2: Int
1212
)
1313

1414
private data class AliasedSrc(
15-
@KGetterAlias("arg1")
15+
@get:KGetterAlias("arg1")
1616
val arg2: Double,
1717
val arg3: Int
1818
)
1919

2020
@DisplayName("エイリアスを貼った場合のテスト")
21-
class ParamAliasTest {
21+
class PropertyAliasTest {
2222
@Test
2323
@DisplayName("パラメータにエイリアスを貼った場合")
2424
fun paramAliasTest() {

0 commit comments

Comments
 (0)