Skip to content

Commit

Permalink
fix(#75): use placeholder %L instead of concatenating String and Co…
Browse files Browse the repository at this point in the history
…deBlock
  • Loading branch information
mcarleio committed May 23, 2024
1 parent 5d8f317 commit 72530fb
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }"
}
}

Expand All @@ -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()
Expand All @@ -193,34 +193,33 @@ 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 }"
}
}
}

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()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String, TargetProperty>
) {
@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<String, SourceProperty>
)
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
)
}

}

0 comments on commit 72530fb

Please sign in to comment.