Skip to content

Commit

Permalink
Support zero argument function in custom mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
petrnymsa committed Dec 13, 2023
1 parent 6dd33a0 commit 857b424
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/injectable/lib/getit.config.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions packages/auto_mappr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,11 @@ class Mappr extends $Mappr {}
When you need to assign a custom function or a const value as a value for given target field,
you can use the `custom` argument in a `Field` mapping.
Alternatively, you can use the `Field.custom()` constructor
which hides other then-invalid parameters.
which hides other then-invalid parameters. Provide `const Target` value or custom mapping function.

You can set up `Target Function(Source dto)` function or `const Target` value.
Custom function has to follow one of these formals:
- has `Source` model argument - `Target Function(Source dto)`
- has exactly zero arguments and returns `Target`

```dart
@AutoMappr([
Expand All @@ -298,7 +300,8 @@ class Mappr extends $Mappr {
static String mapName(UserDto dto) => dto.name.toUpperCase();
}
int mapAge(UserDto _) => 42;
/// Return always 42
int mapAge() => 42;
```

### Ignore mapping
Expand Down
33 changes: 33 additions & 0 deletions packages/auto_mappr/example/lib/mappr.auto_mappr.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 29 additions & 1 deletion packages/auto_mappr/example/lib/mappr.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import 'package:auto_mappr_annotation/auto_mappr_annotation.dart';

import 'package:auto_mappr_example/mappr.auto_mappr.dart';

class Utils {
static DateTime mapDateTime() {
return DateTime.now();
}

static String? mapSpecial(UserDto model) => '${model.xname}?!';
}

@AutoMappr([
MapType<UserDto, User>(
fields: [Field('name', from: 'xname'), Field('born', custom: Utils.mapDateTime)],
),
MapType<UserDto, AnotherUser>(
fields: [
Field('name', from: 'xname'),
Field('born', custom: Utils.mapDateTime),
Field('special', custom: Utils.mapSpecial)
],
),
])
Expand All @@ -14,10 +26,26 @@ class Mappr extends $Mappr {}
class User {
final int id;
final String name;
final DateTime born;

const User({
required this.id,
required this.name,
required this.born,
});
}

class AnotherUser {
final int id;
final String name;
final DateTime born;
final String? special;

AnotherUser({
required this.id,
required this.name,
required this.born,
this.special,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension DartObjectExtension on DartObject {
final asFunction = toFunctionValue();
if (asFunction != null) {
return EmitterHelper.current.refer(asFunction.referCallString, asFunction.library.identifier).call([
if (passModelArgument) refer('model'),
if (passModelArgument && asFunction.parameters.isNotEmpty) refer('model'),
]);
}

Expand Down
11 changes: 6 additions & 5 deletions packages/auto_mappr/test/integration/custom_mapping_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,20 @@ void main() {
expect(
converted,
equals(
const fixture.CustomFunctionFromEmpty(
fixture.CustomFunctionFromEmpty(
1.2,
2,
74.58,
'some test',
true,
[null, true, 3, 8.6],
[
const [null, true, 3, 8.6],
const [
[null, 'xx'],
[true, 3, 8.6],
],
{1, 2, 3, 4, 5},
{'one': 11, 'two': 22, 'three': 33},
const {1, 2, 3, 4, 5},
const {'one': 11, 'two': 22, 'three': 33},
DateTime(2023),
),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import 'custom_mapping.auto_mappr.dart';
Field.custom('listListValue', custom: Mappr.emptyToListListValue),
Field.custom('setValue', custom: Mappr.emptyToSetValue),
Field.custom('mapValue', custom: Mappr.emptyToMapValue),
Field.custom('dateValue', custom: Mappr.dateTimeFixed),
],
),
// from value
Expand Down Expand Up @@ -107,6 +108,8 @@ class Mappr extends $Mappr {
static String convertToNameAndIdPositional(CustomFunctionPositionalDto? dto) => '${dto?.name} #${dto?.id}';

static String convertToNameAndIdNamed(CustomFunctionNamedDto? dto) => '${dto?.name} #${dto?.id}';

static DateTime dateTimeFixed() => DateTime(2023);
}

// custom type
Expand Down Expand Up @@ -203,6 +206,7 @@ class CustomFunctionFromEmpty extends Equatable {
final List<List<Object?>> listListValue;
final Set<int> setValue;
final Map<String, int> mapValue;
final DateTime dateValue;

@override
List<Object?> get props => [
Expand All @@ -215,6 +219,7 @@ class CustomFunctionFromEmpty extends Equatable {
listListValue,
setValue,
mapValue,
dateValue,
];

const CustomFunctionFromEmpty(
Expand All @@ -227,6 +232,7 @@ class CustomFunctionFromEmpty extends Equatable {
this.listListValue,
this.setValue,
this.mapValue,
this.dateValue,
);
}

Expand Down

0 comments on commit 857b424

Please sign in to comment.