Skip to content

Commit

Permalink
Update README.md for safe_mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
PfeiJit authored May 11, 2024
1 parent 7ff2127 commit bd45d58
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion packages/auto_mappr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Heavily inspired by [C# AutoMapper][auto_mapper_net_link].
- [Mapping from source](#mapping-from-source)
- [Nullability handling](#nullability-handling)
- [Forced non-nullable field for nullable source](#forced-non-nullable-field-for-nullable-source)
- [Safe mapping](#safe-mapping)
- [Generics](#generics)
- [Library import aliases](#library-import-aliases)
- [Modules](#modules)
Expand Down Expand Up @@ -479,7 +480,7 @@ This is by design and it is up to developer decide if bang operator is appropiat
```yaml
$default:
builders:
:auto_mappr:
auto_mappr:
options:
ignoreNullableSourceField: true
```
Expand All @@ -488,6 +489,37 @@ This is by design and it is up to developer decide if bang operator is appropiat

Precedense is `global configuration` -> `MapType` -> `Field` -> defaults to false.

### Safe mapping
Sometimes it can happen that mapping goes wrong and exception is thrown. Typically it can be caused
by mapping from source with a nullable variable to a target with a corresponding non-nullable variable.
If you want to avoid propagating the exception through the app, auto_mappr can catch it and return null instead. In case you want to know
that something went wrong during the mapping there is a `onMappingError` callback that is invoked each time error occurs. If you want enable
safe mapping, it can be done either on the global level in `build.yaml` with `safeMapping` option

```yaml
$default:
builders:
auto_mappr:
options:
safeMapping: true
```

or individually for each `MapType`:

```dart
@AutoMappr([
MapType<UserDto, User>(
safeMapping: true,
),
])
class Mappr extends $Mappr {}
```
Note that global `safeMapping` has always the priority.

Safe mapping can be done only through `tryConvert`, `tryConvertIterable`, `tryConvertList` or `tryConvertSet` methods. All of them have an optional parameter
`void Function(Object error, StackTrace stackTrace, SOURCE? source)? onMappingError`, which is a callback function triggered whenever an exception occurs during the mapping process.
This can be used fo example for logging the error.

### Generics

AutoMappr can handle generics, so you can map objects with type arguments with ease.
Expand Down

0 comments on commit bd45d58

Please sign in to comment.