From 87f223121cd75ade60d92a879711c7f05502af88 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Fri, 24 Jul 2020 17:19:29 +0900 Subject: [PATCH 1/5] =?UTF-8?q?Converter=E3=81=AE=E5=A4=89=E6=8F=9B?= =?UTF-8?q?=E9=96=A2=E6=95=B0=E3=81=AEnullability=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 変換処理は入力がnullだった場合呼び出されず、またnull入力に関してはデフォルト引数で記述すべきと判断したため --- src/main/kotlin/com/mapk/conversion/KConvert.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/mapk/conversion/KConvert.kt b/src/main/kotlin/com/mapk/conversion/KConvert.kt index 8b6f226..ab6c938 100644 --- a/src/main/kotlin/com/mapk/conversion/KConvert.kt +++ b/src/main/kotlin/com/mapk/conversion/KConvert.kt @@ -9,5 +9,5 @@ annotation class KConvertBy(val converters: Array(protected val annotation: A) { abstract val srcClass: KClass - abstract fun convert(source: S?): D? + abstract fun convert(source: S): D? } From a10e6b62a5688e5dde4fd9d8f08d369e9437b545 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Fri, 24 Jul 2020 17:19:42 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=E5=A4=89=E6=9B=B4=E3=81=AB=E5=90=88?= =?UTF-8?q?=E3=82=8F=E3=81=9B=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/com/mapk/kmapper/ConversionTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt b/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt index bd717be..1f42c55 100644 --- a/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt +++ b/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt @@ -36,7 +36,7 @@ class ConversionTest { } override val srcClass = String::class - override fun convert(source: String?): Number? = source?.let(converter) + override fun convert(source: String): Number? = source.let(converter) } class FromNumber(annotation: ToNumber) : AbstractKConverter(annotation) { @@ -53,7 +53,7 @@ class ConversionTest { } override val srcClass = Number::class - override fun convert(source: Number?): Number? = source?.let(converter) + override fun convert(source: Number): Number? = source.let(converter) } data class Dst(@ToNumber(BigDecimal::class) val number: BigDecimal) From cf09caa091cbad8416c3fe60d73bc63edab9cbbd Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Fri, 24 Jul 2020 17:22:58 +0900 Subject: [PATCH 3/5] =?UTF-8?q?null=E5=85=A5=E5=8A=9B=E6=99=82=E3=81=AF?= =?UTF-8?q?=E3=82=B3=E3=83=B3=E3=83=90=E3=83=BC=E3=82=BF=E3=82=92=E5=8B=95?= =?UTF-8?q?=E3=81=8B=E3=81=95=E3=81=AA=E3=81=84=E3=82=88=E3=81=86=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/com/mapk/kmapper/BoundParameterForMap.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/mapk/kmapper/BoundParameterForMap.kt b/src/main/kotlin/com/mapk/kmapper/BoundParameterForMap.kt index 1f1a598..c3828ab 100644 --- a/src/main/kotlin/com/mapk/kmapper/BoundParameterForMap.kt +++ b/src/main/kotlin/com/mapk/kmapper/BoundParameterForMap.kt @@ -29,7 +29,7 @@ internal sealed class BoundParameterForMap { override val propertyGetter: Method, private val converter: KFunction<*> ) : BoundParameterForMap() { - override fun map(src: S): Any? = converter.call(propertyGetter.invoke(src)) + override fun map(src: S): Any? = propertyGetter.invoke(src)?.let { converter.call(it) } } internal class UseKMapper( From d3a8966600d85cda3ff3344722cee61425ee7e3f Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Fri, 24 Jul 2020 17:37:39 +0900 Subject: [PATCH 4/5] =?UTF-8?q?null=E3=82=92=E5=85=A5=E3=82=8C=E3=81=9F?= =?UTF-8?q?=E9=9A=9B=E3=81=AB=E5=A4=89=E6=8F=9B=E5=87=A6=E7=90=86=E3=81=AB?= =?UTF-8?q?=E5=85=A5=E3=81=A3=E3=81=A6=E3=81=84=E3=81=AA=E3=81=84=E3=81=93?= =?UTF-8?q?=E3=81=A8=E3=81=AE=E7=A2=BA=E8=AA=8D=E7=94=A8=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/mapk/kmapper/ConversionTest.kt | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt b/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt index 1f42c55..e52dfe3 100644 --- a/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt +++ b/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt @@ -8,8 +8,11 @@ import java.math.BigInteger import kotlin.reflect.KClass import kotlin.reflect.jvm.jvmName import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNull import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertDoesNotThrow import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.EnumSource import org.junit.jupiter.params.provider.ValueSource @@ -56,9 +59,10 @@ class ConversionTest { override fun convert(source: Number): Number? = source.let(converter) } - data class Dst(@ToNumber(BigDecimal::class) val number: BigDecimal) + data class Dst(@ToNumber(BigDecimal::class) val number: BigDecimal?) data class NumberSrc(val number: Number) data class StringSrc(val number: String) + object NullSrc { val number: Number? = null } enum class NumberSource(val values: Array) { Doubles(arrayOf(1.0, -2.0, 3.5)), @@ -89,6 +93,15 @@ class ConversionTest { val actual = KMapper(::Dst).map(StringSrc(str)) assertEquals(0, BigDecimal(str).compareTo(actual.number)) } + + @Test + @DisplayName("nullを入れた際に変換処理に入らないことのテスト") + fun fromNull() { + assertDoesNotThrow { + val actual = KMapper(::Dst).map(NullSrc) + assertNull(actual.number) + } + } } @Nested @@ -111,6 +124,15 @@ class ConversionTest { val actual = PlainKMapper(::Dst).map(StringSrc(str)) assertEquals(0, BigDecimal(str).compareTo(actual.number)) } + + @Test + @DisplayName("nullを入れた際に変換処理に入らないことのテスト") + fun fromNull() { + assertDoesNotThrow { + val actual = PlainKMapper(::Dst).map(NullSrc) + assertNull(actual.number) + } + } } @Nested @@ -133,5 +155,14 @@ class ConversionTest { val actual = BoundKMapper().map(StringSrc(str)) assertEquals(0, BigDecimal(str).compareTo(actual.number)) } + + @Test + @DisplayName("nullを入れた際に変換処理に入らないことのテスト") + fun fromNull() { + assertDoesNotThrow { + val actual = BoundKMapper(::Dst).map(NullSrc) + assertNull(actual.number) + } + } } } From 2303efeebd5c8e5278bb79b071ddbee86e512b2c Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Fri, 24 Jul 2020 17:46:31 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=E3=83=9E=E3=83=83=E3=83=91=E3=83=BC?= =?UTF-8?q?=E3=81=AE=E5=AE=A3=E8=A8=80=E3=82=92=E5=88=87=E3=82=8A=E5=87=BA?= =?UTF-8?q?=E3=81=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/mapk/kmapper/ConversionTest.kt | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt b/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt index e52dfe3..cc97765 100644 --- a/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt +++ b/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt @@ -76,12 +76,14 @@ class ConversionTest { @Nested @DisplayName("KMapper") inner class KMapperTest { + private val mapper = KMapper(::Dst) + @ParameterizedTest @EnumSource(NumberSource::class) @DisplayName("Numberソース") fun fromNumber(numbers: NumberSource) { numbers.values.forEach { - val actual = KMapper(::Dst).map(NumberSrc(it)) + val actual = mapper.map(NumberSrc(it)) assertEquals(0, BigDecimal.valueOf(it.toDouble()).compareTo(actual.number)) } } @@ -90,7 +92,7 @@ class ConversionTest { @ValueSource(strings = ["100", "2.0", "-500"]) @DisplayName("Stringソース") fun fromString(str: String) { - val actual = KMapper(::Dst).map(StringSrc(str)) + val actual = mapper.map(StringSrc(str)) assertEquals(0, BigDecimal(str).compareTo(actual.number)) } @@ -98,7 +100,7 @@ class ConversionTest { @DisplayName("nullを入れた際に変換処理に入らないことのテスト") fun fromNull() { assertDoesNotThrow { - val actual = KMapper(::Dst).map(NullSrc) + val actual = mapper.map(NullSrc) assertNull(actual.number) } } @@ -107,12 +109,14 @@ class ConversionTest { @Nested @DisplayName("PlainKMapper") inner class PlainKMapperTest { + private val mapper = PlainKMapper(::Dst) + @ParameterizedTest @EnumSource(NumberSource::class) @DisplayName("Numberソース") fun fromNumber(numbers: NumberSource) { numbers.values.forEach { - val actual = PlainKMapper(::Dst).map(NumberSrc(it)) + val actual = mapper.map(NumberSrc(it)) assertEquals(0, BigDecimal.valueOf(it.toDouble()).compareTo(actual.number)) } } @@ -121,7 +125,7 @@ class ConversionTest { @ValueSource(strings = ["100", "2.0", "-500"]) @DisplayName("Stringソース") fun fromString(str: String) { - val actual = PlainKMapper(::Dst).map(StringSrc(str)) + val actual = mapper.map(StringSrc(str)) assertEquals(0, BigDecimal(str).compareTo(actual.number)) } @@ -129,7 +133,7 @@ class ConversionTest { @DisplayName("nullを入れた際に変換処理に入らないことのテスト") fun fromNull() { assertDoesNotThrow { - val actual = PlainKMapper(::Dst).map(NullSrc) + val actual = mapper.map(NullSrc) assertNull(actual.number) } }