Skip to content

Commit

Permalink
chore: revert adjacent string ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
mathew65 committed Jan 21, 2025
1 parent 2349cc3 commit 744c8a0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ class MapModelBodyMethodBuilder {
? mapping.whenSourceIsNullExpression!.returned
: refer('Exception').newInstance([
refer(
// ignore: avoid-adjacent-strings, to avoid parsing the errors in tests
"r'Mapping $mapping failed because ${mapping.source} was null, and no default value was provided. '\n"
"r'Consider setting the whenSourceIsNull parameter on the MapType<${mapping.source}, ${mapping.target}> to handle null values during mapping.'",
),
Expand Down
173 changes: 85 additions & 88 deletions packages/auto_mappr/lib/src/generator/auto_mappr_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,95 +106,92 @@ class AutoMapprGenerator extends GeneratorForAnnotation<annotation.AutoMappr> {
required List<TypeConverter> globalConverters,
required ClassElement element,
}) {
return mappers
.map((mapper) {
final mapperType = mapper.type! as ParameterizedType;

final sourceType = mapperType.typeArguments.firstOrNull;
final targetType = mapperType.typeArguments.lastOrNull;

if (sourceType is! InterfaceType) {
final emittedSource = EmitterHelper.current.typeReferEmitted(type: sourceType);

throw InvalidGenerationSourceError(
'$emittedSource is not a class and cannot be mapped from',
element: element,
todo: 'Use a class',
);
}
if (targetType is! InterfaceType) {
final emittedTarget = EmitterHelper.current.typeReferEmitted(type: targetType);

throw InvalidGenerationSourceError(
'$emittedTarget is not a class and cannot be mapped to',
element: element,
todo: 'Use a class',
);
}

final fields = mapper.getField(mapTypeFieldFields)?.toListValue();
final mapTypeConverters = mapper.getField(mapTypeFieldConverters)?.toListValue() ?? [];
final whenSourceIsNull = mapper.getField(mapTypeFieldWhenSourceIsNull)?.toCodeExpression();
final constructor = mapper.getField(mapTypeFieldConstructor)?.toStringValue();
final willIgnoreFieldNull = mapper.getField(mapTypeFieldIgnoreFieldNull)?.toBoolValue();
final isReverse = mapper.getField(mapTypeFieldReverse)?.toBoolValue();
final hasSafeMapping = mapper.getField(mapTypeSafeMapping)?.toBoolValue();

final fieldMappings = fields
?.map(
(fieldMapping) => FieldMapping(
field: fieldMapping.getField(fieldFieldField)!.toStringValue()!,
ignore: fieldMapping.getField(fieldFieldIgnore)!.toBoolValue()!,
from: fieldMapping.getField(fieldFieldFrom)!.toStringValue(),
customExpression:
fieldMapping.getField(fieldFieldCustom)!.toCodeExpression(maybePassModelArgument: true),
whenNullExpression: fieldMapping.getField(fieldFieldWhenNull)!.toCodeExpression(),
ignoreNull: fieldMapping.getField(fieldFieldIgnoreNull)!.toBoolValue(),
),
)
.toList();

return [
TypeMapping(
source: sourceType,
target: targetType,
fieldMappings: fieldMappings ?? [],
typeConverters: [..._toTypeConverters(mapTypeConverters), ...globalConverters],
whenSourceIsNullExpression: whenSourceIsNull,
constructor: constructor,
ignoreFieldNull: willIgnoreFieldNull,
safeMapping: hasSafeMapping,
final res = mappers.map((mapper) {
final mapperType = mapper.type! as ParameterizedType;

final sourceType = mapperType.typeArguments.firstOrNull;
final targetType = mapperType.typeArguments.lastOrNull;

if (sourceType is! InterfaceType) {
final emittedSource = EmitterHelper.current.typeReferEmitted(type: sourceType);

throw InvalidGenerationSourceError(
'$emittedSource is not a class and cannot be mapped from',
element: element,
todo: 'Use a class',
);
}
if (targetType is! InterfaceType) {
final emittedTarget = EmitterHelper.current.typeReferEmitted(type: targetType);

throw InvalidGenerationSourceError(
'$emittedTarget is not a class and cannot be mapped to',
element: element,
todo: 'Use a class',
);
}

final fields = mapper.getField(mapTypeFieldFields)?.toListValue();
final mapTypeConverters = mapper.getField(mapTypeFieldConverters)?.toListValue() ?? [];
final whenSourceIsNull = mapper.getField(mapTypeFieldWhenSourceIsNull)?.toCodeExpression();
final constructor = mapper.getField(mapTypeFieldConstructor)?.toStringValue();
final willIgnoreFieldNull = mapper.getField(mapTypeFieldIgnoreFieldNull)?.toBoolValue();
final isReverse = mapper.getField(mapTypeFieldReverse)?.toBoolValue();
final hasSafeMapping = mapper.getField(mapTypeSafeMapping)?.toBoolValue();

final fieldMappings = fields
?.map(
(fieldMapping) => FieldMapping(
field: fieldMapping.getField(fieldFieldField)!.toStringValue()!,
ignore: fieldMapping.getField(fieldFieldIgnore)!.toBoolValue()!,
from: fieldMapping.getField(fieldFieldFrom)!.toStringValue(),
customExpression: fieldMapping.getField(fieldFieldCustom)!.toCodeExpression(maybePassModelArgument: true),
whenNullExpression: fieldMapping.getField(fieldFieldWhenNull)!.toCodeExpression(),
ignoreNull: fieldMapping.getField(fieldFieldIgnoreNull)!.toBoolValue(),
),
if (isReverse ?? false)
TypeMapping(
source: targetType,
target: sourceType,
fieldMappings: fieldMappings
?.map(
(f) => f.from != null
? FieldMapping(
field: f.from!,
from: f.field,
customExpression: f.customExpression,
whenNullExpression: f.whenNullExpression,
ignore: f.ignore,
ignoreNull: f.ignoreNull,
)
: f,
)
.toList() ??
[],
typeConverters: [..._toTypeConverters(mapTypeConverters), ...globalConverters],
whenSourceIsNullExpression: whenSourceIsNull,
constructor: constructor,
ignoreFieldNull: willIgnoreFieldNull,
safeMapping: hasSafeMapping,
),
];
})
// ignore: avoid-slow-collection-methods, it's ok to use here
.flattened
.toList();
)
.toList();

return [
TypeMapping(
source: sourceType,
target: targetType,
fieldMappings: fieldMappings ?? [],
typeConverters: [..._toTypeConverters(mapTypeConverters), ...globalConverters],
whenSourceIsNullExpression: whenSourceIsNull,
constructor: constructor,
ignoreFieldNull: willIgnoreFieldNull,
safeMapping: hasSafeMapping,
),
if (isReverse ?? false)
TypeMapping(
source: targetType,
target: sourceType,
fieldMappings: fieldMappings
?.map(
(f) => f.from != null
? FieldMapping(
field: f.from!,
from: f.field,
customExpression: f.customExpression,
whenNullExpression: f.whenNullExpression,
ignore: f.ignore,
ignoreNull: f.ignoreNull,
)
: f,
)
.toList() ??
[],
typeConverters: [..._toTypeConverters(mapTypeConverters), ...globalConverters],
whenSourceIsNullExpression: whenSourceIsNull,
constructor: constructor,
ignoreFieldNull: willIgnoreFieldNull,
safeMapping: hasSafeMapping,
),
];
});

return res.flattened.toList();
}

/// Recursively returns all mappings from includes.
Expand Down

0 comments on commit 744c8a0

Please sign in to comment.