You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+32-57Lines changed: 32 additions & 57 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,7 @@ Kotlin AutoMapper uses KSP (Kotlin Symbol Processing) to generate extension func
15
15
-**Visibility Control:** Control the visibility of generated extensions (`public` or `internal`) by setting the visibility of your `@AutoMapperModule` interface.
16
16
-**Supports `data`, `enum`, and `sealed` classes:** Covers most mapping scenarios.
17
17
-**Bidirectional & Unidirectional Mapping:** Generate mappings in one or both directions with a simple `reversible` flag.
18
+
-**Customizable Default Values:** Provide default values for properties that are missing in the source or are `null`.
18
19
-**Automatic Primitive Conversion:** Handles conversions like `String` to `Int`, `Int` to `Long`, etc., out of the box.
19
20
-**Collection Mapping:** Automatically handles `List` and `Set` transformations.
20
21
-**Zero Runtime Dependencies:** The `annotation` library is `SOURCE`-only, so it doesn't get bundled into your final artifact.
@@ -210,23 +211,21 @@ interface MapperModule {
210
211
}
211
212
```
212
213
213
-
### Mapping Properties with Different Names and Nullability
214
+
### Customizing Mappings
214
215
215
-
A common scenario is that property names or nullability do not match between layers. AutoMapper handles this gracefully using the `propertyMappings` parameter.
216
+
AutoMapper provides powerful annotations to handle mismatches in property names or to provide default values when a source property is missing or `null`.
216
217
217
-
You can provide an array of `@PropertyMapping` annotations to define custom rules:
218
-
- Use `from` and `to` to map properties with different names.
219
-
- Use `defaultValue` to provide a fallback for a `nullable` source property when the target property is not nullable. If the source name is the same as the target, you can omit the `to` parameter.
218
+
#### Renaming Properties with `@PropertyMapping`
220
219
221
-
#### For Data Classes
220
+
Use the `propertyMappings` array to map properties that have different names between the source and target. Both `from` and `to` parameters are mandatory.
222
221
223
222
*Models:*
224
223
```kotlin
225
-
// Domain: nullable address
226
-
data classUser(valid:Long, valaddress:String?)
224
+
// Domain
225
+
data classUser(valid:Long, valfullName:String)
227
226
228
-
// Data Layer: different id name, non-nullable address
229
-
data classUserEntity(valuserId:Long, valaddress:String)
227
+
// Data Layer
228
+
data classUserEntity(valuserId:Long, valname:String)
The generated code will safely use the Elvis operator for `address` (`address = address ?: "Unknown"`) and will assign the enum constant for `role` (`role = Role.GUEST`).
280
+
281
+
The same approach works for `sealed class` objects. It's best to use the simple class name (e.g., `TaskState.Idle`), as the processor will automatically add the necessary imports.
0 commit comments