Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use T -> R type converters for T? -> R? convert #142

Merged
merged 8 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/auto_mappr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,31 @@ static Value<Object> objectToValueObject2(Object source) {
}
```

To make it more clear,
here is a list of type converters
and for which
**source field type** -> **target field type**
combinations they can be used.
In these examples,
we've used `int` and `String` as a reference,
but the `TypeConverter`s can be adapted to various data types.

- `TypeConverter<int, String>` ... aka `String converter(int) => ...`
- source field `int` -> target field `String`
- source field `int` -> target field `String?`
- source field `int?` -> target field `String?`, when source field IS NOT null
- `TypeConverter<int, String?>` ... aka `String? converter(int) => ...`
- source field `int` -> target field `String?`
- source field `int?` -> target field `String?`, when source field IS NOT null
- `TypeConverter<int?, String>` ... aka `String converter(int?) => ...`
- source field `int` -> target field `String`
- source field `int?` -> target field `String`
- source field `int` -> target field `String?`
- source field `int?` -> target field `String?`
- `TypeConverter<int?, String?>` ... aka `String? converter(int?) => ...`
- source field `int` -> target field `String?`
- source field `int?` -> target field `String?`

### Reverse mapping

When you want to create a bidirectional mapping (e.g. normal: source to target and reversed: target to source),
Expand Down
2 changes: 2 additions & 0 deletions packages/auto_mappr/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ targets:
:auto_mappr:
enabled: true
generate_for:
# include:
# - test/integration/fixture/type_converters/required_to_nullable.dart
exclude:
- test/integration/error_fixture/**
options:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,67 @@ class TypeConverterBuilder extends AssignmentBuilderBase {
@override
bool canAssign() {
return assignment.typeConverters.firstWhereOrNull(
(converter) => converter.canBeUsed(
mappingSource: source,
mappingTarget: target,
),
(converter) =>
converter.canBeUsed(
mappingSource: source,
mappingTarget: target,
) ||
converter.canBeUsedNullable(
mappingSource: source,
mappingTarget: target,
),
) !=
null;
}

@override
Expression buildAssignment() {
final converter = assignment.typeConverters
// ignore: avoid-unsafe-collection-methods, checked by [canAssign]
.firstWhere((c) => c.canBeUsed(mappingSource: source, mappingTarget: target));
final converter = assignment.typeConverters.firstWhere(
(c) => c.canBeUsed(mappingSource: source, mappingTarget: target),
// ignore: avoid-unsafe-collection-methods, checked by [canAssign]
orElse: () => assignment.typeConverters.firstWhere(
(c) => c.canBeUsedNullable(
mappingSource: source,
mappingTarget: target,
),
),
);

// Call.
if (convertMethodArgument case final methodArgument?) {
final targetRefer = EmitterHelper.current.typeRefer(type: target);

return EmitterHelper.current
.refer(converter.converter.referCallString, converter.converter.library.identifier)
.call([methodArgument]).asA(targetRefer);
// Can be used.
if (converter.canBeUsed(mappingSource: source, mappingTarget: target)) {
return EmitterHelper.current
.refer(
converter.converter.referCallString,
converter.converter.library.identifier,
)
.call([methodArgument]).asA(targetRefer);
}

// Can be used nullable.
return methodArgument.equalTo(literalNull).conditional(
literalNull,
EmitterHelper.current
.refer(
converter.converter.referCallString,
converter.converter.library.identifier,
)
.call([methodArgument.nullChecked]).asA(targetRefer),
);
}

final sourceEmitted = EmitterHelper.current.typeReferEmitted(type: source);
final targetEmitted = EmitterHelper.current.typeReferEmitted(type: target);

// Tear-off.
return EmitterHelper.current
.refer(converter.converter.referCallString, converter.converter.library.identifier)
.refer(
converter.converter.referCallString,
converter.converter.library.identifier,
)
.asA(refer('$targetEmitted Function($sourceEmitted)'));
}
}
15 changes: 15 additions & 0 deletions packages/auto_mappr/lib/src/models/type_converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ class TypeConverter extends Equatable {
_isConverterSubtype(target, mappingTarget, _ConversionRole.target);
}

bool canBeUsedNullable({
required DartType mappingSource,
required DartType mappingTarget,
}) {
// ignore: avoid-inverted-boolean-checks, this is better
if (!(mappingSource.isNullable && mappingTarget.isNullable)) return false;

return canBeUsed(
mappingSource: mappingSource.element!.library!.typeSystem.promoteToNonNull(mappingSource),
mappingTarget: target.isNullable
? mappingTarget
: mappingTarget.element!.library!.typeSystem.promoteToNonNull(mappingTarget),
);
}

bool _isConverterSubtype(DartType converterType, DartType fieldType, _ConversionRole role) {
// Same type.
if (converterType == fieldType) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'required_to_nullable.auto_mappr.dart';
[
// Object -> Object?
MapType<RequiredInput, NullableOutput>(),
// Object? -> Object?, when source is not null
MapType<NullableInput, NullableOutput>(),
],
converters: [
TypeConverter<String, Value<String>?>(RequiredToNullableConverterMappr.stringToNullableValueString),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'required_to_required.auto_mappr.dart';
MapType<RequiredInput, RequiredOutput>(),
// Object -> Object?
MapType<RequiredInput, NullableOutput>(),
// Object? -> Object?, when source is not null
MapType<NullableInput, NullableOutput>(),
],
converters: [
TypeConverter<String, Value<String>>(RequiredToRequiredConverterMappr.stringToValueString),
Expand Down
14 changes: 14 additions & 0 deletions packages/auto_mappr/test/integration/type_converters_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ void main() {

expect(converted, equals(const fixture.NullableOutput(fixture.Value('aaa'))));
});

test('Object? -> Object?, when source is not null', () {
const input = fixture.NullableInput('aaa');
final converted = mapprX.convert<fixture.NullableInput, fixture.NullableOutput>(input);

expect(converted, equals(const fixture.NullableOutput(fixture.Value('aaa'))));
});
});

group('TypeConverter<Object, Object?>', () {
Expand All @@ -128,6 +135,13 @@ void main() {

expect(converted, equals(const fixture.NullableOutput(fixture.Value('aaa'))));
});

test('Object? -> Object?, when source is not null', () {
const input = fixture.NullableInput('aaa');
final converted = mapprX.convert<fixture.NullableInput, fixture.NullableOutput>(input);

expect(converted, equals(const fixture.NullableOutput(fixture.Value('aaa'))));
});
});

group('TypeConverter<Object?, Object>', () {
Expand Down
Loading