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

Commit

Permalink
Merge pull request #42 from k163377/fix_converter_nullability
Browse files Browse the repository at this point in the history
Fix converter nullability.
  • Loading branch information
k163377 authored Jul 24, 2020
2 parents 08b5947 + 2303efe commit 6418203
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/mapk/conversion/KConvert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ annotation class KConvertBy(val converters: Array<KClass<out AbstractKConverter<

abstract class AbstractKConverter<A : Annotation, S : Any, D : Any>(protected val annotation: A) {
abstract val srcClass: KClass<S>
abstract fun convert(source: S?): D?
abstract fun convert(source: S): D?
}
2 changes: 1 addition & 1 deletion src/main/kotlin/com/mapk/kmapper/BoundParameterForMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal sealed class BoundParameterForMap<S> {
override val propertyGetter: Method,
private val converter: KFunction<*>
) : BoundParameterForMap<S>() {
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<S : Any>(
Expand Down
49 changes: 42 additions & 7 deletions src/test/kotlin/com/mapk/kmapper/ConversionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<ToNumber, Number, Number>(annotation) {
Expand All @@ -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<Number>) {
Doubles(arrayOf(1.0, -2.0, 3.5)),
Expand All @@ -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))
}
}
Expand All @@ -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))
}
}
Expand All @@ -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
Expand All @@ -133,5 +159,14 @@ class ConversionTest {
val actual = BoundKMapper<StringSrc, Dst>().map(StringSrc(str))
assertEquals(0, BigDecimal(str).compareTo(actual.number))
}

@Test
@DisplayName("nullを入れた際に変換処理に入らないことのテスト")
fun fromNull() {
assertDoesNotThrow {
val actual = BoundKMapper<NullSrc, Dst>(::Dst).map(NullSrc)
assertNull(actual.number)
}
}
}
}

0 comments on commit 6418203

Please sign in to comment.