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? } 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( diff --git a/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt b/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt index bd717be..cc97765 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 @@ -36,7 +39,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,12 +56,13 @@ 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) + 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)), @@ -72,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)) } } @@ -86,20 +92,31 @@ 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)) } + + @Test + @DisplayName("nullを入れた際に変換処理に入らないことのテスト") + fun fromNull() { + assertDoesNotThrow { + val actual = mapper.map(NullSrc) + assertNull(actual.number) + } + } } @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)) } } @@ -108,9 +125,18 @@ 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)) } + + @Test + @DisplayName("nullを入れた際に変換処理に入らないことのテスト") + fun fromNull() { + assertDoesNotThrow { + val actual = mapper.map(NullSrc) + assertNull(actual.number) + } + } } @Nested @@ -133,5 +159,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) + } + } } }