Skip to content

Commit bbd698f

Browse files
authored
Merge pull request #127 from netglade/fix/reverse-type-converters
Fix reverse with type converters
2 parents 5142b82 + 960b0de commit bbd698f

File tree

7 files changed

+92
-13
lines changed

7 files changed

+92
-13
lines changed

packages/auto_mappr/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
[//]: # (## Unreleased)
22

3+
## 2.0.1
4+
- Fix type converters when used with reverse mappings.
5+
36
## 2.0.0
47
- **Breaking**: Allow "absorbing" modules using `includes` on `@AutoMappr`. Previous `modules` is now `delegates`. [#117](https://github.com/netglade/auto_mappr/pull/117)
58
- **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)

packages/auto_mappr/lib/src/builder/map_bodies/class_body_builder.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class ClassBodyBuilder extends MapBodyBuilderBase {
115115
targetField: targetField,
116116
targetConstructorParam: constructorAssignment,
117117
fieldMapping: mapping.tryGetFieldMapping(targetField.displayName),
118+
typeConverters: mapping.typeConverters,
118119
);
119120

120121
mappedTargetConstructorParams.add(sourceAssignment);
@@ -165,6 +166,7 @@ class ClassBodyBuilder extends MapBodyBuilderBase {
165166
targetField: targetField,
166167
fieldMapping: fieldMapping,
167168
targetConstructorParam: constructorAssignment,
169+
typeConverters: mapping.typeConverters,
168170
),
169171
);
170172
}
@@ -246,6 +248,7 @@ class ClassBodyBuilder extends MapBodyBuilderBase {
246248
assignment: SourceAssignment(
247249
sourceField: sourceField,
248250
targetField: targetField,
251+
typeConverters: mapping.typeConverters,
249252
),
250253
usedNullableMethodCallback: usedNullableMethodCallback,
251254
).build(),

packages/auto_mappr/lib/src/builder/value_assignment_builder.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ class ValueAssignmentBuilder {
4444
.property(sourceField.name);
4545

4646
final assignmentBuilders = [
47+
// Type converter.
48+
TypeConverterBuilder(
49+
assignment: assignment,
50+
mapperConfig: mapperConfig,
51+
mapping: mapping,
52+
usedNullableMethodCallback: usedNullableMethodCallback,
53+
source: assignment.sourceType!,
54+
target: assignment.targetType,
55+
convertMethodArgument: rightSide,
56+
),
4757
// Iterable.
4858
IterableAssignmentBuilder(
4959
assignment: assignment,
@@ -75,16 +85,6 @@ class ValueAssignmentBuilder {
7585
target: assignment.targetType,
7686
convertMethodArgument: rightSide,
7787
),
78-
// Type converter for primitive type.
79-
TypeConverterBuilder(
80-
assignment: assignment,
81-
mapperConfig: mapperConfig,
82-
mapping: mapping,
83-
usedNullableMethodCallback: usedNullableMethodCallback,
84-
source: assignment.sourceType!,
85-
target: assignment.targetType,
86-
convertMethodArgument: rightSide,
87-
),
8888
];
8989

9090
// Try to assign value using one of assignment builder.

packages/auto_mappr/lib/src/generator/auto_mappr_generator.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ class AutoMapprGenerator extends GeneratorForAnnotation<annotation.AutoMappr> {
167167
source: targetType,
168168
target: sourceType,
169169
fieldMappings: fieldMappings ?? [],
170+
typeConverters: [..._toTypeConverters(mapTypeConverters), ...globalConverters],
170171
whenSourceIsNullExpression: whenSourceIsNull,
171172
constructor: constructor,
172173
ignoreFieldNull: ignoreFieldNull,

packages/auto_mappr/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: auto_mappr
22
description: Code generation for mapping between different objects with ease.
3-
version: 2.0.0
3+
version: 2.0.1
44
repository: https://github.com/netglade/auto_mappr
55
issue_tracker: https://github.com/netglade/auto_mappr/issues
66
screenshots:

packages/auto_mappr/test/integration/fixture/type_converters.dart

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ import 'type_converters/module_alpha.dart';
1717
MapType<InListDto, InList>(),
1818
MapType<InMapDto, InMap>(),
1919
MapType<IncludesDto, Includes>(),
20+
// Post w/ reverse.
21+
MapType<PostDto, Post>(reverse: true),
2022
],
2123
converters: [
2224
TypeConverter<String, Value<String>>(Mappr.stringToValueString),
2325
TypeConverter<Object, Value<Object>>(Mappr.objectToValueObject2),
26+
// Post w/ reverse.
27+
userDtoConverter,
28+
userConverter,
2429
],
2530
includes: [MapprAlpha()],
2631
)
@@ -78,7 +83,11 @@ class NormalFieldDto {
7883
final String xString;
7984
final bool normalBool;
8085

81-
const NormalFieldDto({required this.xInt, required this.xString, required this.normalBool});
86+
const NormalFieldDto({
87+
required this.xInt,
88+
required this.xString,
89+
required this.normalBool,
90+
});
8291
}
8392

8493
class NormalField with EquatableMixin {
@@ -99,7 +108,11 @@ class InListDto {
99108
final String xString;
100109
final bool normalBool;
101110

102-
const InListDto({required this.xInt, required this.xString, required this.normalBool});
111+
const InListDto({
112+
required this.xInt,
113+
required this.xString,
114+
required this.normalBool,
115+
});
103116
}
104117

105118
class InList with EquatableMixin {
@@ -161,3 +174,46 @@ class Value<T> with EquatableMixin {
161174

162175
const Value(this.value);
163176
}
177+
178+
// Post w/ reverse.
179+
180+
class Post with EquatableMixin {
181+
final User user;
182+
183+
@override
184+
List<Object?> get props => [user];
185+
186+
const Post({required this.user});
187+
}
188+
189+
class PostDto with EquatableMixin {
190+
final UserDto user;
191+
192+
@override
193+
List<Object?> get props => [user];
194+
195+
const PostDto({required this.user});
196+
}
197+
198+
class User with EquatableMixin {
199+
final String id;
200+
201+
@override
202+
List<Object?> get props => [id];
203+
204+
const User({required this.id});
205+
}
206+
207+
class UserDto with EquatableMixin {
208+
final String id;
209+
210+
@override
211+
List<Object?> get props => [id];
212+
213+
const UserDto({required this.id});
214+
}
215+
216+
const userDtoConverter = TypeConverter(userDtoToUser);
217+
const userConverter = TypeConverter(userToUserDto);
218+
UserDto userToUserDto(User source) => UserDto(id: source.id);
219+
User userDtoToUser(UserDto source) => User(id: source.id);

packages/auto_mappr/test/integration/type_converters_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,20 @@ void main() {
7272

7373
expect(converted, equals(const fixture.Includes(false)));
7474
});
75+
76+
group('with reverse', () {
77+
test('Dto to entity', () {
78+
const dto = fixture.PostDto(user: fixture.UserDto(id: 'alpha123'));
79+
final converted = mappr.convert<fixture.PostDto, fixture.Post>(dto);
80+
81+
expect(converted, equals(const fixture.Post(user: fixture.User(id: 'alpha123'))));
82+
});
83+
84+
test('Entity to dto', () {
85+
const dto = fixture.Post(user: fixture.User(id: 'beta123'));
86+
final converted = mappr.convert<fixture.Post, fixture.PostDto>(dto);
87+
88+
expect(converted, equals(const fixture.PostDto(user: fixture.UserDto(id: 'beta123'))));
89+
});
90+
});
7591
}

0 commit comments

Comments
 (0)