Skip to content

Commit f6c55dd

Browse files
committed
fix(#139): use %L with CodeBlock instead of toString()
1 parent 171aaca commit f6c55dd

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

processor/src/main/kotlin/io/mcarle/konvert/processor/codegen/MappingCodeGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ $className(${"⇥\n%L"}
182182
valueParamHasDefault = true
183183
)
184184
if (convertedValue != null) {
185-
CodeBlock.of("$targetVarName.${sourceProperty.targetName}·=·$convertedValue")
185+
CodeBlock.of("$targetVarName.${sourceProperty.targetName}·=·%L", convertedValue)
186186
} else {
187187
null
188188
}

processor/src/test/kotlin/io/mcarle/konvert/processor/TargetStructureITest.kt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,4 +923,56 @@ data class TargetClass(val property: Int)
923923
assertContains(compilationResult.messages, NoMatchingTypeConverterException::class.qualifiedName!!)
924924
}
925925

926+
@Test
927+
fun emptyConstructorPropertiesDifferentPackage() {
928+
val (compilation) = compileWith(
929+
enabledConverters = listOf(SameTypeConverter()),
930+
code = arrayOf(
931+
SourceFile.kotlin(
932+
name = "a/SourceClass.kt",
933+
contents =
934+
"""
935+
package a
936+
937+
import io.mcarle.konvert.api.KonvertTo
938+
import io.mcarle.konvert.processor.SomeTestClass
939+
import b.TargetClass
940+
941+
@KonvertTo(TargetClass::class)
942+
data class SourceClass(val property: SomeTestClass)
943+
""".trimIndent()
944+
),
945+
SourceFile.kotlin(
946+
name = "b/TargetClass.kt",
947+
contents =
948+
"""
949+
package b
950+
951+
import io.mcarle.konvert.processor.SomeOtherTestClass
952+
953+
class TargetClass {
954+
var property: SomeOtherTestClass? = null
955+
}
956+
""".trimIndent()
957+
),
958+
)
959+
)
960+
val extensionFunctionCode = compilation.generatedSourceFor("SourceClassKonverter.kt")
961+
println(extensionFunctionCode)
962+
963+
assertSourceEquals(
964+
"""
965+
package a
966+
967+
import b.TargetClass
968+
import io.mcarle.konvert.processor.toSomeOtherTestClass
969+
970+
public fun SourceClass.toTargetClass(): TargetClass = TargetClass().also { targetClass ->
971+
targetClass.property = property.toSomeOtherTestClass()
972+
}
973+
""".trimIndent(),
974+
extensionFunctionCode
975+
)
976+
}
977+
926978
}

0 commit comments

Comments
 (0)