diff --git a/converter/src/main/kotlin/io/mcarle/konvert/converter/MapToXConverter.kt b/converter/src/main/kotlin/io/mcarle/konvert/converter/MapToXConverter.kt index 18461b4..2b36d1f 100644 --- a/converter/src/main/kotlin/io/mcarle/konvert/converter/MapToXConverter.kt +++ b/converter/src/main/kotlin/io/mcarle/konvert/converter/MapToXConverter.kt @@ -132,13 +132,12 @@ abstract class MapToXConverter( else -> { mapTypeChanged = true - "$fieldName$nc.mapValues·{·(_,·it)·-> ${ - valueTypeConverter.convert( - "it", - genericSourceValueType, - genericTargetValueType - ) - } }" + args += valueTypeConverter.convert( + "it", + genericSourceValueType, + genericTargetValueType + ) + "$fieldName$nc.mapValues·{·(_,·it)·-> %L }" } } @@ -163,10 +162,11 @@ abstract class MapToXConverter( else -> { mapTypeChanged = true + args += valueTypeConverter.convert("value", genericSourceValueType, genericTargetValueType) """ $fieldName$nc.map·{·(key,·value)·-> ⇥val·newKey·=·key!! -val·newValue·=·${valueTypeConverter.convert("value", genericSourceValueType, genericTargetValueType)} +val·newValue·=·%L newKey·to·newValue ⇤}$nc.toMap() """.trimIndent() @@ -193,13 +193,12 @@ newKey·to·newValue else -> { mapTypeChanged = true - "$fieldName$nc.mapValues·{·(_,·it)·-> ${ - valueTypeConverter.convert( - "it", - genericSourceValueType, - genericTargetValueType - ) - } }" + args += valueTypeConverter.convert( + "it", + genericSourceValueType, + genericTargetValueType + ) + "$fieldName$nc.mapValues·{·(_,·it)·-> %L }" } } } @@ -207,20 +206,20 @@ newKey·to·newValue else -> when { genericSourceValueType == genericTargetValueType -> { mapTypeChanged = true - "$fieldName$nc.mapKeys·{·(it,·_)·-> ${ - keyTypeConverter.convert( - "it", - genericSourceKeyType, - genericTargetKeyType - ) - } }" + args += keyTypeConverter.convert( + "it", + genericSourceKeyType, + genericTargetKeyType + ) + "$fieldName$nc.mapKeys·{·(it,·_)·-> %L }" } needsNotNullAssertionOperator(genericSourceValueType, genericTargetValueType) -> { mapTypeChanged = true + args += keyTypeConverter.convert("key", genericSourceKeyType, genericTargetKeyType) """ $fieldName$nc.map·{·(key,·value)·-> -⇥val·newKey·=·${keyTypeConverter.convert("key", genericSourceKeyType, genericTargetKeyType)} +⇥val·newKey·=·%L val·newValue·=·value!! newKey·to·newValue ⇤}$nc.toMap() @@ -232,17 +231,18 @@ newKey·to·newValue changedTypes = true } mapTypeChanged = true - "$fieldName$nc.mapKeys·{·(it,·_)·-> ${ - keyTypeConverter.convert("it", genericSourceKeyType, genericTargetKeyType) - } }" + args += keyTypeConverter.convert("it", genericSourceKeyType, genericTargetKeyType) + "$fieldName$nc.mapKeys·{·(it,·_)·-> %L }" } else -> { mapTypeChanged = true + args += keyTypeConverter.convert("key", genericSourceKeyType, genericTargetKeyType) + args += valueTypeConverter.convert("value", genericSourceValueType, genericTargetValueType) """ $fieldName$nc.map·{·(key,·value)·-> -⇥val·newKey·=·${keyTypeConverter.convert("key", genericSourceKeyType, genericTargetKeyType)} -val·newValue·=·${valueTypeConverter.convert("value", genericSourceValueType, genericTargetValueType)} +⇥val·newKey·=·%L +val·newValue·=·%L newKey·to·newValue ⇤}$nc.toMap() """.trimIndent() diff --git a/processor/src/test/kotlin/io/mcarle/konvert/processor/konvertfrom/KonvertFromITest.kt b/processor/src/test/kotlin/io/mcarle/konvert/processor/konvertfrom/KonvertFromITest.kt index d9cd60a..1e07bde 100644 --- a/processor/src/test/kotlin/io/mcarle/konvert/processor/konvertfrom/KonvertFromITest.kt +++ b/processor/src/test/kotlin/io/mcarle/konvert/processor/konvertfrom/KonvertFromITest.kt @@ -4,6 +4,7 @@ import com.tschuchort.compiletesting.KotlinCompilation import com.tschuchort.compiletesting.SourceFile import io.mcarle.konvert.api.DEFAULT_KONVERT_FROM_PRIORITY import io.mcarle.konvert.converter.IterableToListConverter +import io.mcarle.konvert.converter.MapToMapConverter import io.mcarle.konvert.converter.SameTypeConverter import io.mcarle.konvert.converter.api.TypeConverterRegistry import io.mcarle.konvert.converter.api.config.GENERATED_FILENAME_SUFFIX_OPTION @@ -575,4 +576,69 @@ data class PersonDto(val firstName: String, val lastName: String, val age: Int, ) } + @Test + fun useOtherMappersWithPackages() { + val (compilation) = compileWith( + enabledConverters = listOf(SameTypeConverter(), MapToMapConverter()), + expectResultCode = KotlinCompilation.ExitCode.OK, + code = arrayOf( + SourceFile.kotlin( + name = "a/Target.kt", + contents = + """ +package a + +import io.mcarle.konvert.api.KonvertFrom +import b.SourceClass +import b.SourceProperty + +class TargetClass( + val property: TargetProperty, + val other: Map +) { + @KonvertFrom(SourceClass::class) + companion object +} + +@KonvertFrom(SourceProperty::class) +data class TargetProperty(val value: String) { + companion object +} + """.trimIndent() + ), + SourceFile.kotlin( + name = "b/Source.kt", + contents = + """ +package b + +class SourceClass( + val property: SourceProperty, + val other: Map +) +data class SourceProperty(val value: String) + """.trimIndent() + ) + ) + ) + val extensionFunctionCode = compilation.generatedSourceFor("TargetClassKonverter.kt") + println(extensionFunctionCode) + + assertSourceEquals( + """ +package a + +import b.SourceClass + +public fun TargetClass.Companion.fromSourceClass(sourceClass: SourceClass): TargetClass = + TargetClass( + property = TargetProperty.fromSourceProperty(sourceProperty = sourceClass.property), + other = sourceClass.other.mapValues { (_, it) -> + TargetProperty.fromSourceProperty(sourceProperty = it) } +) + """.trimIndent(), + extensionFunctionCode + ) + } + }