Skip to content

Commit be17ecc

Browse files
committed
[Processor] Simplify default value handling for strings
This commit simplifies the usage of `defaultValue` in `PropertyMapping`. String default values no longer need to be wrapped in escaped quotes. The processor now automatically adds quotes if the target type is a `String`. Key changes: - Updated the processor to wrap the `defaultValue` in quotes if the target property is a String and the value isn't already quoted. - Removed the now-unnecessary escaped quotes from `defaultValue` assignments in all sample and test files. - Clarified the documentation in the `PropertyMapping` annotation to reflect this simplified usage.
1 parent 3c303f5 commit be17ecc

5 files changed

Lines changed: 22 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ import io.github.jacksever.automapper.annotation.PropertyMapping
238238
// Renames 'id' to 'userId'
239239
PropertyMapping(from = "id", to = "userId"),
240240
// Provides a default value for 'address' if it's null
241-
PropertyMapping(from = "address", defaultValue = "\"Unknown\"")
241+
PropertyMapping(from = "address", defaultValue = "Unknown")
242242
]
243243
)
244244
fun userMapper(user: User): UserEntity

automapper/annotation/src/commonMain/kotlin/io/github/jacksever/automapper/annotation/PropertyMapping.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ package io.github.jacksever.automapper.annotation
2222
* @property from name of the property in the source class
2323
* @property to name of the property in the target class. If omitted, it is assumed to be the same as [from]
2424
* @property defaultValue string literal to be used as a default value if the source property is null and the target is not nullable.
25-
* For example, `defaultValue = "\"\""` for an empty string, or `defaultValue = "0L"` for a Long
25+
* For example, `defaultValue = ""` for an empty string, or `defaultValue = "0L"` for a Long
2626
*/
2727
@Target
2828
@Retention(AnnotationRetention.SOURCE)

automapper/processor/src/main/kotlin/io/github/jacksever/automapper/processor/helper/ParameterHelper.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal object ParameterHelper {
6363
val conversion = getConversionExpression(
6464
sourceType = sourceType,
6565
targetType = targetType,
66-
defaultValue = mapping?.defaultValue.orEmpty(),
66+
defaultValue = mapping?.defaultValue,
6767
)
6868

6969
add("$targetParamName = ${sourceProperty.simpleName.asString()}$conversion")
@@ -107,7 +107,7 @@ internal object ParameterHelper {
107107
"!!$conversion"
108108
} else {
109109
// A default value is provided, use Elvis operator
110-
"$conversion ?: $defaultValue"
110+
"$conversion ?: ${formatDefaultValue(targetType, defaultValue)}"
111111
}
112112
} else {
113113
if (conversion.isNotEmpty()) {
@@ -220,6 +220,19 @@ internal object ParameterHelper {
220220
}
221221
}
222222

223+
/**
224+
* Formats the default value string based on the target type
225+
*/
226+
private fun formatDefaultValue(targetType: KSType, defaultValue: String): String {
227+
val isString = targetType.declaration.simpleName.asString() == "String"
228+
229+
return if (isString && !defaultValue.startsWith(prefix = "\"")) {
230+
"\"$defaultValue\""
231+
} else {
232+
defaultValue
233+
}
234+
}
235+
223236
/**
224237
* Checks if the class declaration corresponds to a List type (Kotlin or Java)
225238
*/

sample/src/commonMain/kotlin/io/github/jacksever/automapper/sample/mapper/MapperModule.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ internal interface MapperModule {
4848
@AutoMapper(
4949
propertyMappings = [
5050
PropertyMapping(from = "id", to = "userId"),
51-
PropertyMapping(from = "address", defaultValue = "\"Unknown\""),
52-
PropertyMapping(from = "middleName", to = "patronymic", defaultValue = "\"N/A\"")
51+
PropertyMapping(from = "address", defaultValue = "Unknown"),
52+
PropertyMapping(from = "middleName", to = "patronymic", defaultValue = "N/A")
5353
]
5454
)
5555
fun userMapper(user: User): UserEntity
@@ -63,7 +63,7 @@ internal interface MapperModule {
6363
@AutoMapper(
6464
reversible = false,
6565
propertyMappings = [
66-
PropertyMapping(from = "address", defaultValue = "\"Unknown\""),
66+
PropertyMapping(from = "address", defaultValue = "Unknown"),
6767
]
6868
)
6969
fun uiUserMapper(user: User): UiUser

sample/src/jvmTest/kotlin/io/github/jacksever/automapper/sample/mapper/DataClassTestMapperModule.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal interface DataClassTestMapperModule {
6363
propertyMappings = [
6464
PropertyMapping(from = "legacyId", to = "id"),
6565
PropertyMapping(from = "statusNum", to = "statusText"),
66-
PropertyMapping(from = "content", defaultValue = "\"Empty\""),
66+
PropertyMapping(from = "content", defaultValue = "Empty"),
6767
]
6868
)
6969
fun complexMapper(from: ComplexSource): ComplexTarget
@@ -78,7 +78,7 @@ internal interface DataClassTestMapperModule {
7878

7979
@AutoMapper(
8080
propertyMappings = [
81-
PropertyMapping(from = "description", defaultValue = "\"Default\""),
81+
PropertyMapping(from = "description", defaultValue = "Default"),
8282
]
8383
)
8484
fun defaultValueMapper(from: DefaultValueSource): DefaultValueTarget

0 commit comments

Comments
 (0)