diff --git a/src/Riok.Mapperly/Descriptors/Mappings/DerivedExistingTargetTypeMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/DerivedExistingTargetTypeMapping.cs index 041f902157..eb4f45b397 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/DerivedExistingTargetTypeMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/DerivedExistingTargetTypeMapping.cs @@ -31,48 +31,49 @@ IReadOnlyCollection existingTargetTypeMappings public override IEnumerable Build(TypeMappingBuildContext ctx, ExpressionSyntax target) { var sourceExpression = TupleExpression(CommaSeparatedList(Argument(ctx.Source), Argument(target))); - var sections = _existingTargetTypeMappings.Select(x => BuildSwitchSection(ctx, x)).Append(BuildDefaultSwitchSection(ctx)); + var sections = _existingTargetTypeMappings.Select(x => BuildSwitchSection(ctx, x)).Append(BuildDefaultSwitchSection(ctx, target)); yield return ctx.SyntaxFactory.SwitchStatement(sourceExpression, List(sections)).AddLeadingLineFeed(ctx.SyntaxFactory.Indentation); } private SwitchSectionSyntax BuildSwitchSection(TypeMappingBuildContext ctx, IExistingTargetMapping mapping) { - // MapToB(source, target); var (sectionCtx, sourceVariableName) = ctx.WithNewScopedSource(SourceName); var targetVariableName = sectionCtx.NameBuilder.New(TargetName); sectionCtx = sectionCtx.AddIndentation(); + // (A source, B target) var positionalTypeMatch = PositionalPatternClause( CommaSeparatedList( Subpattern(DeclarationPattern(mapping.SourceType, sourceVariableName)), Subpattern(DeclarationPattern(mapping.TargetType, targetVariableName)) ) ); - var pattern = RecursivePattern().WithPositionalPatternClause(positionalTypeMatch); - var caseLabel = CasePatternSwitchLabel(pattern).AddLeadingLineFeed(sectionCtx.SyntaxFactory.Indentation); - var target = IdentifierName(targetVariableName); + // case (A source, B target): + var caseLabel = CasePatternSwitchLabel(pattern).AddLeadingLineFeed(sectionCtx.SyntaxFactory.Indentation); var statementContext = sectionCtx.AddIndentation(); - var statements = List( - mapping.Build(statementContext, target).Append(BreakStatement().AddLeadingLineFeed(statementContext.SyntaxFactory.Indentation)) - ); - var section = SwitchSection().WithLabels(SingletonList(caseLabel)).WithStatements(statements); - return section; + // break; + var breakStatement = BreakStatement().AddLeadingLineFeed(statementContext.SyntaxFactory.Indentation); + var target = IdentifierName(targetVariableName); + var statements = List(mapping.Build(statementContext, target).Append(breakStatement)); + + return SwitchSection().WithLabels(SingletonList(caseLabel)).WithStatements(statements); } - private SwitchSectionSyntax BuildDefaultSwitchSection(TypeMappingBuildContext ctx) + private SwitchSectionSyntax BuildDefaultSwitchSection(TypeMappingBuildContext ctx, ExpressionSyntax target) { // default: var defaultCaseLabel = DefaultSwitchLabel().AddLeadingLineFeed(ctx.SyntaxFactory.Indentation + 1); // throw new ArgumentException(msg, nameof(ctx.Source)), var sourceType = Invocation(MemberAccess(ctx.Source, GetTypeMethodName)); + var targetType = Invocation(MemberAccess(target, GetTypeMethodName)); var throwExpression = ThrowArgumentExpression( - InterpolatedString($"Cannot map {sourceType} to {TargetType.ToDisplayString()} as there is no known derived type mapping"), + InterpolatedString($"Cannot map {sourceType} to {targetType} as there is no known derived type mapping"), ctx.Source ) .AddLeadingLineFeed(ctx.SyntaxFactory.Indentation + 2);