File tree Expand file tree Collapse file tree
annotation/src/commonMain/kotlin/io/github/jacksever/automapper/annotation
processor/src/main/kotlin/io/github/jacksever/automapper/processor/helper
commonMain/kotlin/io/github/jacksever/automapper/sample/mapper
jvmTest/kotlin/io/github/jacksever/automapper/sample/mapper Expand file tree Collapse file tree Original file line number Diff line number Diff 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)
244244fun userMapper (user : User ): UserEntity
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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 */
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments