Skip to content

Commit

Permalink
Merge pull request #127 from netglade/fix/reverse-type-converters
Browse files Browse the repository at this point in the history
Fix reverse with type converters
  • Loading branch information
tenhobi authored Oct 2, 2023
2 parents 5142b82 + 960b0de commit bbd698f
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 13 deletions.
3 changes: 3 additions & 0 deletions packages/auto_mappr/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[//]: # (## Unreleased)

## 2.0.1
- Fix type converters when used with reverse mappings.

## 2.0.0
- **Breaking**: Allow "absorbing" modules using `includes` on `@AutoMappr`. Previous `modules` is now `delegates`. [#117](https://github.com/netglade/auto_mappr/pull/117)
- **Breaking**: Remove shared AutoMappr builder that used PartBuilder, now `.auto_mappr.dart` is generated using LibraryBuilder. [#117](https://github.com/netglade/auto_mappr/pull/117)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class ClassBodyBuilder extends MapBodyBuilderBase {
targetField: targetField,
targetConstructorParam: constructorAssignment,
fieldMapping: mapping.tryGetFieldMapping(targetField.displayName),
typeConverters: mapping.typeConverters,
);

mappedTargetConstructorParams.add(sourceAssignment);
Expand Down Expand Up @@ -165,6 +166,7 @@ class ClassBodyBuilder extends MapBodyBuilderBase {
targetField: targetField,
fieldMapping: fieldMapping,
targetConstructorParam: constructorAssignment,
typeConverters: mapping.typeConverters,
),
);
}
Expand Down Expand Up @@ -246,6 +248,7 @@ class ClassBodyBuilder extends MapBodyBuilderBase {
assignment: SourceAssignment(
sourceField: sourceField,
targetField: targetField,
typeConverters: mapping.typeConverters,
),
usedNullableMethodCallback: usedNullableMethodCallback,
).build(),
Expand Down
20 changes: 10 additions & 10 deletions packages/auto_mappr/lib/src/builder/value_assignment_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ class ValueAssignmentBuilder {
.property(sourceField.name);

final assignmentBuilders = [
// Type converter.
TypeConverterBuilder(
assignment: assignment,
mapperConfig: mapperConfig,
mapping: mapping,
usedNullableMethodCallback: usedNullableMethodCallback,
source: assignment.sourceType!,
target: assignment.targetType,
convertMethodArgument: rightSide,
),
// Iterable.
IterableAssignmentBuilder(
assignment: assignment,
Expand Down Expand Up @@ -75,16 +85,6 @@ class ValueAssignmentBuilder {
target: assignment.targetType,
convertMethodArgument: rightSide,
),
// Type converter for primitive type.
TypeConverterBuilder(
assignment: assignment,
mapperConfig: mapperConfig,
mapping: mapping,
usedNullableMethodCallback: usedNullableMethodCallback,
source: assignment.sourceType!,
target: assignment.targetType,
convertMethodArgument: rightSide,
),
];

// Try to assign value using one of assignment builder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class AutoMapprGenerator extends GeneratorForAnnotation<annotation.AutoMappr> {
source: targetType,
target: sourceType,
fieldMappings: fieldMappings ?? [],
typeConverters: [..._toTypeConverters(mapTypeConverters), ...globalConverters],
whenSourceIsNullExpression: whenSourceIsNull,
constructor: constructor,
ignoreFieldNull: ignoreFieldNull,
Expand Down
2 changes: 1 addition & 1 deletion packages/auto_mappr/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: auto_mappr
description: Code generation for mapping between different objects with ease.
version: 2.0.0
version: 2.0.1
repository: https://github.com/netglade/auto_mappr
issue_tracker: https://github.com/netglade/auto_mappr/issues
screenshots:
Expand Down
60 changes: 58 additions & 2 deletions packages/auto_mappr/test/integration/fixture/type_converters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ import 'type_converters/module_alpha.dart';
MapType<InListDto, InList>(),
MapType<InMapDto, InMap>(),
MapType<IncludesDto, Includes>(),
// Post w/ reverse.
MapType<PostDto, Post>(reverse: true),
],
converters: [
TypeConverter<String, Value<String>>(Mappr.stringToValueString),
TypeConverter<Object, Value<Object>>(Mappr.objectToValueObject2),
// Post w/ reverse.
userDtoConverter,
userConverter,
],
includes: [MapprAlpha()],
)
Expand Down Expand Up @@ -78,7 +83,11 @@ class NormalFieldDto {
final String xString;
final bool normalBool;

const NormalFieldDto({required this.xInt, required this.xString, required this.normalBool});
const NormalFieldDto({
required this.xInt,
required this.xString,
required this.normalBool,
});
}

class NormalField with EquatableMixin {
Expand All @@ -99,7 +108,11 @@ class InListDto {
final String xString;
final bool normalBool;

const InListDto({required this.xInt, required this.xString, required this.normalBool});
const InListDto({
required this.xInt,
required this.xString,
required this.normalBool,
});
}

class InList with EquatableMixin {
Expand Down Expand Up @@ -161,3 +174,46 @@ class Value<T> with EquatableMixin {

const Value(this.value);
}

// Post w/ reverse.

class Post with EquatableMixin {
final User user;

@override
List<Object?> get props => [user];

const Post({required this.user});
}

class PostDto with EquatableMixin {
final UserDto user;

@override
List<Object?> get props => [user];

const PostDto({required this.user});
}

class User with EquatableMixin {
final String id;

@override
List<Object?> get props => [id];

const User({required this.id});
}

class UserDto with EquatableMixin {
final String id;

@override
List<Object?> get props => [id];

const UserDto({required this.id});
}

const userDtoConverter = TypeConverter(userDtoToUser);
const userConverter = TypeConverter(userToUserDto);
UserDto userToUserDto(User source) => UserDto(id: source.id);
User userDtoToUser(UserDto source) => User(id: source.id);
16 changes: 16 additions & 0 deletions packages/auto_mappr/test/integration/type_converters_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,20 @@ void main() {

expect(converted, equals(const fixture.Includes(false)));
});

group('with reverse', () {
test('Dto to entity', () {
const dto = fixture.PostDto(user: fixture.UserDto(id: 'alpha123'));
final converted = mappr.convert<fixture.PostDto, fixture.Post>(dto);

expect(converted, equals(const fixture.Post(user: fixture.User(id: 'alpha123'))));
});

test('Entity to dto', () {
const dto = fixture.Post(user: fixture.User(id: 'beta123'));
final converted = mappr.convert<fixture.Post, fixture.PostDto>(dto);

expect(converted, equals(const fixture.PostDto(user: fixture.UserDto(id: 'beta123'))));
});
});
}

0 comments on commit bbd698f

Please sign in to comment.