Skip to content

Commit

Permalink
fix(#99): remove duplicate let when using @KonvertFrom with an ex…
Browse files Browse the repository at this point in the history
…pression
  • Loading branch information
mcarleio committed Nov 17, 2024
1 parent ce4eaad commit ff16653
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ $className(${"⇥\n%L"}
val expression = "let·{ ${source.expression} }"
CodeBlock.of(
source.mappingParamName
?.let { "$it.let·{ $expression }" }
?.let { "$it.$expression" }
?: expression
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class KonvertFromITest : KonverterITest() {
code = SourceFile.kotlin(
name = "TestCode.kt",
contents =
"""
"""
import io.mcarle.konvert.api.KonvertFrom
import io.mcarle.konvert.api.Mapping
Expand Down Expand Up @@ -66,7 +66,7 @@ class TargetClass(
code = SourceFile.kotlin(
name = "TestCode.kt",
contents =
"""
"""
import io.mcarle.konvert.api.KonvertFrom
import io.mcarle.konvert.api.Mapping
Expand Down Expand Up @@ -105,7 +105,7 @@ class TargetClass(
code = SourceFile.kotlin(
name = "TestCode.kt",
contents =
"""
"""
import io.mcarle.konvert.api.KonvertFrom
import io.mcarle.konvert.api.Mapping
Expand Down Expand Up @@ -133,7 +133,7 @@ class TargetClass<T>(
code = SourceFile.kotlin(
name = "TestCode.kt",
contents =
"""
"""
import io.mcarle.konvert.api.KonvertFrom
import io.mcarle.konvert.api.Mapping
Expand All @@ -159,7 +159,7 @@ class TargetClass(
code = SourceFile.kotlin(
name = "TestCode.kt",
contents =
"""
"""
import io.mcarle.konvert.api.KonvertFrom
import io.mcarle.konvert.api.Mapping
Expand Down Expand Up @@ -187,7 +187,7 @@ annotation class TargetClass(
code = SourceFile.kotlin(
name = "TestCode.kt",
contents =
"""
"""
import io.mcarle.konvert.api.KonvertFrom
import io.mcarle.konvert.api.Mapping
Expand Down Expand Up @@ -215,7 +215,7 @@ class TargetClass<T>(
code = SourceFile.kotlin(
name = "TestCode.kt",
contents =
"""
"""
import io.mcarle.konvert.api.KonvertFrom
import io.mcarle.konvert.api.Mapping
Expand Down Expand Up @@ -243,7 +243,7 @@ object TargetClass {
code = SourceFile.kotlin(
name = "TestCode.kt",
contents =
"""
"""
import io.mcarle.konvert.api.KonvertFrom
import io.mcarle.konvert.api.Mapping
Expand Down Expand Up @@ -271,6 +271,43 @@ data class TargetProperty(val value: String) {
assertContains(extensionFunctionCode, "TargetProperty.fromSourceProperty(sourceProperty = sourceClass.sourceProperty)")
}

@Test
fun useExpression() {
val (compilation) = compileWith(
enabledConverters = listOf(SameTypeConverter()),
code = SourceFile.kotlin(
name = "TestCode.kt",
contents =
"""
import io.mcarle.konvert.api.KonvertFrom
import io.mcarle.konvert.api.Mapping
class SourceClass(
val sourceProperty: String
)
@KonvertFrom(SourceClass::class, mappings=[
Mapping(target="targetProperty", expression = "it.sourceProperty.lowercase()"),
])
class TargetClass(
var targetProperty: String
) {
companion object
}
""".trimIndent()
)
)
val extensionFunctionCode = compilation.generatedSourceFor("TargetClassKonverter.kt")
println(extensionFunctionCode)

assertSourceEquals(
"""
public fun TargetClass.Companion.fromSourceClass(sourceClass: SourceClass): TargetClass = TargetClass(
targetProperty = sourceClass.let { it.sourceProperty.lowercase() }
)
""".trimIndent(),
extensionFunctionCode
)
}

@Test
fun handleDifferentPackages() {
Expand All @@ -280,7 +317,7 @@ data class TargetProperty(val value: String) {
SourceFile.kotlin(
name = "a/SourceClass.kt",
contents =
"""
"""
package a
class SourceClass(val property: String)
Expand All @@ -289,7 +326,7 @@ class SourceClass(val property: String)
SourceFile.kotlin(
name = "b/TargetClass.kt",
contents =
"""
"""
package b
import io.mcarle.konvert.api.KonvertFrom
Expand Down Expand Up @@ -329,7 +366,7 @@ class TargetClass {
SourceFile.kotlin(
name = "a/SomeClass.kt",
contents =
"""
"""
package a
class SomeClass(val property: String)
Expand All @@ -338,7 +375,7 @@ class SomeClass(val property: String)
SourceFile.kotlin(
name = "b/SomeClass.kt",
contents =
"""
"""
package b
import io.mcarle.konvert.api.KonvertFrom
Expand Down Expand Up @@ -374,7 +411,7 @@ class SomeClass {
SourceFile.kotlin(
name = "a/SomeClass.kt",
contents =
"""
"""
package a
Expand All @@ -384,7 +421,7 @@ class SomeClass(val property: String)
SourceFile.kotlin(
name = "b/SomeClass.kt",
contents =
"""
"""
package b
import io.mcarle.konvert.api.KonvertFrom
Expand Down Expand Up @@ -469,7 +506,7 @@ data class SourceClass(val property: String)
code = SourceFile.kotlin(
name = "TestCode.kt",
contents =
"""
"""
import io.mcarle.konvert.api.KonvertFrom
import io.mcarle.konvert.api.Mapping
Expand Down Expand Up @@ -503,7 +540,7 @@ class TargetClass(val children: List<TargetClass>) {
SourceFile.kotlin(
name = "a/Person.kt",
contents =
"""
"""
package a
import io.mcarle.konvert.api.KonvertFrom
Expand All @@ -520,7 +557,7 @@ data class Person(val firstName: String, val lastName: String, val age: Int, val
SourceFile.kotlin(
name = "b/PersonDto.kt",
contents =
"""
"""
package b
import io.mcarle.konvert.api.KonvertFrom
Expand Down Expand Up @@ -578,7 +615,7 @@ data class PersonDto(val firstName: String, val lastName: String, val age: Int,
SourceFile.kotlin(
name = "a/Target.kt",
contents =
"""
"""
package a
import io.mcarle.konvert.api.KonvertFrom
Expand All @@ -602,7 +639,7 @@ data class TargetProperty(val value: String) {
SourceFile.kotlin(
name = "b/Source.kt",
contents =
"""
"""
package b
class SourceClass(
Expand Down

0 comments on commit ff16653

Please sign in to comment.